本文共 5807 字,大约阅读时间需要 19 分钟。
package exa.ydoing.spring;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.config.ConfigurableBeanFactory;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;public class App { public static void main(String[] args) { //实例化BeanFactory Resource res = new ClassPathResource("ApplicationContext.xml"); BeanFactory bf = new XmlBeanFactory(res); //注册InstantiationAwareBeanPostProcessor后置处理器,因为InstantiationAwareBeanPostProcessor继承了BeanPostProcessor, //所以不再单独注册BeanPostProcessor后置处理器 ((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor()); //获得bean MyBean bean = bf.getBean("myBean", MyBean.class); bean.printName(); //销毁所有Bean ((XmlBeanFactory)bf).destroySingletons(); }}
InstantiationAwareBeanPostProcessor 的实现:
package exa.ydoing.spring;import java.beans.PropertyDescriptor;import org.springframework.beans.BeansException;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用BeanPostProcessor接口的postProcessBeforeInitialization方法"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用BeanPostProcessor接口的postProcessAfterInitialization方法"); return bean; } @Override public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法"); return null; } @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法"); return true; } @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法"); return pvs; }}
MyBean的代码:
package exa.ydoing.spring;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 MyBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean{ private BeanFactory beanFactory; private String name; private String tag; public MyBean(){ System.out.println("调用构造器实例化Bean"); } public void printName(){ System.out.println("MyBean name is: " + name); } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; System.out.println("设置Bean的属性为:" + tag); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("调用DisposableBean接口的destroy方法"); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("调用InitializingBean接口的afterPropertiesSet方法"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { // TODO Auto-generated method stub this.beanFactory = beanFactory; System.out.println("调用BeanFactoryAware接口的setBeanFactory方法"); } @Override public void setBeanName(String name) { // TODO Auto-generated method stub this.name = name; System.out.println("调用BeanNameAware接口的setBeanName方法"); }}
Bean的配置很简单:
输出:
调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法调用构造器实例化Bean调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法设置Bean的属性为:test调用BeanNameAware接口的setBeanName方法调用BeanFactoryAware接口的setBeanFactory方法调用BeanPostProcessor接口的postProcessBeforeInitialization方法调用InitializingBean接口的afterPropertiesSet方法调用BeanPostProcessor接口的postProcessAfterInitialization方法MyBean name is: myBean调用DisposableBean接口的destroy方法
转载地址:http://fxhrb.baihongyu.com/