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>
本文深入探讨了Spring框架中Bean的生命周期控制机制,包括@PostConstruct和@PreDestory注解、InitializingBean接口和DisposableBean接口,以及标准命名方法init()和destory()的应用。同时,介绍了如何在应用上下文中配置这些机制,并强调了使用CommonAnnotationBeanPostProcessor的必要性。
8820

被折叠的 条评论
为什么被折叠?



