- Bean은 객체 생성 -> 의존관계 주입의 LifeCycle을 가짐
- Component Scan을 통해 Bean들을 스캔 (
@Configuration
, @Service
, ...) 해 IoC Container에 등록
- Bean 객체 생성
- Dependency Injection
- 생성자 주입 방식의 경우, 2번과 3번이 한 단계에서 일어남
LifeCycle Callback
@PostConstruct, @PreDestroy
@Component
public class Foo {
@PostConstruct
public void foo() { ... }
@PreDestroy
public void bar() { ... }
}
InitializingBean, DisposableBean
public class Foo implements InitializingBean {
@Override
public void afterPropertiesSet() { ... }
}
public class Foo implements DisposableBean {
@Override
public void destroy() { ... }
}
@Bean(initMethod), @Bean(destroyMethod)
@Bean(initMethod = "init", destroyMethod = "destroy")
public Foo initFoo() {
return new Foo();
}
class Foo {
public void init() { ... }
public void destroy() { ... }
}
실행 순서
@PostConstruct
, @PreDestroy
InitializingBean
, DisposableBean
@Bean(initMethod)
, @Bean(destroyMethod)