# DI 개념
각 클래스 간의 의존관계를 빈 설정(Bean Definition) 정보를 바탕으로 컨테이너가 자동으로 연결해주는 것을 말함
개발자들은 단지 빈 설정파일에서 의존관계가 필요하다는 정보를 추가하면 됨
객체 레퍼런스를 컨테이너로부터 주입 받아서, 실행 시에 동적으로 의존관계가 생성됨
컨테이너가 흐름의 주체가 되어 애플리케이션 코드에 의존관계를 주입해주는 것
이를 통해 코드가 단순해지며, 컴포넌트 간의 결합도(Coupling)가 제거된다.
# DI 유형
# Spring DI 컨테이너 개념
Spring DI 컨테이너가 관리하는 객체를 Bean(빈) 이라고 하고, 이 빈들을 관리한다는 의미로 컨테이너를 빈 팩토리(Bean Factory)라고 부른다.
# 실습 1. Service 클래스를 만들어 DAO를 참조
package kr.co.acomp.hello;
public class HelloDAO {
// simple function
public int addTwoNumber(int a, int b) {
return a + b;
}
}
package kr.co.acomp.hello.service;
import kr.co.acomp.hello.HelloDAO;
public class HelloService {
private HelloDAO dao;
public int calcTwoNumbers(int a, int b) {
// 직접 객체 생성
dao = new HelloDAO();
return dao.addTwoNumber(a, b);
}
}
src/main/resources/spring-context.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="helloService" class="kr.co.acomp.hello.service.HelloService" />
<bean id="helloDAO" class="kr.co.acomp.hello.HelloDAO" />
</beans>
main 코드 수정
package kr.co.acomp.hello;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import kr.co.acomp.hello.service.HelloService;
public class HelloMain {
public static void main(String[] args) {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("/spring-context.xml");
HelloService service= ctx.getBean("helloService", HelloService.class);
int result = service.calcTwoNumbers(5, 67);
System.out.println(result);
}
}
# 실습 2. Dependency Injection을 통한 참조
위의 방법 1은 service 객체에서 new를 통해 DAO 객체를 직접 만들어내기에 효율적이지 못함
Spring Container가 자동으로 관리해주는 것이 보다 효율적임
1) Setter 통한 Injection
package kr.co.acomp.hello.service;
import kr.co.acomp.hello.HelloDAO;
public class HelloService {
private HelloDAO helloDAO;
// setter만 만들어주면 됨
public void setHelloDAO(HelloDAO dao) {
this.helloDAO = dao;
}
public int calcTwoNumbers(int a, int b) {
return helloDAO.addTwoNumber(a, b);
}
}
설정파일을 통해서 Setter를 등록하여 자동으로 Injection이 되도록 설정
src/main/resources/spring-context.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="helloService" class="kr.co.acomp.hello.service.HelloService">
<property name="helloDAO" ref="helloDAO" />
</bean>
<bean id="helloDAO" class="kr.co.acomp.hello.HelloDAO" />
</beans>
2) Constructor를 통한 Injection)
package kr.co.acomp.hello.service;
import kr.co.acomp.hello.HelloDAO;
public class HelloService {
private HelloDAO helloDAO;
// constructor를 통해 Injection
public HelloService(HelloDAO dao) {
this.helloDAO = dao;
}
public int calcTwoNumbers(int a, int b) {
return helloDAO.addTwoNumber(a, b);
}
}
설정파일을 통해서 Constructor를 통해 자동으로 Injection이 되도록 설정
src/main/resources/spring-context.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="helloService" class="kr.co.acomp.hello.service.HelloService">
<constructor-arg ref="helloDAO" />
</bean>
<bean id="helloDAO" class="kr.co.acomp.hello.HelloDAO" />
</beans>
'Archived(Programming) > Spring #2(기초)' 카테고리의 다른 글
Spring MVC (0) | 2020.03.11 |
---|---|
Layered Architecture & @Component (4) | 2020.03.10 |
IoC(Inversion of Control) (0) | 2020.03.10 |
Spring과 Maven (0) | 2020.03.09 |
Spring 간략 소개 (2) | 2020.03.09 |