LifeCycle类:
package com.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class LifeCycle implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
public LifeCycle() {
System.out.println("Constructor executing");
}
private String cycle;
public void setCycle(String cycle) {
System.out.println("Setter methods executing");
this.cycle = cycle;
}
@Override
public void setBeanName(String beanName) {
System.out.println("BeanNameAware.setBeanName(String beanName) executing");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware.setBeanFactory(BeanFactory beanFactory) executing");
}
@PostConstruct
public void beforeBeanConstructed() {
System.out.println("BeanPostProcessor executing (before bean constructed)");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean.afterPropertiesSet() executing");
}
public void initMethod() {
System.out.println("InitMethod executing");
}
@PreDestroy
public void afterBeanConstructed() {
System.out.println("BeanPostProcessor executing (after bean constructed)");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy() executing");
}
public void destroyMethod() {
System.out.println("DestroyMethod executing");
}
}
配置文件beans-config.xml:
<?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-3.0.xsd">
<bean id="lifeCycle" class="com.bean.LifeCycle" init-method="initMethod" destroy-method="destroyMethod">
<property name="cycle" value="Life Cycle"/>
</bean>
<bean id="beanPostProcessor" class="org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor">
<property name="initAnnotationType" value="javax.annotation.PostConstruct"/>
<property name="destroyAnnotationType" value="javax.annotation.PreDestroy"/>
</bean>
</beans>
测试类:
package com.action;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.bean.LifeCycle;
public class LifeCycleDemo {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans-config.xml"));
BeanPostProcessor bpp = (BeanPostProcessor)factory.getBean("beanPostProcessor");
factory.addBeanPostProcessor(bpp);
LifeCycle lifeCycle = (LifeCycle)factory.getBean("lifeCycle");
factory.destroySingleton("lifeCycle");
}
}
/**Output:
Constructor executing
Setter methods executing
BeanNameAware.setBeanName(String beanName) executing
BeanFactoryAware.setBeanFactory(BeanFactory beanFactory) executing
BeanPostProcessor executing (before bean constructed)
InitializingBean.afterPropertiesSet() executing
InitMethod executing
BeanPostProcessor executing (after bean constructed)
DisposableBean.destroy() executing
DestroyMethod executing
*/
//解释:
在传统的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实现了InitializingBean接口,afterPropertiesSet()方法会被调用;
如果Bean指定了init-method属性,init-method属性指定的方法将被调用。
如果InitializingBean和init-method属性同时出现,前者先于后者执行。
7.如果有BeanPsotProcessor和Bean关联,那么它们的postProcessAfterInitialization()方法将被调用。
到这个时候,Bean已经可以被应用系统使用了,并且将被保留在Bean Factory中直到它不再需要。
8.最后当关闭应用时,Bean Factory会调用destroySingletons()方法,这时,会执行:
如果Bean实现了DisposableBean接口,destory()方法会被调用;
如果指定了destroy-method属性,destroy-method属性指定的方法将被调用。
如果DisposableBean和destroy-method属性同时出现,前者先于后者执行。
Bean在Spring应用上下文的生命周期与在Bean工厂中的生命周期只有一点不同,
唯一不同的是,如果Bean实现了ApplicationContextAwre接口,setApplicationContext()方法被调用。
只有singleton行为的bean接受容器管理生命周期。
non-singleton行为的bean,Spring容器仅仅是new的替代,容器只负责创建。
分享到:
评论