spring 後置處理器BeanFactoryPostProcessor和BeanPostProcessor的用法和區別

來源:互聯網
上載者:User

 

主要區別就是: 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 stubif(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執行個體的內容(答案應該是否定的).

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.