public class UserDao {
private ConnectionMaker connectionMaker; //초기에 설정하면 사용 중에는 바뀌지 않는 읽기전용 인스턴스 변수
public UserDao(ConnectionMaker connectionMaker) { //생성자
this.connectionMaker = connectionMaker;
}
public class UserDao {
private ConnectionMaker connectionMaker; //초기에 설정하면 사용 중에는 바뀌지 않는 읽기전용 인스턴스 변수
public void setConnectionMaker(ConnectionMaker connectionMaker) { //파라미터로 받은 오브젝트는 인스턴스 변수에 저장해두도록 만든다.
this.connectionMaker = connectionMaker;
}
일반 메소드를 이용한 주입
JAVA XML
@Configuration = <beans>
@Bean = <bean>
- 빈의 이름 : @Bean 메소드 이름이 빈의 이름이다. 이 이름은 getBean()에서 사용된다.
- 빈의 클래스 : 빈 오브젝트를 어떤 클래스를 이용해서 만들지 정의
- 빈의 의존 오브젝트(생략가능) : 빈의 생성자나 수정나 메소드를 통해 의존 오브젝트를 넣어준다.
@Bean //-------------------------------------><bean
public ConnectionMaker
connectionMaker() {//----------------------->id="connectionMaker"
return new DConnectionMaker();//------------>class="~~~...DConnectionMaker"/>
}
property 태그는 name과 ref로 구성
userDao.setConnectionMaker(connectionMaker());
//<property name = "connectionMaker" ref="connectionMaker"/>
완성된 applicationContext.xml
<?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="connectionMaker" class="com.toby.dao.DConnectionMaker"/>
<bean id="userDao" class="com.toby.dao.UserDao">
<property name="connectionMaker" ref="connectionMaker"/>
</bean>
</beans>
//ApplicationContext context = new AnnotationConfigApplicationContext(DaoFactory.class);
ApplicationContext ac = new GenericXmlApplicationContext("applicationContext.xml");