public class Student implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean,ApplicationContextAware { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("BeanFactoryAware......"); } @Override public void setBeanName(String s) { System.out.println("BeanNameAware......"); } @Override public void destroy() throws Exception { System.out.println("DisposableBean......"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean......"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("ApplicationContextAware......"); } }
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("student"); MutablePropertyValues propertyValues = bd.getPropertyValues(); //配置文件中的信息在加载到Spring中后以BeanDefinition的形式存在.在这里又可以更改BeanDefinition,所以可以理解为更改配置文件里面的内容 // propertyValues.add("zdy","123"); } }
public class MyBeanPostProcessor implements BeanPostProcessor { public MyBeanPostProcessor() { super(); System.out.println("这是BeanPostProcessor实现类构造器!!"); } @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out.println("BeanPostProcessor接口方法After对属性进行更改!"); return arg0; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out.println("BeanPostProcessor接口方法Before对属性进行更改!"); return arg0; } }
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调用Before方法"); return null; } // 接口方法、实例化Bean之后调用 @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("InstantiationAwareBeanPostProcessor调用Ater方法"); return bean; } // 接口方法、设置某个属性时调用 @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法"); return pvs; } }
public class App { public static void main( String[] args ) { ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu = (Student) ac.getBean("student"); stu.setName("wangwu"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans <bean id="student" class="com.zdy.Student"> <constructor-arg value="zhangsan"/> </bean> <bean id="myBeanFactoryPostProcessor" class="com.zdy.MyBeanFactoryPostProcessor"></bean> <bean id="myInstantiationAwareBeanPostProcessor" class="com.zdy.MyInstantiationAwareBeanPostProcessor"></bean> <bean id="myBeanPostProcessor" class="com.zdy.MyBeanPostProcessor"></bean> </beans>
这是BeanFactoryPostProcessor实现类构造器!! BeanFactoryPostProcessor调用postProcessBeanFactory方法 这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!! 这是BeanPostProcessor实现类构造器!! InstantiationAwareBeanPostProcessor调用Before方法 InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法 BeanNameAware...... BeanFactoryAware...... ApplicationContextAware...... BeanPostProcessor接口方法Before对属性进行更改! InitializingBean...... InstantiationAwareBeanPostProcessor调用Ater方法 BeanPostProcessor接口方法After对属性进行更改!