在传统的Java应用中,Bean的生命周期很是简单。Java的关键词new用来实例化Bean(或许他是非序列化的)。这样就够用了。相反,Bean 的生命周期在Spring容器中更加细致。理解Spring Bean的生命周期很是重要,由于你或许要利用Spring提供的机会来订制Bean的建立过程。
1. 容器寻找Bean的定义信息而且将其实例化。
2.受用依赖注入,Spring按照Bean定义信息配置Bean的全部属性。
3.若是Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。
4.若是Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。
5.若是BeanPostProcessor和Bean关联,那么它们的postProcessBeforeInitialzation()方法将被调用。
6.若是Bean指定了init-method方法,它将被调用。
7.最后,若是有BeanPsotProcessor和Bean关联,那么它们的postProcessAfterInitialization()方法将被调用。
到这个时候,Bean已经能够被应用系统使用了,而且将被保留在Bean Factory中知道它再也不须要。有两种方法能够把它从Bean Factory中删除掉。
1.若是Bean实现了DisposableBean接口,destory()方法被调用。
2.若是指定了订制的销毁方法,就调用这个方法。
Bean在Spring应用上下文的生命周期与在Bean工厂中的生命周期只有一点不一样,惟一不一样的是,若是Bean实现了ApplicationContextAwre接口,setApplicationContext()方法被调用。
举例:(这里只是演示Bean工厂的例子)
我这里用的是spring-1.2.6,因为版本缘由吧,这里演示的不是很好!
1.HelloWorld.java
java
package com.spring.lifecycle; app
import org.springframework.beans.BeansException; post
import org.springframework.beans.factory.BeanFactory; 测试
import org.springframework.beans.factory.BeanFactoryAware; this
import org.springframework.beans.factory.BeanNameAware; spa
import org.springframework.beans.factory.DisposableBean; xml
import org.springframework.beans.factory.InitializingBean; 接口
import org.springframework.beans.factory.config.BeanPostProcessor; 生命周期
public class HelloWorld implements BeanNameAware,BeanFactoryAware, BeanPostProcessor,InitializingBean,DisposableBean{
private String hello;
public void setBeanName(String arg0) {
System.out.println("调用BeanNameAware的setBeanName()..." );
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
System.out.println("调用setHello()..." );
}
public void customInit() {
System.out.println("调用customInit()...");
}
public void customDestroy() {
System.out.println("调用customDestroy()...");
}
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("调用BeanPostProcessor的postProcessAfterInitialization()...");
return null;
}
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("调用BeanPostProcessor的postProcessBeforeInitialization()...");
return null;
}
public void destroy() throws Exception {
System.out.println("调用DisposableBean的destory()...");
}
public void afterPropertiesSet() throws Exception {
System.out.println("调用InitializingBean的afterPropertiesSet()...");
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("调用BeanFactoryAware的setBeanFactory()...");
}
}
2.测试程序
package com.spring.springtest;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.lifecycle.HelloWorld;
public class TestLifeCycle extends TestCase {
private BeanFactory bf;
protected void setUp() throws Exception {
bf = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void testLifeCycle() throws Exception {
HelloWorld hello = (HelloWorld) bf.getBean("helloWorld");
assertEquals("hello world!", hello.getHello());
hello.destroy();
}
}
3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloWorld" class="com.spring.lifecycle.HelloWorld"
init-method="customInit" destroy-method="customDestroy">
<property name="hello" value="hello world!"></property>
</bean>
</beans>
4.运行结果:
引用
调用setHello()... 调用BeanNameAware的setBeanName()... 调用BeanFactoryAware的setBeanFactory()... 调用InitializingBean的afterPropertiesSet()... 调用customInit()... 调用DisposableBean的destory()...