简单说说Spring框架中Bean的生命周期

8次阅读

共计 2978 个字符,预计需要花费 8 分钟才能阅读完成。

首先简单说一下(以下为一个回答的参考模板)

1、实例化一个 Bean--也就是我们常说的 new;

2、按照 Spring 上下文对实例化的 Bean 进行配置--也就是 IOC 注入;

3、如果这个 Bean 已经实现了 BeanNameAware 接口,会调用它实现的 setBeanName(String) 方法,此处传递的就是 Spring 配置文件中 Bean 的 id 值

4、如果这个 Bean 已经实现了 BeanFactoryAware 接口,会调用它实现的 setBeanFactory(setBeanFactory(BeanFactory) 传递的是 Spring 工厂自身(可以用这个方式来获取其它 Bean,只需在 Spring 配置文件中配置一个普通的 Bean 就可以);

5、如果这个 Bean 已经实现了 ApplicationContextAware 接口,会调用 setApplicationContext(ApplicationContext) 方法,传入 Spring 上下文(同样这个方式也可以实现步骤 4 的内容,但比 4 更好,因为 ApplicationContext 是 BeanFactory 的子接口,有更多的实现方法);

6、如果这个 Bean 关联了 BeanPostProcessor 接口,将会调用 postProcessBeforeInitialization(Object obj, String s) 方法,BeanPostProcessor 经常被用作是 Bean 内容的更改,并且由于这个是在 Bean 初始化结束时调用那个的方法,也可以被应用于内存或缓存技术;

7、如果 Bean 在 Spring 配置文件中配置了 init-method 属性会自动调用其配置的初始化方法。

8、如果这个 Bean 关联了 BeanPostProcessor 接口,将会调用 postProcessAfterInitialization(Object obj, String s) 方法、;

注:以上工作完成以后就可以应用这个 Bean 了,那这个 Bean 是一个 Singleton 的,所以一般情况下我们调用同一个 id 的 Bean 会是在内容地址相同的实例,当然在 Spring 配置文件中也可以配置非 Singleton,这里我们不做赘述。

9、当 Bean 不再需要时,会经过清理阶段,如果 Bean 实现了 DisposableBean 这个接口,会调用那个其实现的 destroy() 方法;

10、最后,如果这个 Bean 的 Spring 配置中配置了 destroy-method 属性,会自动调用其配置的销毁方法。

结合代码理解一下

1、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\>  

这个配置文件就定义了一个标识为 HelloWorld 的 Bean。在一个配置文档中可以定义多个 Bean。

2、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 属性:

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 属性了。

3、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());

4、Bean 的销毁

1、使用配置文件中的 destory-method 属性

与初始化属性 init-methods 类似,在 Bean 的类中实现一个撤销 Bean 的方法,然后在配置文件中通过 destory-method 指定,那么当 bean 销毁时,Spring 将自动调用指定的销毁方法。

2、实现 org.springframwork.bean.factory.DisposebleBean 接口

如果实现了 DisposebleBean 接口,那么 Spring 将自动调用 bean 中的 Destory 方法进行销毁,所以,Bean 中必须提供 Destory 方法。

图解

正文完
 0