DI란?
- 종속성 주입 (쉽게 말해 객체 조립)
- Object에 lookup코드를 사용하지 않고 컨테이너가 직접 의존 구조를 Object에 설정할 수 있도록 지정해 주는 방식
class A{
private B b;
public A(){
b = new B();
}
class A{
private B b;
public void setB(b){
this.b = b;
}
Injection의 2가지 방법
- Setter Injection
- Construction Injection
Container
- 객체의 생성, 사용, 소멸에 해당하는 라이프사이클 담당
- 라이프사이클을 기본으로 애플리케이션 사용에 필요한 조요 기능을 제공
Container의 필요성
- 비즈니스 로직 외에 부가적인 기능들에 대해서는 독립적으로 관리되도록 하기 위해
- Configuration에 대한 일관성을 갖기 위함
IoC 컨테이너
- 오브젝트의 생성과 관계설정, 사용, 제거 등의 작업을 애플리케이션 코드 대신 처리해주는 독립적 컨테이너
Dependency Injection
spring에 전달될 지시서를 만들자
1) Exam exam = new Exam();
<bean id="exam" class="com.ssafy.test.Exam" p:kor="10" p:eng="20">
</bean>
2) ExamConsole console = new GridExamConsole();
console.setExam(exam)
<bean id="console" class="com.ssafy.test.GridExamConsole">
<property name="exam" ref="exam" />
</bean>
3) List<'Exam'> = new ArrayList();
<bean id="exams" class="java.util.ArrayList" >
<constructor-arg>
<list>
<bean class="com.ssafy.test.NewlecExam" p:kor="1" p:eng="1"/>
<ref bean="exam"/>
</list>
</constructor-arg>
</bean>
<util:list id="exams" list-class="java.util.ArrayList">
<bean class="com.ssafy.test.NewlecExam" p:kor="1" p:eng="1"/>
<ref bean="exam"/>
</util:list>