spring的bean初始化前、中、后想执行一些代码怎么办?

spring的bean初始化前、中、后想做一些事情,怎么办?

解决方法:

1、bean初始化前、后执行配置,创建MyPostProcessor类,实现BeanPostProcessor接口,实现接口中的postProcessBeforeInitialization(bean初始化前执行的方法),

postProcessAfterInitialization(bean初始化后执行的方法)

public class MyPostProcessor implements BeanPostProcessor {  

   @Override  

   public Object postProcessBeforeInitialization(Object bean, String beanName)

           throws BeansException {  

       System.out.println("对象" + beanName + "开始初始化");  

       return bean;  

   }  

   @Override  

   public Object postProcessAfterInitialization(Object bean, String beanName)  

           throws BeansException {  

       System.out.println("对象" + beanName + "初试化完成");  

       return bean;  

   }  

}  

2.http://www.yayihouse.com/yayishuwu/chapter/2010

猜你喜欢

转载自blog.csdn.net/qq_30908729/article/details/94614359