spring은 bean의 생명주기를 관리하는 3가지 방법이 있다.
- InitializingBean, DisposableBean
- init(), destroy()
- @PostConstruct, @PreDestroy
하나의 bean에 대해 여러 lifecycle이 구성되어 있을 경우, 아래와 같은 순서대로 실행이 된다.
초기화 순서
@PostConstrct → InitializingBean에 정의된 afterPropertiesSet() 콜백 → 사용자 정의 init() 메서드
@PostConstrct
@Sevice
public class LifeCycleTest {
@PostConstruct
public void postConstruct() {
System.out.println("postConstruct");
}
}
InitializingBean
@Service
public class LifeCycleTest implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
}
사용자 정의 init() 메서드
@Bean(initMethod = "init")
public LifeCycleTest lifeCycleTest() {
return new LifeCycleTest();
}
public class LifeCycleTest {
public void init() {
System.out.println("init");
}
}
소멸 순서
@PreDestroy → DisposableBean에 정의된 destory() 콜백 → 사용자 정의 destroy() 메서드
[참고]
반응형
'Framework > spring' 카테고리의 다른 글
| [spring boot] spring boot configuration 이해 (0) | 2017.08.30 |
|---|---|
| [spring] cache (0) | 2017.08.29 |
| 스프링 팁: *Utils 클래스 활용 (0) | 2017.04.03 |
| [spring security] Controller에서 Principal 가져오기 (0) | 2016.03.24 |