Spring Bean生命周期

1.Bean的建立:BeanFactory容器寻找Bean的定义信息,读取Bean定义文件,并将其实例化,生成各个Bean实例。
2.属性注入:使用依赖注入,Spring按照Bean定义信息配置Bean所有属性。
3.BeanNameAware的setBeanName():传递Bean的ID。
4.BeanFactoryAware的setBeanFactory():工厂调用setBeanFactory()方法传入工厂自身。
如果是使用ApplicationContext来生成并管理Bean的话则稍有不同,使用ApplicationContext来生成及管理Bean实例的话,在执行BeanFactoryAware的setBeanFactory()阶段【4】后,若Bean类上有实现org.springframework.context.ApplicationContextAware接口,则执行其setApplicationContext()方法,接着才执行BeanPostProcessors的ProcessBeforeInitialization()及之后的流程 

5.BeanPostProcessors的ProcessBeforeInitialization() -> 预初始化:
        如果任何的org.springframework.beans.factory.config.BeanPostProcessors实例与Bean实例相关。则执行BeanPostProcessors实例的processBeforeInitialization()方法。
6.initializingBean的afterPropertiesSet():
        如果Bean类已实现org.springframework.beans.factory.InitializingBean接口,则执行他的afterProPertiesSet()方法。
7.Bean定义文件中定义init-method:
        可以在Bean定义文件中使用"init-method"属性设定方法名称
        例如:<bean calss="onlyfun.caterpillar.HelloBean" init-method="initBean">
8.BeanPostProcessors的ProcessAfterInitialization() -> 后初始化
        如果有任何的BeanPostProcessors实例与Bean实例关联,则执行BeanPostProcessors实例的ProcessAfterInitialization()方法。 
        此时,Bean已经可以被应用系统使用,并且将保留在BeanFactory中知道它不在被使用。有两种方法可以将其从BeanFactory中删除掉
9.DisposableBean的destroy()
        在容器关闭时,如果Bean类有实现org.springframework.beans.factory.DisposableBean接口,则执行他的destroy()方法。
10.Bean定义文件中定义destroy-method





利用Spring提供的机会来订制Bean的创建过程

只有singleton行为的bean接受容器管理生命周期。 
non-singleton行为的bean,Spring容器仅仅是new的替代,容器只负责创建。

Spring可以管理实例化结束之后,和销毁之前的行为,管理bean的生命周期行为主要未如下两个时机: 
        Bean全部依赖注入之后 
        Bean即将销毁之前

1)依赖关系注入后的行为实现: 
有两种方法:A.编写init方法  B.实现InitializingBean接口 
afterPropertiesSet和init同时出现,前者先于后者执行,使用init方法,需要对配置文件加入init-method属性

2)bean销毁之前的行为 
有两种方法:A.编写close方法  B.实现DisposableBean接口 
destroy和close同时出现,前者先于后者执行,使用close方法,需要对配置文件加入destroy-method属性
*  请介绍一下spring的bean的生命周期
一、Bean的定义
Spring通常通过配置文件定义Bean。如:
<?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-2.0.xsd”>
<bean id=”HelloWorld” class=”com.pqf.beans.HelloWorld”>
<property name=”msg”>
<value>HelloWorld</value>
</property>
</bean>
</beans>
这个配置文件就定义了一个标识为 HelloWorld 的Bean。在一个配置文档中可以定义多个Bean。
二、Bean的初始化
有两种方式初始化Bean。
1、在配置文档中通过指定init-method 属性来完成
在Bean的类中实现一个初始化Bean属性的方法,如init(),如:
public class HelloWorld{
public String msg=null;
public Date date=null;
public void init() {
msg=”HelloWorld”;
date=new Date();
}
……
}
然后,在配置文件中设置init-mothod属性:
<bean id=”HelloWorld” class=”com.pqf.beans.HelloWorld” init-mothod=”init” >
</bean>
2、实现 org.springframwork.beans.factory.InitializingBean接口
Bean实现InitializingBean接口,并且增加 afterPropertiesSet() 方法:
public class HelloWorld implement InitializingBean {
public String msg=null;
public Date date=null;
public void afterPropertiesSet() {
msg=”向全世界问好!”;
date=new Date();
}
……
}
那么,当这个Bean的所有属性被Spring的BeanFactory设置完后,会自动调用afterPropertiesSet()方法对Bean进行初始化,于是,配置文件就不用指定 init-method属性了。
三、Bean的调用
有三种方式可以得到Bean并进行调用:
1、使用BeanWrapper
HelloWorld hw=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(hw);
bw.setPropertyvalue(”msg”,”HelloWorld”);
system.out.println(bw.getPropertyCalue(”msg”));
2、使用BeanFactory
InputStream is=new FileInputStream(”config.xml”);
XmlBeanFactory factory=new XmlBeanFactory(is);
HelloWorld hw=(HelloWorld) factory.getBean(”HelloWorld”);
system.out.println(hw.getMsg());
3、使用ApplicationConttext
ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”);
HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”);
System.out.println(hw.getMsg());
四、Bean的销毁
1、使用配置文件中的 destory-method 属性
与初始化属性 init-methods类似,在Bean的类中实现一个撤销Bean的方法,然后在配置文件中通过 destory-method指定,那么当bean销毁时,Spring将自动调用指定的销毁方法。
2、实现 org.springframwork.bean.factory.DisposebleBean接口
如果实现了DisposebleBean接口,那么Spring将自动调用bean中的Destory方法进行销毁,所以,Bean中必须提供Destory方法。

*  Spring中如何获取bean
通过xml配置文件
bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext.
第一种方式:FileSystemXmlApplicationContext
通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:
ApplicationContext ac = new FileSystemXmlApplicationContext(”applicationContext.xml”)
ac.getBean(”beanName”);
第二种方式:WebApplicationContextUtil
在B/S系统中,通常在web.xml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext.例如:
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ctx =   WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext();
另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

原文地址:https://www.cnblogs.com/lsx1993/p/4631506.html