Bean的生命周期
1、实现各种生命周期控制访问的NewCar
package com.beanprocess; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*; /** * Created by gao on 16-3-18. */ public class NewCar implements BeanFactoryAware,BeanNameAware,InitializingBean, DisposableBean{ private String brand; private String color; private int maxSpeed; private BeanFactory beanFactory; private String beanName; public NewCar() { System.out.println("调用NewCar()构造函数"); } public NewCar(String brand, String color, int maxSpeed) { this.brand = brand; this.color = color; this.maxSpeed = maxSpeed; } public String getBrand() { return brand; } public void setBrand(String brand) { System.out.println("调用setBrand()设置属性"); this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public void introduce() { System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:" + maxSpeed); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("调用BeanFactoryAware.setBeanFactory()."); this.beanFactory = beanFactory; } @Override public void setBeanName(String beanName) { System.out.println("调用BeanNameAware.setBeanName()."); this.beanName = beanName; } @Override public void destroy() throws Exception { System.out.println("调用DisposableBean.destroy()."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("调用InitializingBean.afterPropertiesSet()。"); } public void myInit(){ System.out.println("调用init-method所指定的myInit(),将maxSpeed设置为350."); this.maxSpeed = 350; } public void myDestory(){ System.out.println("调用destory-method所指定的myDestory()。"); } }
2、InstantiationAwareBeanPostProcessor实现类
package com.beanprocess; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; import java.beans.PropertyDescriptor; /** * Created by gao on 16-3-18. */ public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { if ("newCar".equals(beanName)) { System.out.println("InstantiationAware BeanPostProcessor.postProcessBeforeInstantiation"); } return null; } public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { if("newCar".equals(beanName)){ System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation"); } return true; } public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if("newCar".equals(beanName)){ System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues"); } return pvs; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }
3、BeanPostProcessor实现类
package com.beanprocess; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * Created by gao on 16-3-18. */ public class MyBeanPostProcessor implements BeanPostProcessor{ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(beanName.equals("newCar")){ NewCar newCar = (NewCar)bean; if(newCar.getMaxSpeed() >= 350){ System.out.println("调用MyBeanPostProcessor.postProcessAfterInitialization(),将maxSpeed调整为350。"); newCar.setMaxSpeed(350); } } return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if(beanName.equals("newCar")){ NewCar newCar = (NewCar)bean; if(newCar.getColor() == null){ System.out.println("调用MyBeanPostProcessor.postProcessBeforeInitialization(),color为空,设置为默认黑色。"); newCar.setColor("黑色"); } } return bean; } }
4、工厂后处理器:MyBeanFactoryPostProcessor.java
package com.beanprocess; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; /** * Created by gao on 16-3-18. */ public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException { BeanDefinition bd = bf.getBeanDefinition("newCar"); bd.getPropertyValues().addPropertyValue("brand", "奇瑞QQ"); System.out.println("调用MyBeanFactoryPostProcessor.postProcessBeanFactory()!"); } }
5、beans.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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="newCar" class="com.beanprocess.NewCar" init-method="myInit" destroy-method="myDestory" p:brand="迈锐宝" p:color="黑色" p:maxSpeed="300" /> <bean id="myBeanPostProcessor" class="com.beanprocess.MyBeanPostProcessor"/> <bean id="myBeanFactoryPostProcessor" class="com.beanprocess.MyBeanFactoryPostProcessor"/> </beans>
5、测试类:BeanLifeCycle
package com.beanprocess; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /** * Created by gao on 16-3-18. */ public class BeanLifeCycle { private static void LifeCycleInBeanFactory(){ Resource res = new ClassPathResource("beans.xml"); BeanFactory bf = new XmlBeanFactory(res); (new MyBeanFactoryPostProcessor()).postProcessBeanFactory((XmlBeanFactory)bf); //InstantiationAwareBeanPostProcessor // ((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor()); //BeanPostProcessor // ((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyBeanPostProcessor()); NewCar newCar1 = (NewCar)bf.getBean("newCar"); newCar1.introduce(); newCar1.setColor("红色"); NewCar newCar2 = bf.getBean("newCar",NewCar.class); newCar2.introduce(); ((XmlBeanFactory)bf).destroySingletons(); } public static void main(String[] args) { LifeCycleInBeanFactory(); } }
输出结果:
2016-03-18 20:10:42,648 INFO [main] (XmlBeanDefinitionReader.java:315) - Loading XML bean definitions from class path resource [beans.xml]
调用MyBeanFactoryPostProcessor.postProcessBeanFactory()!
调用NewCar()构造函数
调用setBrand()设置属性
调用BeanNameAware.setBeanName().
调用BeanFactoryAware.setBeanFactory().
调用InitializingBean.afterPropertiesSet()。
调用init-method所指定的myInit(),将maxSpeed设置为350.
brand:奇瑞QQ;color:黑色;maxSpeed:350
brand:奇瑞QQ;color:红色;maxSpeed:350
2016-03-18 20:10:43,251 INFO [main] (DefaultSingletonBeanRegistry.java:422) - Destroying singletons in org.springframework.beans.factory.xml.XmlBeanFactory@55782201: defining beans [newCar,myBeanPostProcessor,myBeanFactoryPostProcessor]; root of factory hierarchy
调用DisposableBean.destroy().
调用destory-method所指定的myDestory()。