DI(Spring)

xmlファイル式

public class Office {

private String officeNo;

// getter/setter

@Override

public String toString() {

return "officeNo:" + officeNo;

}

}

public class Car {

private String brand;

private double price;

// getter/setter

@Override

public String toString() {

return "brand:" + brand + "," + "price:" + price;

}

}

public class Boss {

private Car car;

private Office office;

// getter/setter

@Override

public String toString() {

return "car:" + car + "\n" + "office:" + office;

}

}

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="boss" class="com.test.Boss">

<property name="car" ref="car"/>

<property name="office" ref="office" />

</bean>

<bean id="office" class="com.test.Office">

<property name="officeNo" value="001"/>

</bean>

<bean id="car" class="com.test.Car" scope="singleton">

<property name="brand" value="Toyota"/>

<property name="price" value="500"/>

</bean>

</beans>

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

Boss boss = (Boss) ac.getBean("boss");

インタフェース例

public class UserServiceImpl implements UserService {

private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

public void addUser(User user) {

userDao.saveUser(user);

}

}

public class UserDaoImpl implements UserDao {

public void saveUser(User user) {

// do something

}

}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="a" class="org.xxx.dao.UserDaoImpl"></bean>

<bean id="b" class="org.xxx.dao.xxxDaoImpl"></bean>

<bean name="userService" class="org.xxx.bo.UserServiceImpl">

<property name="userDao" ref="a"></property>

</bean>

</beans>

ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

UserService service = (UserService) ac.getBean("userService");

service.addUser(new User());

アノテーション式(Spring 2.5から)

1、@Autowired org.springframework.beans.factory.annotation.Autowired;

2、@Qualifier org.springframework.beans.factory.annotation.Qualifier;

3、@Resource javax.annotation.Resource;

4、@Component org.springframework.stereotype.Component;

@Autowired, @Qualifier, @Resource

設定ファイルにbeanを書く必要がある

//★方法1

public class Boss {

@Autowired

private Car car;

@Autowired

private Office office;

//setterを削除

}

//★方法2

public class Boss {

private Car car;

private Office office;

//AutowiredはデフォルトがbyType、必要な場合はQualifierでbyName

@Autowired

public void setCar(@Qualifier(value="car")Car car) {

this.car = car;

}

@Autowired

public void setOffice(Office office) {

this.office = office;

}

// getter

}

//★方法3

public class Boss {

private Car car;

private Office office;

@Autowired

public Boss(Car car, Office office){

this.car = car;

this.office = office ;

}

// setter/getter

}

//★方法4 推薦

public class Boss {

private Car car;

private Office office;

//ResourceはデフォルトがbyName

@Resource

public void setCar(Car car) {

this.car = car;

}

@Resource(name="office")

public void setOffice(Office office) {

this.office = office;

}

// getter

}

AutowiredAnnotationBeanPostProcessorのみ

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- @AutowiredをつけるBeanに自動的にDI -->

<bean class="org.springframework.beans.factory.annotation.

AutowiredAnnotationBeanPostProcessor"/>

<!-- 設定はシンプルになる -->

<bean id="boss" class="com.test.Boss"/>

<bean id="office" class="com.test.Office">

<property name="officeNo" value="001"/>

</bean>

<bean id="car" class="com.test.Car" scope="singleton">

<property name="brand" value="Toyota"/>

<property name="price" value="500"/>

</bean>

</beans>

AutowiredAnnotationBeanPostProcessor

CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor

RequiredAnnotationBeanPostProcessor

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config/>

<bean>...省略...</bean>

</beans>

@Component

設定ファイルにbeanを書く必要がない

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.test"/>

</beans>

@Component(name="boss")

public class Boss {

private Car car;

private Office office;

@Resource

public void setCar(Car car) {

this.car = car;

}

@Resource

public void setOffice(Office office) {

this.office = office;

}

// getter

}

インタフェース例

@Component("userService")

public class UserServiceImpl implements UserService {

@Resource(name="userDao")

private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

public void addUser(User user) {

userDao.saveUser(user);

}

}

@Component("userDao")

public class UserDaoImpl implements UserDao {

public void saveUser(User user) {

// do something

}

}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="org.xxx.*"/>

</beans>