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

时间:2023-12-14 13:57:14

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

BEAN类:

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

MyBeanPostProcessor类,实现了BeanPostProcessor接口:

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

MyBeanFactoryPostProcessor实现了BeanFactoryPostProcessor接口:

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

编写测试用例:

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

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. <bean class="com.springdemo.postProcessor.MyBeanPostProcessor"></bean>
  6. <!--我们先把BeanFactoryPostProcessor注释掉,不启用,然后查看测试输出结果
  7. <bean class="com.springdemo.postProcessor.MyBeanFactoryPostProcessor"></bean>
  8. -->
  9. <bean id="postProcessorBean" class="com.springdemo.postProcessor.PostProcessorBean" >
  10. <property name="username" value="test"></property>
  11. <property name="password" value="test"></property>
  12. </bean>
  13. </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的用法和区别