티스토리 뷰
728x90
반응형
SMALL
Spring IoC, DI
IoC(Inversion of Control) Container
BeanFactory
- IoC를 담당하는 핵심 Container
- Bean들의 의존성, lifeCycle을 관리
ApplicationContext
- BeanFactory 인터페이스를 상속
- BeanFactory에서 제공하는 기능 외에, AOP, Message처리, 이벤트처리 등의 기능 제공
- Bean을 생성할 때, 모든 Bean을 singleton으로 생성하여 관리한다
Bean 등록 방법
- Component Scan
- @Component, @Repository, @Service, @Controller, @Configuration 등등의 Annotation들이 있다
- Java 설정파일로 등록
- 설정파일을 이용하여 Bean을 등록하면, @Controller를 생성하지 않고 Bean으로 등록
DI (Dependency Injection) - 의존성 주입
- @Autowired Annotaion
- Field, Setter, Constructor 등 여러곳에서 사용할 수 있다
- Field Injection
- Bean으로 등록된 객체를, 사용하려고하는 클래스에 Field로 선언하고 @Autowired
- 권장하지 않음(단일 책임의 원칙 위반 등)
- ex)
@Service
public class BeanSample {
@Autowired
private TestDao testDao;
....
}
- Setter Injection
- Setter 메서드에 @Autowired를 붙여 DI구현
- ex)
public class BeanSample {
private TestDao testDao;
@Autowired
public void setTestDao(TestDao testDao) {
this.testDao= testDao;
}
}
- Constructor Injection
- 위의 Field Injection의 단점을 보완한 패턴
- ex)
public class BeanSample {
private TestDao testDao;
@Autowired
public BeanSample (TestDao testDao) {
this.testDao= testDao;
}
}
728x90
반응형
LIST
'Spring boot study > 기타...' 카테고리의 다른 글
java singleton 정리 (0) | 2020.11.11 |
---|---|
Controller의 리턴타입 정리 (0) | 2020.07.15 |
댓글