MyBeanPostProcessor (实现容器级别后置处理器)
public class MyBeanPostProcessor implements BeanPostProcessor { public MyBeanPostProcessor() { super(); System.out.println("这是BeanPostProcessor实现类构造器!!"); // TODO Auto-generated constructor stub } @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out .println("BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!"); return arg0; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out .println("BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!"); return arg0; } }MyInstantiationAwareBeanPostProcessor(实现容器级别后置处理器)
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { public MyInstantiationAwareBeanPostProcessor() { super(); System.out .println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!"); } // 接口方法、实例化Bean之前调用 @Override public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { System.out .println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法"); return null; } // 接口方法、实例化Bean之后调用 @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out .println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法"); return bean; } // 接口方法、设置某个属性时调用 @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { System.out .println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法"); return pvs; } }MyBeanFactoryPostProcessor (实现容器级别后置处理器)
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public MyBeanFactoryPostProcessor() { super(); System.out.println("这是BeanFactoryPostProcessor实现类构造器!!"); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException { System.out .println("BeanFactoryPostProcessor调用postProcessBeanFactory方法"); BeanDefinition bd = arg0.getBeanDefinition("person"); bd.getPropertyValues().addPropertyValue("phone", "110"); } }配置Spring的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=" "> <bean> </bean> <bean> </bean> <bean> </bean> <bean init-method="myInit" destroy-method="myDestory" scope="singleton" p:name="张三" p:address="广州" p:phone="15900000000"/> </beans>Main方法执行测试
public class Main { public static void main(String[] args) { System.out.println("现在开始初始化容器"); ApplicationContext factory = new ClassPathXmlApplicationContext("bens.xml"); System.out.println("容器初始化成功"); //得到Preson,并使用 Person person = factory.getBean("person",Person.class); System.out.println(person); System.out.println("现在开始关闭容器!"); ((ClassPathXmlApplicationContext)factory).registerShutdownHook(); } }测试结果(我对Main方法执行结果做了格式化,方便参照)
LOGGER测试:现在开始初始化容器 【工厂构造】这是BeanFactoryPostProcessor实现类构造器!! 【工厂方法】BeanFactoryPostProcessor调用postProcessBeanFactory方法 【容器构造】这是BeanPostProcessor实现类构造器!! 【容器构造】这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!! 【容器方法】InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法 【类构造】调用Person的构造器实例化 【容器方法】InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法 【类注入属性】注入属性address 【类注入属性】注入属性name 【类注入属性】注入属性phone 【Bean方法:BeanNameAware接口】调用BeanNameAware.setBeanName() 【Bean方法:BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory() 【容器方法】BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改! 【Bean方法:InitializingBean接口】调用InitializingBean.afterPropertiesSet() 【自身方法:init-method】调用<bean>的init-method属性指定的初始化方法 【容器方法】BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改! 【容器方法】InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法 LOGGER测试:容器初始化成功 Person [address=广州, name=张三, phone=110] LOGGER测试:现在开始关闭容器! 【Bean级别:DiposibleBean接口】调用DiposibleBean.destory() 【自身方法:destroy-method】调用<bean>的destroy-method属性指定的初始化方法将Demo代码的执行结果与上述表中的执行时机进行对比,看看执行时机是否正确,
小结理解透彻Spring Bean的生命周期对开发中可以解决比较棘手的问题,对于深入学习Spring Framework框架这是必须要掌握的知识,所以可以多看两遍。