Spring IOC -bean对象的生命周期详解

生命周期执行的过程如下:
1) spring对bean进行实例化,默认bean是单例
2) spring对bean进行依赖注入
3) 如果bean实现了BeanNameAware接口,spring将bean的id传给setBeanName()方法
4) 如果bean实现了BeanFactoryAware接口,spring将调用setBeanFactory方法,将BeanFactory实例传进来
5) 如果bean实现了ApplicationContextAware()接口,spring将调用setApplicationContext()方法将应用上下文的引用传入
6) 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessBeforeInitialization接口方法
7) 如果bean实现了InitializingBean接口,spring将调用它们的afterPropertiesSet接口方法,类似的如果bean使用了init-method属性声明了初始化方法,改方法也会被调用
8) 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessAfterInitialization接口方法
9) 此时bean已经准备就绪,可以被应用程序使用了,他们将一直驻留在应用上下文中,直到该应用上下文被销毁
10) 若bean实现了DisposableBean接口,spring将调用它的distroy()接口方法。同样的,如果bean使用了destroy-method属性声明了销毁方法,则该方法被调用

 

  上面流程图当中涉及调用很多的方法,可能我们直接去理解和记忆比较困难,其实对于这么一大堆方法我们可以根据它们的特点对他们进行整理分类,下面提供一种可供大家参考的分类模型:

分类类型所包含方法
Bean自身的方法 配置文件中的init-method和destroy-method配置的方法、Bean对象自己调用的方法
Bean级生命周期接口方法 BeanNameAware、BeanFactoryAware、InitializingBean、DiposableBean等接口中的方法
容器级生命周期接口方法 InstantiationAwareBeanPostProcessor、BeanPostProcessor等后置处理器实现类中重写的方法

  这时回过头再来看这些方法轮廓上就比较清晰了,记忆的时候我们可通过记忆分类的类型来理解性的记忆所涉及的方法。

  其实很多时候我们并不会真的去实现上面说描述的那些接口,那么下面我们就除去那些接口针对bean的单例和非单例来描述下bean的生命周期:

 

一,单例管理的对象:

1.默认情况下,spring在读取xml文件的时候,就会创建对象。
2.在创建的对象的时候(先调用构造器),会去调用init-method=".."属性值中所指定的方法。

3.对象在被销毁的时候,会调用destroy-method="..."属性值中所指定的方法。(例如调用container.destroy()方法的时候)

4.lazy-init="true",可以让这个对象在第一次被访问的时候创建。

代码编写如下:

1.先写Bean类-LifeBean类:

 1 public class LifeBean{  
 2     private String name;  
 3     private LifeBean lb;  
 4       
 5     public LifeBean getLb() {  
 6         return lb;  
 7     }  
 8     public void setLb(LifeBean lb) {  
 9         this.lb = lb;  
10     }  
11     public LifeBean(){  
12         System.out.println("LifeBean() 构造器");  
13     }  
14     public String getName() {  
15         return name;  
16     }  
17   
18     public void setName(String name) {  
19         System.out.println("setName() 方法");  
20         this.name = name;  
21     }  
22       
23     public void init(){  
24         System.out.println("this is init in lifeBean");  
25     }  
26       
27     public void destory(){  
28         System.out.println("this is destory in lifeBean");  
29     }  
30       
31 }
 



 

2.写配置文件life.xnl:(注:配置文件时随便命名的,只要是xml文件就行~)

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 6            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
 7            http://www.springframework.org/schema/context  
 8            http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
 9     <bean name="life" class="com.x.spring.bean.LifeBean"  init-method="init" destroy-method="destory"></bean>  
10 </beans>

3.编写测试类LifeTest:

 1 public class LifeTest {  
 2   
 3     public static void main(String[] args) {  
 4         String path = "life.xml";  
 5         ClassPathXmlApplicationContext container =   
 6                 new ClassPathXmlApplicationContext(path);  
 7           
 8         LifeBean l1 = (LifeBean) container.getBean("life");  
 9           
10         //container.destroy();  
11     }  
12 } 
 



看看注释掉了这一行的效果图:

在看看没注释掉的效果图:

这个就算写完了~

二,非单例管理的对象:

1.spring读取xml文件的时候,不会创建对象.
2.在每一次访问这个对象的时候,spring容器都会创建这个对象,并且调用init-method=".."属性值中所指定的方法.
3.对象销毁的时候,spring容器不会帮我们调用任何方法,因为是非单例,这个类型的对象有很多个,spring容器一旦把这个对象交给你之后,就不再管理这个对象了.

代码编写:

只需要改一点配置文件的代码:

<?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-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
    <bean name="life" class="com.x.spring.bean.LifeBean" scope="prototype"   init-method="init" destroy-method="destory"></bean>  
</beans>

效果图如下:

好了,这个生命周期到这里就写完了~

 

本文转自:https://blog.csdn.net/qq_33642117/article/details/51924653

posted @ 2018-04-09 20:48  Margaery  阅读(15243)  评论(0编辑  收藏  举报