小言_互联网的博客

【Spring】Bean的生命周期详解

340人阅读  评论(0)

1. 什么是Bean的生命周期:

  • Spring其实就是一个管理Bean对象的工厂, 它负责对象的创建, 对象的销毁等

  • 所谓的生命周期就是: 对象从创建开始到最终销毁的整个过程

  • Spring容器只对singleton的Bean进行完整的生命周期管理, 如果是prototype作用域的Bean, Spring容器只负责将该Bean初始化完毕. 等客户端程序一旦获取到该Bean之后, Spring容器就不再管理该对象的生命周期了


   
  1. // 等客户端程序一旦获取到该Bean, 例如:
  2. Person person = ctx.getBean( "person", Person.class);
  • 这俩步(检查Bean是否实现了DisposableBean接口, 并调用接口方法销毁Bean), prototype不管了

2. Bean的生命周期之五步:

  • 第二步: Bean属性赋值(调用set方法)

  • 第三步: 初始化Bean(会调用Bean的init方法, 这个init方法需要自己写自己配)

  • 第四步: 使用Bean

  • 第五步: 销毁Bean(会调用Bean的destroy方法, 这个destroy方法需要自己写自己配)

  • bean标签需要指定init-method destroy-method 属性


   
  1. // 普通的bean
  2. public class Person {
  3. private String name;
  4. public Person () {
  5. System.out.println( "第一步: 无参构造方法执行!");
  6. }
  7. public void setName (String name) {
  8. this.name = name;
  9. System.out.println( "第二步: 给对象的属性赋值!");
  10. }
  11. // 这个方法需要自己写自己配
  12. public void initBean (){
  13. System.out.println( "第三步: 初始化Bean!");
  14. }
  15. // 这个方法需要自己写自己配
  16. public void destroy (){
  17. System.out.println( "第五步: 销毁Bean!");
  18. }
  19. }
  20. // xml
  21. // 需要指定init-method destroy-method 属性
  22. <bean id= "person" class= "com.powernode.xxxx.Person" init-method= "initBean" destroy-method= "destroy">
  23. <property name= "name" value= "大锤"/>
  24. </bean>
  25. // @Test
  26. public void Bean5 (){
  27. ApplicationContext ctx = new
  28. ClassPathXmlApplicationContext( "Bean_life.xml");
  29. Person person = ctx.getBean( "person", Person.class);
  30. System.out.println( "第四步: 使用Bean ." + person);
  31. // 注意需要手动关闭Spring容器, 这样Spring容器才会销毁Bean
  32. ClassPathXmlApplicationContext context =
  33. (ClassPathXmlApplicationContext) ctx;
  34. context.close();
  35. }

3. Bean的生命周期之七步:

  • 在上述的五步中, 第三步是初始化Bean, 如果你还想在初始化前和初始化后添加代码, 可以加入"Bean后处理器"

  • 编写一个类实现BeanPostProcessor接口, 并且重写before和after方法

  • 哪七步? :

  • 第一步: 实例化Bean(调用无参构造方法)

  • 第二步: Bean属性赋值(调用set方法)

  • 第三步: 执行"Bean后处理器"的before方法

  • 第四步: 初始化Bean(会调用Bean的init方法, 这个init方法需要自己写自己配)

  • 第五步: 执行"Bean后处理器"的after方

  • 第六步: 使用Bean

  • 第七步: 销毁Bean(会调用Bean的destroy方法, 这个destroy方法需要自己写自己配)

  • bean标签需要配置Bean后处理器


   
  1. // 实现BeanPostProcessor接口的类
  2. public class LogBeanPostProcessor implements BeanPostProcessor {
  3. // 方法有俩个参数
  4. // 第一个参数: 刚创建的bean对象
  5. // 第二个参数: bean的名字
  6. @Override
  7. public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException {
  8. System.out.println( "执行Bean后处理器的before方法");
  9. return BeanPostProcessor. super.postProcessBeforeInitialization(bean, beanName);
  10. }
  11. @Override
  12. public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {
  13. System.out.println( "执行Bean后处理器的after方法");
  14. return BeanPostProcessor. super.postProcessAfterInitialization(bean, beanName);
  15. }
  16. }
  17. // xml
  18. // 配置Bean后处理器
  19. // 注意: 这个bean后处理器将作用域整个配置文件中所有的Bean
  20. <bean class= "com.powernode.xxxx.LogBeanPostProcessor"/>
  21. <bean id= "person" class= "com.powernode.xxxx.Person" init-method= "initBean" destroy-method= "destroyBean">
  22. <property name= "name" value= "大锤"/>
  23. </bean>
  24. // 其他代码和Bean生命周期五步相同

4. Bean的生命周期之十步:

Bean生命周期的十步: 比七步添加的那三步在哪里?

  • 点位1: 在"Bean后处理器"before方法之前

  • 干了什么事?

  • 检查Bean是否实现了Aware相关的接口, 如果实现了接口则调用这些接口中的方法

  • 然后调用这些方法的目的是为了给你传递一些数据, 让你更加方便使用

  • 点位2: 在"Bean后处理器"before方法之后

  • 干了什么事?

  • 检查Bean是否实现了InitializingBean接口, 如果实现了, 则调用接口中的方法

  • 点位3: 使用Bean之后, 或者说销毁Bean之前

  • 干了什么事?

  • 检查Bean是否实现了DisposableBean接口, 如果实现了, 则调用接口中的方法

  • 添加的这三个点位的特点: 都是在检查你这个Bean是否实现了某些特定的接口, 如果实现了这些接口, 则Spring容器会调用这个接口中的方法

  • 检查是否实现了Aware的相关接口是什么意思?

  • Aware相关的接口包括: BeanNameAware, BeanClassLoaderAware, BeanFactoryAware

  • 当Bean实现了BeanNameAware, Spring会将Bean的名字传递给Bean

  • 当Bean实现了BeanClassLoaderAware, Spring会将加载该Bean的类加载器传递给Bean

  • 当Bean实现了BeanFactoryAware, Spring会将Bean工厂对象传递给Bean


   
  1. // 普通的bean
  2. public class Person implements BeanNameAware, BeanClassLoaderAware,
  3. BeanFactoryAware, InitializingBean,DisposableBean {
  4. private String name;
  5. public Person () {
  6. System.out.println( "第一步: 无参构造方法执行!");
  7. }
  8. public void setName (String name) {
  9. this.name = name;
  10. System.out.println( "第二步: 给对象的属性赋值!");
  11. }
  12. // 这个方法需要自己写自己配
  13. public void initBean (){
  14. System.out.println( "第三步: 初始化Bean!");
  15. }
  16. // 这个方法需要自己写自己配
  17. public void destroyBean (){
  18. System.out.println( "第五步: 销毁Bean!");
  19. }
  20. @Override
  21. public void setBeanClassLoader (ClassLoader classLoader) {
  22. System.out.println( "Bean这个类的加载器: " + classLoader);
  23. }
  24. @Override
  25. public void setBeanFactory (BeanFactory beanFactory) throws BeansException {
  26. System.out.println( "生产这个Bean的工厂对象是: " + beanFactory);
  27. }
  28. @Override
  29. public void setBeanName (String s) {
  30. System.out.println( "这个Bean的名字是: " + s);
  31. }
  32. @Override
  33. public void afterPropertiesSet () throws Exception {
  34. System.out.println( "InitializingBean's afterPropertiesSet 执行!");
  35. }
  36. @Override
  37. public void destroy () throws Exception {
  38. System.out.println( "DisposableBean's destroy 方法执行!");
  39. }
  40. }
  41. // xml
  42. // 配置Bean后处理器
  43. // 注意: 这个bean后处理器将作用域整个配置文件中所有的Bean
  44. <bean class= "com.powernode.xxxx.LogBeanPostProcessor"/>
  45. <bean id= "person" class= "com.powernode.xxxx.Person" init-method= "initBean" destroy-method= "destroyBean">
  46. <property name= "name" value= "大锤"/>
  47. </bean>
  48. // @Test
  49. public void Bean5 (){
  50. ApplicationContext ctx = new
  51. ClassPathXmlApplicationContext( "Bean_life.xml");
  52. Person person = ctx.getBean( "person", Person.class);
  53. System.out.println( "第四步: 使用Bean ." + person);
  54. // 注意需要手动关闭Spring容器, 这样Spring容器才会销毁Bean
  55. ClassPathXmlApplicationContext context =
  56. (ClassPathXmlApplicationContext) ctx;
  57. context.close();
  58. }

5. 自己new的对象纳入Spring容器管理:


   
  1. @Test
  2. public void testRegisterBean (){
  3. // 自己new对象
  4. Student student = new Student();
  5. System.out.println(student); // com.powernode.bean.Student@1b9e1916
  6. // 将以上自己new的这个对象纳入到Spring容器来管理, 半路上交给Spring来管理
  7. DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
  8. factory.registerSingleton( "studentBean",student);
  9. // 从Spring中获取
  10. Object studentBean = factory.getBean( "studentBean");
  11. System.out.println(studentBean); // com.powernode.bean.Student@1b9e1916
  12. }

转载:https://blog.csdn.net/qq_68993495/article/details/128926664
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场