<properties>
<!-- Generic properties -->
<java.version>11.0.5</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>5.1.5.RELEASE</spring-framework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
</dependencies>
<bean id="mBean" class="pack.Message1" />
<!-- pack.Message1 mBean = new pack.Message1();과
유사함. Singleton -->
public static void main(String[] args) {
Message1 message1 = new Message1();
message1.sayHello("홍길동");
Message2 message2 = new Message2();
message2.sayHello("홍길동2");
System.out.println("----");
MessageInter inter;
inter = message1;
inter.sayHello("홍길동2");
inter = message2;
inter.sayHello("신기해");
System.out.println("-------");
ApplicationContext context =
new ClassPathXmlApplicationContext("init.xml");
MessageInter inter2 = (MessageInter)context.getBean("mBean");
inter2.sayHello("홍길동3");
}
MVN repository에서 Spring Context 설치 후 pom.xml에 추가 .
(해당 단계는 위에서 이미 적용해보았다.
DB처리 용 DataDao 인터페이스 생성 .(model)
public interface DataDao {
void selectData();
//...
}
public void selectData() {
System.out.println("selectData수행");
}
public interface SelectService {
void selectProcess();
}
public class SelectServiceImp1 implements SelectService{
private DataDao dataDao; //
//생성자 주입(constructor Injection) : private 인터페이스 멤버에 접근하기 위함
public SelectServiceImp1(DataDao dataDao) {
this.dataDao = dataDao;
}
public void selectProcess() {
System.out.println("selectProcess가 DB처리를 위해 DataDao의 파생클래스 사용");
dataDao.selectData();
}
}
public static void main(String[] args) {
//기존방법
DataDaoImp1 daoImp1 = new DataDaoImp1();
DataDao dataDao = daoImp1;
SelectServiceImp1 imp1 = new SelectServiceImp1(dataDao);
SelectService selectService = imp1;
selectService.selectProcess();
}
DataDao dataDao = daoImp1;
에서 다형성의 특징을 살려 주소를 넘겼다.SelectService
에 넣어 select처리를 시작하도록 했다. selectServiceImp1
의 Bean을 생성한다.<bean id="selectServiceImp1" class="pack.controller.SelectServiceImp1">
<constructor-arg>
<ref bean="dataDaoImp1" />
</constructor-arg>
<bean name="dataDaoImp1" class="pack.model.DataDaoImp1" />
</bean>
<constructor-arg>
태그로 가능하다. //xml정보 넘김
ApplicationContext context = new ClassPathXmlApplicationContext("init.xml");
//xml속 bean명을 넘김
SelectService selectService2 = (SelectService)context.getBean("selectServiceImp1");
selectService.selectProcess();
ApplicationContext
init.xml 의 context객체를 만들고, context의 getBean으로 앞에서 선언한 bean id를 불렀다.Config.java
@Configuration
public class Config {
public DataDaoImp1 dataDaoImp1() { //싱글톤
DataDaoImp1 dataDaoImp1 = new DataDaoImp1();
return dataDaoImp1;
}
@Bean("selectServiceImp1") //getBean 호출시 불릴 이름. 생략됨.
public SelectServiceImp1 selectServiceImp1() {
SelectServiceImp1 selectServiceImp1 = new SelectServiceImp1(dataDaoImp1());
return selectServiceImp1;
}
}
DataDaoImp1
을 @Bean 이 적용된 컨트롤러 메소드에 넣었다. AnnotationConfigApplicationContext context2 =
new AnnotationConfigApplicationContext(Config.class);
SelectService selectService3 = (SelectService)context.getBean("selectServiceImp1");
selectService.selectProcess();