Spring之BeanPostProcessor接口
2015-07-30 13:57
681 查看
BeanPostProcessor接口
public interface BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }
官方解释:
Factory hook that allows for custom modification of new bean instances, e.g. checking for marker interfaces or wrapping them with proxies. ApplicationContexts can autodetect BeanPostProcessor beans in their bean definitions and apply them to any beans subsequently created. Plain bean factories allow for programmatic registration of post-processors, applying to all beans created through this factory. Typically, post-processors that populate beans via marker interfaces or the like will implement postProcessBeforeInitialization(java.lang.Object, java.lang.String), while post-processors that wrap beans with proxies will normally implementpostProcessAfterInitialization(java.lang.Object, java.lang.String).
如果这个接口的某个实现类被注册到某个容器,那么该容器的每个受管Bean在调用初始化方法之前,都会获得该接口实现类的一个回调。容器调用接口定义的方法时会将该受管Bean的实例和名字通过参数传入方法,进过处理后通过方法的返回值返回给容器。
public class ExampleBeanPostProcessor implements InitializingBean{ public void initMethod(){ System.out.println("ExampleBeanPostProcessor init-method initMethod"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("ExampleBeanPostProcessor InitializingBean afterPropertiesSet"); } }
public class BeanPostProcessorImpl implements BeanPostProcessor{ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean "+beanName+" init 后 预处理"); return bean; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean "+ beanName+" init 前 预处理"); return bean; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" > <bean class="com.xiang.bean.BeanPostProcessorImpl"></bean> <bean id="exampleBeanPostProcessor" class="com.xiang.bean.ExampleBeanPostProcessor" init-method="initMethod"></bean> </beans>
Bean exampleBeanPostProcessor init 前 预处理 ExampleBeanPostProcessor InitializingBean afterPropertiesSet ExampleBeanPostProcessor init-method initMethod Bean exampleBeanPostProcessor init 后 预处理
BeanPostProcessor的作用域是容器级的,它只和所在容器有关。如果你在容器中定义了BeanPostProcessor,它仅仅对此容器中的bean进行后置。它不会对定义在另一个容器中的bean进行任何处理。BeanPostProcessor是在spring容器加载了bean的定义文件并且实例化bean之后执行的。
BeanFactory和ApplicationContext容器的注册方式不大一样:若使用BeanFactory,则必须要显示的调用其addBeanPostProcessor()方法进行注册,参数为BeanPostProcessor实现类的实例;如果是使用ApplicationContext,那么容器会在配置文件在中自动寻找实现了BeanPostProcessor接口的Bean,然后自动注册,我们要做的只是配置一个BeanPostProcessor实现类的Bean就可以了。
BeanPostPrcessorImpl beanPostProcessor = new BeanPostPrcessorImpl(); Resource resource = new FileSystemResource("applicationContext.xml"); ConfigurableBeanFactory factory = new XmlBeanFactory(resource); factory.addBeanPostProcessor(beanPostProcessor);
假如我们使用了多个的BeanPostProcessor的实现类,那么如何确定处理顺序呢?其实只要实现Ordered接口,设置order属性就可以很轻松的确定不同实现类的处理顺序了。
public class BeanPostProcessorImplFirst implements BeanPostProcessor,Ordered{ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean "+beanName+" init 前 预处理 First"); return bean; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean "+beanName+" init 后 预处理 First"); return bean; } @Override public int getOrder() { return 0; } }
public class BeanPostProcessorImplSecond implements BeanPostProcessor,Ordered{ @Override public int getOrder() { return 1; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean "+beanName+" init 前 预处理 Second"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("Bean "+beanName+" init 后 预处理 Second"); return bean; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" > <bean class="com.xiang.bean.BeanPostProcessorImpl"></bean> <bean class="com.xiang.bean.BeanPostProcessorImplFirst"></bean> <bean class="com.xiang.bean.BeanPostProcessorImplSecond"></bean> <bean id="exampleBeanPostProcessor" class="com.xiang.bean.ExampleBeanPostProcessor" init-method="initMethod"></bean> </beans>
Bean exampleBeanPostProcessor init 后 预处理 First Bean exampleBeanPostProcessor init 前 预处理 Second Bean exampleBeanPostProcessor init 前 预处理 ExampleBeanPostProcessor InitializingBean afterPropertiesSet ExampleBeanPostProcessor init-method initMethod Bean exampleBeanPostProcessor init 前 预处理 First Bean exampleBeanPostProcessor init 后 预处理 Second Bean exampleBeanPostProcessor init 后 预处理
优先级高的先执行,有优先级的比没有优先级的先执行
BeanPostProcessor的前置和后置处理的方法中都要返回该bean,不能是null
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- 实例讲解Java的Spring框架中的AOP实现
- 解析Java中如何获取Spring中配置的bean
- Spring的注解配置与XML配置之间的比较
- java Spring整合Freemarker的详细步骤
- Java 二维码,QR码,J4L-QRCode 的资料整理
- java当中的定时器的4种使用方式
- java中 spring 定时任务 实现代码