spring中Bean的生命周期总结

spring中在ApplicationContext或在BeanFactory中Bean的生命周期总结

 Spring中Bean的生命周期,在学习spring的过程中bean的生命周期理解对学习spring有很大的帮助,下面我就分别介绍在ApplicationContextBeanFactoryBean的生命周期。

1、在ApplicationContextBean的生命周期

生命周期执行的过程如下:

1、需找所有的bean根据bean定义的信息来实例化bean

2、使用依赖注入,spring按bean定义信息配置bean的所有属性

3、若bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递bean的ID。

4、若bean实现了BeanFactoryAware接口,工厂调用setBeanFactory() 方法传入工厂自身。

5、若bean实现了ApplicationContextAware()接口,setApplicationContext()方法会被调用

6、若BeanPostProcessor和bean关联,

则它们的postProcessBeforeInitialization()方法被调用

7若bean指定了init-method="init"方法、,它将被调用。

8若有BeanPostProcessor和bean关联,

     则它们的postProcessAfterInitialization()方法被调用

    注意:通过已上操作,此时的Bean就可以被应用的系统使用,并讲保留在BeanFactory工厂中直到不再需要为止.但我们也可以通过9或者10进行销毁

9、若bean实现了DisposableBean接口,distroy()方法被调用

10、如果指定了destroy-method="close"定制的销毁方法,就调用这个方法

案例分析:

   创建一个实体Bean代码如下:

  1. package www.csdn.spring.dao;  
  2.   
  3.   
  4. import org.springframework.beans.BeansException;  
  5.   
  6. import org.springframework.beans.factory.BeanFactory;  
  7.   
  8. import org.springframework.beans.factory.BeanFactoryAware;  
  9.   
  10. import org.springframework.beans.factory.BeanNameAware;  
  11.   
  12. import org.springframework.beans.factory.DisposableBean;  
  13.   
  14. import org.springframework.beans.factory.InitializingBean;  
  15.   
  16. import org.springframework.context.ApplicationContext;  
  17.   
  18. import org.springframework.context.ApplicationContextAware;  
  19.   
  20.   
  21. public class HelloDaoImpl implements BeanNameAware, BeanFactoryAware,  
  22.   
  23. InitializingBean, ApplicationContextAware, DisposableBean {  
  24.   
  25.   
  26. private String content;  
  27.   
  28.   
  29. public HelloDaoImpl() {  
  30.   
  31. System.out.println("----------HelloDaoImpl实例化");  
  32.   
  33. }  
  34.   
  35.   
  36. public void setContent(String content) {  
  37.   
  38. System.out.println("----------通过依赖注入的内容是:" + content);  
  39.   
  40. this.content = content;  
  41.   
  42. }  
  43.   
  44.   
  45. @Override  
  46.   
  47. public void setBeanName(String beanId) {  
  48.   
  49. System.out.println("----------输出BeanId:" + beanId);  
  50.   
  51. }  
  52.   
  53.   
  54. @Override  
  55.   
  56. public void setBeanFactory(BeanFactory factory) throws BeansException {  
  57.   
  58. System.out.println("----------factory:" + factory.getClass());  
  59.   
  60.   
  61. }  
  62.   
  63.   
  64. @Override  
  65.   
  66. public void setApplicationContext(ApplicationContext applicationContext)  
  67.   
  68. throws BeansException {  
  69.   
  70. System.out.println("----------" + applicationContext);  
  71.   
  72.   
  73. }  
  74.   
  75.   
  76. @Override  
  77.   
  78. public void afterPropertiesSet() throws Exception {  
  79.   
  80. System.out.println("----------afterPropertiesSet");  
  81.   
  82. }  
  83.   
  84.   
  85. public void init() {  
  86.   
  87. System.out.println("----------初始化方法");  
  88.   
  89. }  
  90.   
  91.   
  92. @Override  
  93.   
  94. public void destroy() throws Exception {  
  95.   
  96. System.out.println("----------bean被销毁");  
  97.   
  98.   
  99. }  
  100.   
  101.   
  102. public void close() {  
  103.   
  104. System.out.println("----------close");  
  105.   
  106. }  
  107.   
  108.   
  109. }  
package www.csdn.spring.dao;


import org.springframework.beans.BeansException;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.BeanFactoryAware;

import org.springframework.beans.factory.BeanNameAware;

import org.springframework.beans.factory.DisposableBean;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;


public class HelloDaoImpl implements BeanNameAware, BeanFactoryAware,

InitializingBean, ApplicationContextAware, DisposableBean {


private String content;


public HelloDaoImpl() {

System.out.println("----------HelloDaoImpl实例化");

}


public void setContent(String content) {

System.out.println("----------通过依赖注入的内容是:" + content);

this.content = content;

}


@Override

public void setBeanName(String beanId) {

System.out.println("----------输出BeanId:" + beanId);

}


@Override

public void setBeanFactory(BeanFactory factory) throws BeansException {

System.out.println("----------factory:" + factory.getClass());


}


@Override

public void setApplicationContext(ApplicationContext applicationContext)

throws BeansException {

System.out.println("----------" + applicationContext);


}


@Override

public void afterPropertiesSet() throws Exception {

System.out.println("----------afterPropertiesSet");

}


public void init() {

System.out.println("----------初始化方法");

}


@Override

public void destroy() throws Exception {

System.out.println("----------bean被销毁");


}


public void close() {

System.out.println("----------close");

}


}

 

创建BeanPostProcessor实现类

  1. package www.csdn.spring.dao;  
  2.   
  3.   
  4. import org.springframework.beans.BeansException;  
  5.   
  6. import org.springframework.beans.factory.config.BeanPostProcessor;  
  7.   
  8.   
  9. public class BeanPostProcessorImpl implements BeanPostProcessor {  
  10.   
  11. @Override  
  12.   
  13. public Object postProcessBeforeInitialization(Object bean, String beanName)  
  14.   
  15. throws BeansException {  
  16.   
  17. System.out.println("我把:" + beanName + "实例化化之前的操作");  
  18.   
  19. return bean;  
  20.   
  21. }  
  22.   
  23.   
  24. @Override  
  25.   
  26. public Object postProcessAfterInitialization(Object bean, String beanName)  
  27.   
  28. throws BeansException {  
  29.   
  30. System.out.println("我把:" + beanName + "实例化化之后的操作");  
  31.   
  32. return bean;  
  33.   
  34. }  
  35.   
  36.   
  37. }  
package www.csdn.spring.dao;


import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;


public class BeanPostProcessorImpl implements BeanPostProcessor {

@Override

public Object postProcessBeforeInitialization(Object bean, String beanName)

throws BeansException {

System.out.println("我把:" + beanName + "实例化化之前的操作");

return bean;

}


@Override

public Object postProcessAfterInitialization(Object bean, String beanName)

throws BeansException {

System.out.println("我把:" + beanName + "实例化化之后的操作");

return bean;

}


}

 

在配置文件中配置实体BeanBeanPostProcessorImpl 特殊的Bean

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11.   
  12.   
  13. <!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->  
  14.   
  15. <bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl"  
  16.   
  17. scope="singleton" lazy-init="false" init-method="init" destroy-method="close">  
  18.   
  19. <!-- 通过set方法注入 -->  
  20.   
  21. <property name="content" value="陈老师早上好!" />  
  22.   
  23. </bean>  
  24.   
  25.   
  26. <!-- BeanPostProcessor与当前的bean都进行关联 -->  
  27.   
  28. <bean class="www.csdn.spring.dao.BeanPostProcessorImpl" />  
  29.   
  30.   
  31. </beans>  
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans.xsd">



<!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->

<bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl"

scope="singleton" lazy-init="false" init-method="init" destroy-method="close">

<!-- 通过set方法注入 -->

<property name="content" value="陈老师早上好!" />

</bean>


<!-- BeanPostProcessor与当前的bean都进行关联 -->

<bean class="www.csdn.spring.dao.BeanPostProcessorImpl" />


</beans>

 

创建测试类BeanTest 

  1. package www.csdn.spring.bean.cycle;  
  2.   
  3.   
  4. import org.junit.Test;  
  5.   
  6. import org.springframework.context.ApplicationContext;  
  7.   
  8. import org.springframework.context.ConfigurableApplicationContext;  
  9.   
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  11.   
  12.   
  13. public class BeanTest {  
  14.   
  15.   
  16. @Test  
  17.   
  18. public void test() {  
  19.   
  20. ApplicationContext context = new ClassPathXmlApplicationContext(  
  21.   
  22. "classpath:spring-dao.xml");  
  23.   
  24. // 强制造型   
  25.   
  26. ConfigurableApplicationContext cont = (ConfigurableApplicationContext) context;  
  27.   
  28. // 执行关闭 可以考到 destory-method的方法的执行   
  29.   
  30. cont.close();  
  31.   
  32.   
  33. }  
  34.   
  35. }  
package www.csdn.spring.bean.cycle;


import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BeanTest {


@Test

public void test() {

ApplicationContext context = new ClassPathXmlApplicationContext(

"classpath:spring-dao.xml");

// 强制造型

ConfigurableApplicationContext cont = (ConfigurableApplicationContext) context;

// 执行关闭 可以考到 destory-method的方法的执行

cont.close();


}

}

 

执行结果如下:

总结:在ApplicationContextBean的生命周期

2、在 BeanFactoryBean的生命周期

     在BeanFactoryBean的生命周期与ApplicationContextbean的生命周期唯一不同的是:若bean实现了ApplicationContextAware()接口,在ApplicationContextbean的生命周期中setApplicationContext()方法会被调用,而在BeanFactoryBean的生命周期此方法不会被调用。

案例采用上个案例:测试类中测试代码如下:

  1. // 创建资源对象   
  2.   
  3. ClassPathResource resource = new ClassPathResource("spring-dao.xml");  
  4.   
  5. // 采用BeanFactory初始化容器   
  6.   
  7. ConfigurableBeanFactory cbf = new XmlBeanFactory(resource);  
  8.   
  9. // bean后置处理器必须通过addBeanPostProcessor(new BeanPostProcessorImpl())手动添加  
  10.   
  11. cbf.addBeanPostProcessor(new BeanPostProcessorImpl());  
  12.   
  13. //在配置文件中给BeanPostProcessorImpl的bean加上id="beanPostProcessorImpl" //cbf.addBeanPostProcessor(cbf.getBean("beanPostProcessorImpl", BeanPostProcessorImpl.class));  
  14.   
  15. // 获取Bean对象   
  16.   
  17. cbf.getBean("helloDaoImpl", HelloDaoImpl.class);  
  18.   
  19. // 销毁   
  20.   
  21. cbf.destroySingletons();  
// 创建资源对象

ClassPathResource resource = new ClassPathResource("spring-dao.xml");

// 采用BeanFactory初始化容器

ConfigurableBeanFactory cbf = new XmlBeanFactory(resource);

// bean后置处理器必须通过addBeanPostProcessor(new BeanPostProcessorImpl())手动添加

cbf.addBeanPostProcessor(new BeanPostProcessorImpl());

//在配置文件中给BeanPostProcessorImpl的bean加上id="beanPostProcessorImpl" //cbf.addBeanPostProcessor(cbf.getBean("beanPostProcessorImpl", BeanPostProcessorImpl.class));

// 获取Bean对象

cbf.getBean("helloDaoImpl", HelloDaoImpl.class);

// 销毁

cbf.destroySingletons();

 

执行结果:

总结:在BeanFactoryBean的生命周期

另外说明:

     应用上下文与bean工厂的另一个重要区别是关于单例bean如何被加载。

bean工厂延迟加载所有bean,直到getBean()方法被调用。

应用上下文会在启动后预载入所有单例bean.这样可确保应用不需要等待他们被创建。

  • 16
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 对于SpringBean生命周期Spring在容器有5个生命周期阶段,它们分别是:实例化、属性设置、初始化、销毁以及单例实例化,每个阶段的bean都会触发一个对应的回调事件,让开发者能够在每个阶段做一些额外的处理。 ### 回答2: SpringBean 生命周期包括以下几个阶段: 1. Bean 实例化:Spring 在启动时会通过反射机制实例化 Bean。这是 Bean 生命周期的开始阶段。 2. Bean 属性设置:Spring 在实例化 Bean 后,会根据配置文件或注解等方式,将属性值注入到 Bean 。 3. Bean 初始化前置处理:如果 Bean 实现了 InitializingBean 接口,那么 Spring 会在属性设置完成后调用 Bean 的 afterPropertiesSet() 方法进行初始化前的处理。 4. Bean 初始化后置处理:如果 Bean 配置了 init-method 方法,或者在配置文件通过 init-method 属性指定了初始化方法,那么 Spring 会在初始化前置处理完成后调用该方法。 5. Bean 使用阶段:在初始化完成后,Bean 就可以被应用程序使用了。 6. Bean 销毁前置处理:如果 Bean 实现了 DisposableBean 接口,那么在关闭应用程序或手动销毁 Bean 时,Spring 会先调用 Bean 的 destroy() 方法进行销毁前的处理。 7. Bean 销毁后置处理:如果 Bean 配置了 destroy-method 方法,或者在配置文件通过 destroy-method 属性指定了销毁方法,那么 Spring 会在销毁前置处理完成后调用该方法。 在整个 Bean 生命周期,开发人员可以通过实现 InitializingBean 和 DisposableBean 接口,或者在配置文件指定 init-method 和 destroy-method 方法,来自定义 Bean 的初始化和销毁过程,并在这些过程进行一些特定的操作。 ### 回答3: SpringBean生命周期可以分为以下阶段: 1. 实例化:Spring通过Bean定义创建Bean的实例。这可以通过构造函数实例化,或者通过工厂方法来实现。 2. 属性赋值:SpringBean的属性值注入到Bean的实例。这可以通过依赖注入(DI)方式进行,也可以通过配置文件或注解来实现。 3. 初始化:在Bean实例化和属性赋值之后,Spring调用Bean的初始化方法。这可以通过实现InitializingBean接口的afterPropertiesSet()方法,或者使用@PostConstruct注解来实现。 4. 使用:在初始化完成之后,Bean可以被使用,执行业务逻辑。 5. 销毁:当Bean不再需要时,Spring调用Bean的销毁方法。这可以通过实现DisposableBean接口的destroy()方法,或者使用@PreDestroy注解来实现。 需要注意的是,在Bean的生命周期,可以通过配置文件或注解来控制Bean的创建和销毁方式。 总的来说,SpringBean生命周期包括实例化、属性赋值、初始化、使用和销毁这几个阶段。通过控制Bean的生命周期,我们可以在合适的时机执行一些特定的操作,如初始化资源、释放资源等。这样可以有效地管理Bean的生命周期,提高系统的性能和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值