Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
来源:互联网 发布:数据图表设计案例 编辑:程序博客网 时间:2024/05/16 18:26
Spring 容器中的 Bean 是有生命周期的,Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作。下面是常用的三种指定特定操作的方法:
- 通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
- 通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
- 在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。
这几种配置方式,执行顺序是怎样的呢?我们通过例子来研究下:
Java类:
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class InitAndDestroySeqBean implements InitializingBean,DisposableBean {
- public InitAndDestroySeqBean(){
- System.out.println("执行InitAndDestroySeqBean: 构造方法");
- }
- @PostConstruct
- public void postConstruct() {
- System.out.println("执行InitAndDestroySeqBean: postConstruct");
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet");
- }
- public void initMethod() {
- System.out.println("执行InitAndDestroySeqBean: init-method");
- }
- @PreDestroy
- public void preDestroy() {
- System.out.println("执行InitAndDestroySeqBean: preDestroy");
- }
- @Override
- public void destroy() throws Exception {
- System.out.println("执行InitAndDestroySeqBean: destroy");
- }
- public void destroyMethod() {
- System.out.println("执行InitAndDestroySeqBean: destroy-method");
- }
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/chj/spring/bean.xml");
- context.close();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config/>
- <bean id="initAndDestroySeqBean" class="com.chj.spring.InitAndDestroySeqBean" init-method="initMethod" destroy-method="destroyMethod"/>
- </beans>
- 2013-03-03 17:11:19,098 DEBUG support.DefaultListableBeanFactory - Creating instance of bean 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: 构造方法
- 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found init method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
- 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found destroy method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Eagerly caching bean 'initAndDestroySeqBean' to allow for resolving potential circular references
- 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
- 执行InitAndDestroySeqBean: postConstruct
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: afterPropertiesSet
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking init method 'initMethod' on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: init-method
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Finished creating instance of bean 'initAndDestroySeqBean'
- 2013-03-03 17:11:19,129 INFO support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@56a499: display name [org.springframework.context.support.ClassPathXmlApplicationContext@56a499]; startup date [Sun Mar 03 17:11:17 CST 2013]; root of context hierarchy
- 2013-03-03 17:11:19,129 INFO support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1292d26: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,initAndDestroySeqBean]; root of factory hierarchy
- 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking destroy method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
- 执行InitAndDestroySeqBean: preDestroy
- 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy() on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: destroy
- 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy method 'destroyMethod' on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: destroy-method
Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method
Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method 0 0
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- 在Spring容器中Bean的几种初始化、销毁方法的实现和先后顺序
- Spring容器中定义Bean几种初始化方法和销毁方法
- Spring——Bean的初始化方法和销毁方法
- spring bean初始化和销毁方法
- Spring bean初始化和销毁方法
- Arraylist和linkedlist的性能测试
- alsa声卡驱动移植
- Python--网页更新监控工具
- Linux系统中对SSD硬盘优化的方法
- python 读取图片数据
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- CCF-最大矩形
- 网络爬虫开发技术——快速线程池爬虫
- linux下指定mysql数据库服务器主从同步的配置实例
- [ios]使用Masonry为自定义view添加约束时,得先把自定义view添加到父视图上
- Mockito单元测试-注解的详细使用
- ffplay不能直播red5上的直播流问题解决
- TestDataStream的用法
- 设计模式之原型模式