spring源码解读:BeanPostProcessor接口
来源:互联网 发布:人工智能需要什么专业 编辑:程序博客网 时间:2024/05/29 12:19
在aware接口中曾经提到过其实它是不做啥事情的,要实现回调通知的功能还得BeanPostProcessor来,现在来看一下BeanPostProcessor的源码
package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;/** * Factory hook that allows for custom modification of new bean instances, * e.g. checking for marker interfaces or wrapping them with proxies. * * <p>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. * * <p>Typically, post-processors that populate beans via marker interfaces * or the like will implement {@link #postProcessBeforeInitialization}, * while post-processors that wrap beans with proxies will normally * implement {@link #postProcessAfterInitialization}. * * @author Juergen Hoeller * @since 10.10.2003 * @see InstantiationAwareBeanPostProcessor * @see DestructionAwareBeanPostProcessor * @see ConfigurableBeanFactory#addBeanPostProcessor * @see BeanFactoryPostProcessor */public interface BeanPostProcessor {/** * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code> * or a custom init-method). The bean will already be populated with property values. * The returned bean instance may be a wrapper around the original. * @param bean the new bean instance * @param beanName the name of the bean * @return the bean instance to use, either the original or a wrapped one; if * <code>null</code>, no subsequent BeanPostProcessors will be invoked * @throws org.springframework.beans.BeansException in case of errors * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet //实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务 Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;/** * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code> * or a custom init-method). The bean will already be populated with property values. * The returned bean instance may be a wrapper around the original. * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean * instance and the objects created by the FactoryBean (as of Spring 2.0). The * post-processor can decide whether to apply to either the FactoryBean or created * objects or both through corresponding <code>bean instanceof FactoryBean</code> checks. * <p>This callback will also be invoked after a short-circuiting triggered by a * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method, * in contrast to all other BeanPostProcessor callbacks. * @param bean the new bean instance * @param beanName the name of the bean * @return the bean instance to use, either the original or a wrapped one; if * <code>null</code>, no subsequent BeanPostProcessors will be invoked * @throws org.springframework.beans.BeansException in case of errors * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet * @see org.springframework.beans.factory.FactoryBean *///实例化、依赖注入、初始化完毕时执行 Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;}
其继承的关系
BeanPostProcessor接口作用是:如果我们需要在Spring容器完成Bean的实例化、配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcessor接口的实现,然后注册到容器中。
Spring中Bean的实例化过程图示:
由上图可以看到,Spring中的BeanPostProcessor在实例化过程处于的位置,BeanPostProcessor接口有两个方法需要实现:postProcessBeforeInitialization和postProcessAfterInitialization,
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { public MyBeanPostProcessor() { super(); System.out.println("这是BeanPostProcessor实现类构造器!!"); } @Override public Object postProcessAfterInitialization(Object bean, String arg1) throws BeansException { System.out.println("bean处理器:bean创建之后.."); return bean; } @Override public Object postProcessBeforeInitialization(Object bean, String arg1) throws BeansException { System.out.println("bean处理器:bean创建之前.."); return bean; } }
由方法名字也可以看出,前者在实例化及依赖注入完成后、在任何初始化代码(比如配置文件中的init-method)调用之前调用;后者在初始化代码调用之后调用。
注意:
1、接口中的两个方法都要将传入的bean返回,而不能返回null,如果返回的是null那么我们通过getBean方法将得不到目标2、BeanFactory和ApplicationContext对待bean后置处理器稍有不同。ApplicationContext会自动检测在配置文件中实现了BeanPostProcessor接口的所有bean,并把它们注册为后置处理器,然后在容器创建bean的适当时候调用它,因此部署一个后置处理器同部署其他的bean并没有什么区别。而使用BeanFactory实现的时候,bean 后置处理器必须通过代码显式地去注册,在IoC容器继承体系中的ConfigurableBeanFactory接口中定义了注册方法:
/** * Add a new BeanPostProcessor that will get applied to beans created * by this factory. To be invoked during factory configuration. * <p>Note: Post-processors submitted here will be applied in the order of * registration; any ordering semantics expressed through implementing the * {@link org.springframework.core.Ordered} interface will be ignored. Note * that autodetected post-processors (e.g. as beans in an ApplicationContext) * will always be applied after programmatically registered ones. * @param beanPostProcessor the post-processor to register */ void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);
另外,不要将BeanPostProcessor标记为延迟初始化。因为如果这样做,Spring容器将不会注册它们,自定义逻辑也就无法得到应用。假如你在<beans />元素的定义中使用了'default-lazy-init'属性,请确信你的各个BeanPostProcessor标记为'lazy-init="false"'。
InstantiationAwareBeanPostProcessor
package org.springframework.beans.factory.config; import java.beans.PropertyDescriptor; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException; boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException; PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException; }
其使用方法与上面介绍的BeanPostProcessor接口类似,只时回调时机不同。
其中其子接口BeanFactoryPostProcessor和BeanPostProcessor的用法和区别
主要区别就是: BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能,下面举个例子说明
BEAN类:
package com.springdemo.postProcessor; public class PostProcessorBean { private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
MyBeanPostProcessor类,实现了BeanPostProcessor接口:
package com.springdemo.postProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import com.springdemo.form.LoginForm; public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub //如果是PostProcessorBean则username信息 if(bean instanceof PostProcessorBean) { System.out.println("PostProcessorBean Bean initialized"); PostProcessorBean pb = (PostProcessorBean)bean; System.out.println("username:"+pb.getUsername()); } return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub if(bean instanceof PostProcessorBean) { System.out.println("PostProcessorBean Bean initializing"); PostProcessorBean pb = (PostProcessorBean)bean; System.out.println("username:"+pb.getUsername()); } return bean; } }
MyBeanFactoryPostProcessor实现了BeanFactoryPostProcessor接口:
package com.springdemo.postProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // TODO Auto-generated method stub //BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能 //我们在这里修改postProcessorBean的username注入属性 BeanDefinition bd = beanFactory.getBeanDefinition("postProcessorBean"); MutablePropertyValues pv = bd.getPropertyValues(); if(pv.contains("username")) { pv.addPropertyValue("username", "xiaojun"); } } }
编写测试用例:
package com.springdemo.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.springdemo.factory.ApplicationContextFactory; import com.springdemo.postProcessor.PostProcessorBean; import junit.framework.TestCase; public class BeanPostPorcessorTest extends TestCase { private ApplicationContext context; protected void setUp() throws Exception { super.setUp(); String[] paths = {"classpath*:/spring/applicationContext-*.xml"}; context = new ClassPathXmlApplicationContext(paths); } protected void tearDown() throws Exception { super.tearDown(); } public void testBeanPostProcessor() { } public void testBeanFactoryPostProcessor() { //PostProcessorBean bean =(PostProcessorBean)ServiceLocator.getService("postProcessorBean"); PostProcessorBean bean =(PostProcessorBean)context.getBean("postProcessorBean"); System.out.println("---------------testBeanFactoryPostProcessor----------------"); System.out.println("username:"+bean.getUsername()); System.out.println("password:"+bean.getPassword()); // } }
spring配置文件如下(先不启用MyBeanFactoryPostProcessor):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean class="com.springdemo.postProcessor.MyBeanPostProcessor"></bean> <!--我们先把BeanFactoryPostProcessor注释掉,不启用,然后查看测试输出结果 <bean class="com.springdemo.postProcessor.MyBeanFactoryPostProcessor"></bean> --> <bean id="postProcessorBean" class="com.springdemo.postProcessor.PostProcessorBean" > <property name="username" value="test"></property> <property name="password" value="test"></property> </bean> </beans>
测试输出结果如下:
PostProcessorBean Bean initializing
username:test
PostProcessorBean Bean initialized
username:test
---------------testBeanFactoryPostProcessor----------------
username:test
password:test
然后我们取消注释启用MyBeanFactoryPostProcessor,测试结果如下:
PostProcessorBean Bean initializing
username:xiaojun
PostProcessorBean Bean initialized
username:xiaojun
---------------testBeanFactoryPostProcessor----------------
username:xiaojun
password:test
从结果可以看出:BeanFactoryPostProcessor的回调比BeanPostProcessor要早,因为BeanPostProcess中输出的username已经变成了xiaojun,而不是test.还有就是BeanFactoryPostProcessor确实有能力改变初始化BEAN的内容,您可以试试在MyBeanPostProcess中试一试set一下username看看能不能改变BEAN实例的内容(答案应该是否定的).
- spring源码解读:BeanPostProcessor接口
- Spring中的BeanPostProcessor接口
- Spring之BeanPostProcessor接口
- spring BeanPostProcessor接口
- Spring源码学习--BeanPostProcessor
- spring源码解读:aware接口
- Spring源码解读:Envrionment接口
- Spring源码解读:EventListener接口
- Spring源码解读:ServletContextListener接口
- spring源码解读:BeanFactory接口
- Spring 的 BeanPostProcessor接口实现
- Spring 的 BeanPostProcessor接口实现
- Spring 的 BeanPostProcessor接口实现
- Spring 的 BeanPostProcessor接口实现
- Spring 的 BeanPostProcessor接口实现
- Spring 的 BeanPostProcessor接口实现
- Spring 的 BeanPostProcessor接口实现
- Spring 的 BeanPostProcessor接口实现
- Python 中列表
- Android获取单选与复选框的值
- 如何在一个页面用多个swiper插件
- Centos7安装完毕后无法联网的解决方法
- mybatis mapper.xml中#{} 与${}用法区别
- spring源码解读:BeanPostProcessor接口
- Lxxxxx注册使用及搭建55过程(内含优惠码)
- sqlserver中添加字段,更改字段类型,名称,查看类型
- openstack下创建windows虚机出现do_hivex_close
- Yii 2.0风格加载自定义类或命名空间 [配置使用Yii autoloader]
- TensorFlow-1: 如何识别数字
- Java-反射机制
- linux下生产者消费者问题的实现
- Ajax报错之Unexpected token S in JSON at position