Spring的BeanFactoryPostProcessor和BeanPostProcessor

PostProcessor:後處理器; 
BeanPostProcessor:Bean的後置處理器(處理的對象是Bean); 
BeanFactoryPostProcessor:BeanFactory的後置處理器(處理的對象是BeanFactory)java

(1)看一下BeanFactoryPostProcessor接口的源碼:spring

@FunctionalInterface public interface BeanFactoryPostProcessor { /** * Modify the application context's internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for overriding or adding * properties even to eager-initializing beans. * @param beanFactory the bean factory used by the application context * @throws org.springframework.beans.BeansException in case of errors */ void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; } 

 

①方法postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)的做用是什麼? 
源碼中是這樣說明的:app

//Modify the application context's internal bean factory after its standard initialization.

bean factory已經經歷了standard initialization; 
方法的入參ConfigurableListableBeanFactory beanFactory便是standard initialization後的bean factory; 
把standard initialization後的bean factory交給咱們處理,咱們能夠對其進行修改。 
②何時調用postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)? 
源碼中是這樣說明的:ide

// All bean definitions will have been loaded, but no beans will have been instantiated yet.

 

bean factory已經經歷了standard initialization,全部的bean definitions也已經被加載到bean factory之中,可是尚未建立(實例化)任何的Bean。 
③方法的入參beanFactory 
方法的入參ConfigurableListableBeanFactory beanFactory便是咱們須要處理的對象;beanFactory已經經歷了standard initialization,全部的bean definitions也已經被加載到beanFactory之中。 
④如何操做(處理)beanFactory呢? 
操做beanFactory的常見方式: 
對加載到beanFactory中的bean definitions進行修改。post

經過參數beanFactory,能夠獲取相關Bean的bean definition,並修改bean definition。測試

(2)代碼示例: 
User類:this

public class User { private String username; private int age; public void setUsername(String username) { System.out.println("setUsername()"); this.username = username; } public void setAge(int age) { System.out.println("setAge()"); this.age = age; } public User(){ System.out.println("Constructor()"); } @Override public String toString() { return "User [username=" + username + ", age=" + age + "]"; } }

BeanFactoryPostProcessor處理器:spa

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public MyBeanFactoryPostProcessor(){ System.out.println("BeanFactoryPostProcessor實現類的構造器"); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("BeanFactoryPostProcessor.postProcessBeanFactory()"); //獲取指定的BeanDefinition BeanDefinition bd = beanFactory.getBeanDefinition("user1"); //根據BeanDefinition獲取MutablePropertyValues MutablePropertyValues pvs = bd.getPropertyValues(); //增長 pvs.add("age", 20); //覆蓋 pvs.add("username", "Richard"); } } 

 

Bean配置文件:code

<bean id="user1" class="com.User"> <!-- 這裏只爲屬性username賦值,屬性age沒有賦值 --> <property name="username" value="Jack"></property> </bean> <bean class="com.MyBeanFactoryPostProcessor"></bean>

 

測試類:xml

public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); User user1=(User)context.getBean("user1"); System.out.println(user1); } 

運行結果: 

BeanFactoryPostProcessor實現類的構造器 
BeanFactoryPostProcessor.postProcessBeanFactory() 
Constructor() 
setUsername() 
setAge() 
User [username=Richard, age=20]

(3)注意點: 
BeanFactoryPostProcessor接口和BeanPostProcessor接口很是類似,都是spring初始化bean時對外暴露的擴展點。

①BeanFactory的後置處理器:Spring提供的一種特殊的Bean 
②BeanFactory的後置處理器,須要實現BeanFactoryPostProcessor接口 
③須要在Bean的配置文件中,註冊BeanFactory的後置處理器,但不須要設置id屬性。IOC容器會自動識別這是個BeanFactory的後置處理器,自動的使用它。

<bean class="com.MyBeanFactoryPostProcessor"></bean>

 

④實例化BeanFactoryPostProcessor實現類,並調用BeanFactoryPostProcessor的postProcessBeanFactory()方法; 這2步會很早進行,在Bean實例被建立以前進行。 ⑤Bean的後置處理器,操做的對象是Bean,會對容器中的全部Bean進行處理,每一個Bean都要通過2個處理方法postProcessBeforeInitialization()和postProcessAfterInitialization(); BeanFactory的後置處理器,操做的對象是BeanFactory,所以只會有一次處理過程(很早進行,在Bean實例被建立以前進行),即只調用處理方法postProcessBeanFactory()一次。

相關文章
相關標籤/搜索