spring 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别

 

主要区别就是: BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能,下面举个例子说明

BEAN类:

  1. package com.springdemo.postProcessor;  
  2.   
  3. public class PostProcessorBean {  
  4.     private String username;  
  5.       
  6.     private String password;  
  7.   
  8.     public String getPassword() {  
  9.         return password;  
  10.     }  
  11.   
  12.     public void setPassword(String password) {  
  13.         this.password = password;  
  14.     }  
  15.   
  16.     public String getUsername() {  
  17.         return username;  
  18.     }  
  19.   
  20.     public void setUsername(String username) {  
  21.         this.username = username;  
  22.     }  
  23. }  


MyBeanPostProcessor类,实现了BeanPostProcessor接口:

  1. package com.springdemo.postProcessor;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.config.BeanPostProcessor;  
  5.   
  6. import com.springdemo.form.LoginForm;  
  7.   
  8. public class MyBeanPostProcessor implements BeanPostProcessor {  
  9.   
  10.       
  11.       
  12.     public Object postProcessAfterInitialization(Object bean, String beanName)  
  13.             throws BeansException {  
  14.         // TODO Auto-generated method stub  
  15.         //如果是PostProcessorBean则username信息  
  16.         if(bean instanceof PostProcessorBean)  
  17.         {  
  18.             System.out.println("PostProcessorBean Bean initialized");  
  19.             PostProcessorBean pb = (PostProcessorBean)bean;  
  20.               
  21.             System.out.println("username:"+pb.getUsername());  
  22.         }  
  23.         return bean;  
  24.     }  
  25.   
  26.     public Object postProcessBeforeInitialization(Object bean, String beanName)  
  27.             throws BeansException {  
  28.         // TODO Auto-generated method stub  
  29.         if(bean instanceof PostProcessorBean)  
  30.         {  
  31.             System.out.println("PostProcessorBean Bean initializing");  
  32.             PostProcessorBean pb = (PostProcessorBean)bean;  
  33.               
  34.             System.out.println("username:"+pb.getUsername());  
  35.         }  
  36.         return bean;  
  37.     }  
  38.   
  39. }  


MyBeanFactoryPostProcessor实现了BeanFactoryPostProcessor接口:

  1. package com.springdemo.postProcessor;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.MutablePropertyValues;  
  5. import org.springframework.beans.factory.config.BeanDefinition;  
  6. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;  
  7. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  8.   
  9. public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {  
  10.   
  11.     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)  
  12.             throws BeansException {  
  13.         // TODO Auto-generated method stub  
  14.         //BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能  
  15.         //我们在这里修改postProcessorBean的username注入属性  
  16.         BeanDefinition bd = beanFactory.getBeanDefinition("postProcessorBean");  
  17.         MutablePropertyValues pv =  bd.getPropertyValues();  
  18.         if(pv.contains("username"))  
  19.         {  
  20.             pv.addPropertyValue("username", "xiaojun");  
  21.         }  
  22.           
  23.     }  
  24.   
  25. }  


编写测试用例:

  1. package com.springdemo.test;  
  2.   
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.springdemo.factory.ApplicationContextFactory;  
  8.   
  9. import com.springdemo.postProcessor.PostProcessorBean;  
  10.   
  11. import junit.framework.TestCase;  
  12.   
  13. public class BeanPostPorcessorTest extends TestCase {  
  14.     private ApplicationContext context;  
  15.     protected void setUp() throws Exception {  
  16.         super.setUp();  
  17.         String[] paths = {"classpath*:/spring/applicationContext-*.xml"};  
  18.   
  19.         context = new ClassPathXmlApplicationContext(paths);  
  20.           
  21.     }  
  22.   
  23.     protected void tearDown() throws Exception {  
  24.         super.tearDown();  
  25.     }  
  26.       
  27.     public void testBeanPostProcessor()  
  28.     {  
  29.           
  30.     }  
  31.     public void testBeanFactoryPostProcessor()  
  32.     {  
  33.         //PostProcessorBean bean =(PostProcessorBean)ServiceLocator.getService("postProcessorBean");  
  34.         PostProcessorBean bean =(PostProcessorBean)context.getBean("postProcessorBean");  
  35.         System.out.println("---------------testBeanFactoryPostProcessor----------------");  
  36.         System.out.println("username:"+bean.getUsername());  
  37.         System.out.println("password:"+bean.getPassword());  
  38.         //  
  39.     }  
  40. }  


spring配置文件如下(先不启用MyBeanFactoryPostProcessor):

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.       
  6.     <bean class="com.springdemo.postProcessor.MyBeanPostProcessor"></bean>  
  7.     <!--我们先把BeanFactoryPostProcessor注释掉,不启用,然后查看测试输出结果    
  8.     <bean class="com.springdemo.postProcessor.MyBeanFactoryPostProcessor"></bean>  
  9.     -->  
  10.     <bean id="postProcessorBean" class="com.springdemo.postProcessor.PostProcessorBean" >  
  11.         <property name="username" value="test"></property>  
  12.         <property name="password" value="test"></property>  
  13.     </bean>  
  14. </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 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别

原文地址:https://www.cnblogs.com/langtianya/p/5179352.html