Bean在Spring容器中的生命周期

   Bean在Spring容器中的生命周期

配置在Spring中的Bean在Spring容器中从加载到销毁会经历那些过程呢?如果实现一些特定的Spring接口,这些特定接口的方法会在什么时候被调用呢?本文简单介绍一下这些过程.

Bean在Spring容器中的生命周期如下图所示:

 

1,调用Bean的构造函数(或者工厂方法)实例化Bean.
2,对Bean的成员变量赋值.
3,如果Bean实现了BeanNameAware,调用Bean的setBeanName方法.
4,如果Bean实现了BeanFactoryAware,调用Bean的setBeanFactory方法.
5,如果Bean实现了ApplicationContextAware,调用Bean的setApplicationContext方法.
6,如果容器中配置了BeanPostProcessor,调用BeanPostProcessor的postProcessBeforeInitialization方法(如果有多个BeanPostProcessor,调用每一个BeanPostProcessor的postProcessBeforeInitialization方法).
6,如果Bean实现了InitializingBean,调用Bean的afterPropertiesSet方法.
7,如果Bean配置了init-method方法,调用init-method配置的Bean方法.
8,如果容器中配置了BeanPostProcessor,调用BeanPostProcessor的postProcessAfterInitialization方法.(如果有多个BeanPostProcessor,调用每一个BeanPostProcessor的postProcessAfterInitialization方法).
9,Bean处于可以使用的状态.
10,Spring容器关闭.
11,4,如果Bean实现了DisposableBean,调用Bean的destroy方法.
12,如果Bean配置了destroy-method方法,调用destroy-method配置的Bean的方法.

实例:LifeCycleBean implements InitializingBean, DisposableBean,BeanNameAware,BeanFactoryAware, ApplicationContextAware,配置了init-method="init" destroy-method="cleanup",容器中有两个BeanPostProcessor

 

[java]   view plain copy
  1. public class LifeCycleBean implements InitializingBean, DisposableBean,  
  2.         BeanNameAware,BeanFactoryAware, ApplicationContextAware {  
  3.     private String str = "default";  
  4.     public LifeCycleBean() {  
  5.         System.out.println("construct LifecycleBean ***************");  
  6.     }  
  7.     public LifeCycleBean(String str) {  
  8.         this.str = str;  
  9.     }  
  10.     public String getStr() {  
  11.         return str;  
  12.     }  
  13.     public void setStr(String str) {  
  14.         System.out.println("setStr ***************");  
  15.         this.str = str;  
  16.     }  
  17.     public void init() {  
  18.         System.out.println("init mtd ***************");  
  19.     }  
  20.     public void cleanup() {  
  21.         System.out.println("cleanup mtd ***************");  
  22.     }  
  23.       
  24.     public void afterPropertiesSet() throws Exception {  
  25.         System.out.println("afterPropertiesSet ***************");  
  26.     }  
  27.     public void destroy() throws Exception {  
  28.         System.out.println("destroy ***************");  
  29.     }     
  30.     public void setApplicationContext(ApplicationContext arg0)  
  31.             throws BeansException {  
  32.         System.out.println("setApplicationContext***************");  
  33.     }  
  34.     public void setBeanFactory(BeanFactory arg0) throws BeansException {  
  35.         System.out.println("setBeanFactory***************");  
  36.     }  
  37.     public void setBeanName(String arg0) {  
  38.         System.out.println("setBeanName***************" + arg0);              
  39.     }  
  40. }  
  41. public class MyBeanPostProcessor implements BeanPostProcessor {  
  42.     public Object postProcessBeforeInitialization(Object bean, String beanName)  
  43.             throws BeansException {  
  44.         System.out.println("************** MyBeanPostProcessor postProcessBeforeInitialization Bean '" + beanName);  
  45.         return bean;  
  46.     }  
  47.     public Object postProcessAfterInitialization(Object bean, String beanName)  
  48.             throws BeansException {  
  49.         System.out.println("************** MyBeanPostProcessor postProcessAfterInitialization Bean '" + beanName);  
  50.         return bean;  
  51.     }  
  52. }  
Spring配置
[html]   view plain copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  5.     <bean class="com.test.spring.beanfactoryCtx.MyBeanPostProcessor" />  
  6.     <bean class="com.test.spring.beanfactoryCtx.MyBeanPostProcessor1" />  
  7.     <bean name="lifeCycleBean" class="com.test.spring.beanfactoryCtx.LifeCycleBean" init-method="init" destroy-method="cleanup">  
  8.         <property name="str" value="test str"/>  
  9.     </bean>  
  10. </beans>  
调用代码:注意在从main方法启动Spring的话,需要 registerShutdownHook才能看到destroy方法和由destroy-method配置的方法的调用.
[java]   view plain copy
  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:conf/bfactoryCtx1.xml");  
  2. ((ClassPathXmlApplicationContext)ctx).registerShutdownHook();  
  3. LifeCycleBean bean1 = ctx.getBean("lifeCycleBean",LifeCycleBean.class);  
  4. System.out.println("***********" + bean1 +  "   " + bean1.getStr());  
输出结果如下:
[java]   view plain copy
  1. construct LifecycleBean ***************  
  2. setStr ***************       #1  
  3. setBeanName***************lifeCycleBean  
  4. setBeanFactory***************  
  5. setApplicationContext***************  
  6. ************** MyBeanPostProcessor postProcessBeforeInitialization Bean 'lifeCycleBean  #2  
  7. ************** MyBeanPostProcessor1 postProcessBeforeInitialization Bean 'lifeCycleBean #2  
  8. afterPropertiesSet ***************  
  9. init mtd ***************  
  10. ************** MyBeanPostProcessor postProcessAfterInitialization Bean 'lifeCycleBean   #2  
  11. ************** MyBeanPostProcessor1 postProcessAfterInitialization Bean 'lifeCycleBean  #2  
  12. ***********com.test.spring.beanfactoryCtx.LifeCycleBean@157fb52   test4                 #3  
  13. destroy ***************  
  14. cleanup mtd ***************  
#1为成员变量赋值的步骤
#2如果有多个BeanPostProcessor的话,每一个BeanPostProcessor都会被调用一次.
#3Bean 被客户端代码使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值