实验11:测试bean的后置处理器:
- Spring有一个接口,后置处理器,可以在bean的初始化前后调用方法。
<bean id="beanPostProcessor" class="com.atguigu.bean.MyBeanPostProcessor"></bean>
写后置处理器的实现类:
package com.atguigu.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* 1.编写后置处理器的实现类
* 2.将后置处理器注册在配置文件中
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName+"正在初始化之前调用"+bean);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName+"在初始化之后调用"+bean);
return bean;
}
}