该接口目前有两个方法:
● postProcessBeforeInitialization 该在初始化方法之前调用。
● postProcessAfterInitialization 该方法再初始化方法之后调用。
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof User) {
System.out.println("BeanPostProcessor.postProcessBeforeInitialization");
((User) bean).setName("bean初始化前修改");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof User) {
System.out.println("BeanPostProcessor.postProcessAfterInitialization");
((User) bean).setName("bean初始化后修改");
}
return bean;
}
}