Bean Life Cycle (Initialization & Destory)
We have 3 ways to control th life-cycle of Bean.
1) @PostConstruct and @PreDestory
2) afterPropertiesSet() from interface InitializingBean
destory() from interface DisposableBean
3) Standard method naming, init() & destory()
Above are 3 mechanisms, if applying the mechanisms to one bean with different method names,the sequence would be :
INFO [com.vincent.spring.main.LifeCycleBean] - LifeCycleBean Constructor INFO [com.vincent.spring.main.LifeCycleBean] - postConstruct INFO [com.vincent.spring.main.LifeCycleBean] - AfterPropertiesSet INFO [com.vincent.spring.main.LifeCycleBean] - init
if with same names, will only be excuted once.
In applicationcontext.xml, should add below:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
For @PostConstruct & @PreDestory,we need to add the dependency of "javax.annotation" to pom.xml.
<dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>