Spring的BeanFactoryPostProcessor和BeanPostProcessor

轉載:http://blog.csdn.net/caihaijiang/article/details/35552859


BeanFactoryPostProcessor和BeanPostProcessor,這兩個接口,都是spring初始化bean時對外暴露的擴展點。兩個接口名稱看起來很相似,但作用及使用場景卻不同,分析如下:

1、BeanFactoryPostProcessor接口

該接口的定義如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public interface BeanFactoryPostProcessor {  
  2.   
  3.     /** 
  4.      * Modify the application context's internal bean factory after its standard 
  5.      * initialization. All bean definitions will have been loaded, but no beans 
  6.      * will have been instantiated yet. This allows for overriding or adding 
  7.      * properties even to eager-initializing beans. 
  8.      * @param beanFactory the bean factory used by the application context 
  9.      * @throws org.springframework.beans.BeansException in case of errors 
  10.      */  
  11.     void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;  
  12.   
  13. }  
實現該接口,可以在spring的bean創建之前,修改bean的定義屬性。也就是說,Spring允許BeanFactoryPostProcessor在容器實例化任何其它bean之前讀取配置元數據,並可以根據需要進行修改,例如可以把bean的scope從singleton改爲prototype,也可以把property的值給修改掉。可以同時配置多個BeanFactoryPostProcessor,並通過設置'order'屬性來控制各個BeanFactoryPostProcessor的執行次序。
注意:BeanFactoryPostProcessor是在spring容器加載了bean的定義文件之後,在bean實例化之前執行的。接口方法的入參是ConfigurrableListableBeanFactory,使用該參數,可以獲取到相關bean的定義信息,例子:

1)spring bean的定義:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  
  9.     default-autowire="byName">  
  10.       
  11.     <bean id="myJavaBean" class="com.ali.caihj.postprocessor.MyJavaBean">  
  12.         <property name="desc" value="測試一下啦" />  
  13.         <property name="remark" value="這是備註信息啦啦啦" />  
  14.     </bean>  
  15.     <bean id="myBeanFactoryPostProcessor" class="com.ali.caihj.postprocessor.MyBeanFactoryPostProcessor" />  
  16. </beans>  

2)自定義的BeanFactoryPostProcessor:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {  
  2.   
  3.     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {  
  4.         System.out.println("調用MyBeanFactoryPostProcessor的postProcessBeanFactory");  
  5.         BeanDefinition bd = beanFactory.getBeanDefinition("myJavaBean");  
  6.         System.out.println("屬性值============" + bd.getPropertyValues().toString());  
  7.         MutablePropertyValues pv =  bd.getPropertyValues();    
  8.         if (pv.contains("remark")) {    
  9.             pv.addPropertyValue("remark""把備註信息修改一下");    
  10.         }    
  11.         bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);  
  12.     }  
  13.   
  14. }  
spring中,有內置的一些BeanFactoryPostProcessor實現類,常用的有:
  • org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
  • org.springframework.beans.factory.config.PropertyOverrideConfigurer
  • org.springframework.beans.factory.config.CustomEditorConfigurer:用來註冊自定義的屬性編輯器


2、BeanPostProcessor接口

該接口的定義如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public interface BeanPostProcessor {  
  2.   
  3.     /** 
  4.      * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean 
  5.      * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code> 
  6.      * or a custom init-method). The bean will already be populated with property values. 
  7.      * The returned bean instance may be a wrapper around the original. 
  8.      * @param bean the new bean instance 
  9.      * @param beanName the name of the bean 
  10.      * @return the bean instance to use, either the original or a wrapped one 
  11.      * @throws org.springframework.beans.BeansException in case of errors 
  12.      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet 
  13.      */  
  14.     Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;  
  15.   
  16.     /** 
  17.      * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean 
  18.      * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code> 
  19.      * or a custom init-method). The bean will already be populated with property values. 
  20.      * The returned bean instance may be a wrapper around the original. 
  21.      * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean 
  22.      * instance and the objects created by the FactoryBean (as of Spring 2.0). The 
  23.      * post-processor can decide whether to apply to either the FactoryBean or created 
  24.      * objects or both through corresponding <code>bean instanceof FactoryBean</code> checks. 
  25.      * <p>This callback will also be invoked after a short-circuiting triggered by a 
  26.      * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method, 
  27.      * in contrast to all other BeanPostProcessor callbacks. 
  28.      * @param bean the new bean instance 
  29.      * @param beanName the name of the bean 
  30.      * @return the bean instance to use, either the original or a wrapped one 
  31.      * @throws org.springframework.beans.BeansException in case of errors 
  32.      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet 
  33.      * @see org.springframework.beans.factory.FactoryBean 
  34.      */  
  35.     Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;  
  36.   
  37. }  
BeanPostProcessor,可以在spring容器實例化bean之後,在執行bean的初始化方法前後,添加一些自己的處理邏輯。這裏說的初始化方法,指的是下面兩種:

1)bean實現了InitializingBean接口,對應的方法爲afterPropertiesSet

2)在bean定義的時候,通過init-method設置的方法

注意:BeanPostProcessor是在spring容器加載了bean的定義文件並且實例化bean之後執行的。BeanPostProcessor的執行順序是在BeanFactoryPostProcessor之後。

spring中,有內置的一些BeanPostProcessor實現類,例如:

  • org.springframework.context.annotation.CommonAnnotationBeanPostProcessor:支持@Resource註解的注入
  • org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor:支持@Required註解的注入
  • org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:支持@Autowired註解的注入
  • org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor:支持@PersistenceUnit和@PersistenceContext註解的注入
  • org.springframework.context.support.ApplicationContextAwareProcessor:用來爲bean注入ApplicationContext等容器對象

這些註解類的BeanPostProcessor,在spring配置文件中,可以通過這樣的配置 <context:component-scan base-package="*.*" /> ,自動進行註冊。(spring通過ComponentScanBeanDefinitionParser類來解析該標籤


3、下面通過完整的一個例子,來加深理解

1)定義一個JavaBean

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MyJavaBean implements InitializingBean {  
  2.     private String desc;  
  3.     private String remark;  
  4.       
  5.     public MyJavaBean() {  
  6.         System.out.println("MyJavaBean的構造函數被執行啦");  
  7.     }  
  8.     public String getDesc() {  
  9.         return desc;  
  10.     }  
  11.     public void setDesc(String desc) {  
  12.         System.out.println("調用setDesc方法");  
  13.         this.desc = desc;  
  14.     }  
  15.     public String getRemark() {  
  16.         return remark;  
  17.     }  
  18.     public void setRemark(String remark) {  
  19.         System.out.println("調用setRemark方法");  
  20.         this.remark = remark;  
  21.     }  
  22.     public void afterPropertiesSet() throws Exception {  
  23.         System.out.println("調用afterPropertiesSet方法");  
  24.         this.desc = "在初始化方法中修改之後的描述信息";  
  25.     }  
  26.     public void initMethod() {  
  27.         System.out.println("調用initMethod方法");  
  28.     }  
  29.     public String toString() {  
  30.         StringBuilder builder = new StringBuilder();  
  31.         builder.append("[描述:").append(desc);  
  32.         builder.append(", 備註:").append(remark).append("]");  
  33.         return builder.toString();  
  34.     }  
  35. }  


2)定義一個BeanFactoryPostProcessor

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {  
  2.   
  3.     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {  
  4.         System.out.println("調用MyBeanFactoryPostProcessor的postProcessBeanFactory");  
  5.         BeanDefinition bd = beanFactory.getBeanDefinition("myJavaBean");  
  6.         MutablePropertyValues pv =  bd.getPropertyValues();    
  7.         if (pv.contains("remark")) {    
  8.             pv.addPropertyValue("remark""在BeanFactoryPostProcessor中修改之後的備忘信息");    
  9.         }    
  10.     }  
  11.   
  12. }  
3)定義一個BeanPostProcessor

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MyBeanPostProcessor implements BeanPostProcessor {  
  2.   
  3.     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {  
  4.         System.out.println("BeanPostProcessor,對象" + beanName + "調用初始化方法之前的數據: " + bean.toString());  
  5.         return bean;  
  6.     }  
  7.     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {  
  8.         System.out.println("BeanPostProcessor,對象" + beanName + "調用初始化方法之後的數據:" + bean.toString());  
  9.         return bean;  
  10.     }  
  11. }  
4)spring的配置
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  
  9.     default-autowire="byName">  
  10.       
  11.     <bean id="myJavaBean" class="com.ali.caihj.postprocessor.MyJavaBean" init-method="initMethod">  
  12.         <property name="desc" value="原始的描述信息" />  
  13.         <property name="remark" value="原始的備註信息" />  
  14.     </bean>  
  15.       
  16.     <bean id="myBeanPostProcessor" class="com.ali.caihj.postprocessor.MyBeanPostProcessor" />  
  17.     <bean id="myBeanFactoryPostProcessor" class="com.ali.caihj.postprocessor.MyBeanFactoryPostProcessor" />  
  18. </beans>  

5)測試

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class PostProcessorMain {  
  2.     public static void main(String[] args) {  
  3.         ApplicationContext context = new ClassPathXmlApplicationContext("config/postprocessor.xml");  
  4.         MyJavaBean bean = (MyJavaBean) context.getBean("myJavaBean");  
  5.         System.out.println("===============下面輸出結果============");  
  6.         System.out.println("描述:" + bean.getDesc());  
  7.         System.out.println("備註:" + bean.getRemark());  
  8.   
  9.     }  
  10. }  
6)運行結果如下:


7)分析

從上面的結果可以看出,BeanFactoryPostProcessor在bean實例化之前執行,之後實例化bean(調用構造函數,並調用set方法注入屬性值),然後在調用兩個初始化方法前後,執行了BeanPostProcessor。初始化方法的執行順序是,先執行afterPropertiesSet,再執行init-method。


4、進一步深入分析

在使用ApplicationContext啓動spring容器的時候,在AbstractApplicationContext.refresh()方法中,完成相關初始化工作:


1)BeanFactoryPostProcessor.postProcessBeanFactory,是在第5步執行的,invokeBeanFactoryPostProcessors方法實現如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. /** 
  2.      * Instantiate and invoke all registered BeanFactoryPostProcessor beans, 
  3.      * respecting explicit order if given. 
  4.      * <p>Must be called before singleton instantiation. 
  5.      */  
  6.     protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {  
  7.         // Invoke factory processors registered with the context instance.  
  8.         for (Iterator it = getBeanFactoryPostProcessors().iterator(); it.hasNext();) {  
  9.             BeanFactoryPostProcessor factoryProcessor = (BeanFactoryPostProcessor) it.next();  
  10.             factoryProcessor.postProcessBeanFactory(beanFactory);  
  11.         }  
  12.   
  13.         // Do not initialize FactoryBeans here: We need to leave all regular beans  
  14.         // uninitialized to let the bean factory post-processors apply to them!  
  15.         String[] postProcessorNames =  
  16.                 beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.classtruefalse);  
  17.   
  18.         // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,  
  19.         // Ordered, and the rest.  
  20.         List priorityOrderedPostProcessors = new ArrayList();  
  21.         List orderedPostProcessorNames = new ArrayList();  
  22.         List nonOrderedPostProcessorNames = new ArrayList();  
  23.         for (int i = 0; i < postProcessorNames.length; i++) {  
  24.             if (isTypeMatch(postProcessorNames[i], PriorityOrdered.class)) {  
  25.                 priorityOrderedPostProcessors.add(beanFactory.getBean(postProcessorNames[i]));  
  26.             }  
  27.             else if (isTypeMatch(postProcessorNames[i], Ordered.class)) {  
  28.                 orderedPostProcessorNames.add(postProcessorNames[i]);  
  29.             }  
  30.             else {  
  31.                 nonOrderedPostProcessorNames.add(postProcessorNames[i]);  
  32.             }  
  33.         }  
  34.   
  35.         // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.  
  36.         Collections.sort(priorityOrderedPostProcessors, new OrderComparator());  
  37.         invokeBeanFactoryPostProcessors(beanFactory, priorityOrderedPostProcessors);  
  38.   
  39.         // Next, invoke the BeanFactoryPostProcessors that implement Ordered.  
  40.         List orderedPostProcessors = new ArrayList();  
  41.         for (Iterator it = orderedPostProcessorNames.iterator(); it.hasNext();) {  
  42.             String postProcessorName = (String) it.next();  
  43.             orderedPostProcessors.add(getBean(postProcessorName));  
  44.         }  
  45.         Collections.sort(orderedPostProcessors, new OrderComparator());  
  46.         invokeBeanFactoryPostProcessors(beanFactory, orderedPostProcessors);  
  47.   
  48.         // Finally, invoke all other BeanFactoryPostProcessors.  
  49.         List nonOrderedPostProcessors = new ArrayList();  
  50.         for (Iterator it = nonOrderedPostProcessorNames.iterator(); it.hasNext();) {  
  51.             String postProcessorName = (String) it.next();  
  52.             nonOrderedPostProcessors.add(getBean(postProcessorName));  
  53.         }  
  54.         invokeBeanFactoryPostProcessors(beanFactory, nonOrderedPostProcessors);  
  55.     }  
  56.   
  57.     /** 
  58.      * Invoke the given BeanFactoryPostProcessor beans. 
  59.      */  
  60.     private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List postProcessors) {  
  61.         for (Iterator it = postProcessors.iterator(); it.hasNext();) {  
  62.             BeanFactoryPostProcessor postProcessor = (BeanFactoryPostProcessor) it.next();  
  63.             postProcessor.postProcessBeanFactory(beanFactory);  
  64.         }  
  65.     }  
通過beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false),獲取spring配置文件中定義的所有實現BeanFactoryPostProcessor接口的bean,然後根據優先級進行排序,之後對於每個BeanFactoryPostProcessor,調用postProcessBeanFactory方法。

2)而BeanPostProcessor的執行,取決於配置文件中bean的定義,如果定義的bean是singleton並且不是抽象類,也不延遲初始化,則BeanPostProcessor是在第11步中執行;而對於prototype的bean,BeanPostProcessor是在程序getBean的時候執行的。在第6步中,調用registerBeanPostProcessors方法,註冊所有實現BeanPostProcessor接口的bean,該方法的實現如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {  
  2.         String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.classtruefalse);  
  3.   
  4.         // Register BeanPostProcessorChecker that logs an info message when  
  5.         // a bean is created during BeanPostProcessor instantiation, i.e. when  
  6.         // a bean is not eligible for getting processed by all BeanPostProcessors.  
  7.         int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;  
  8.         beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));  
  9.   
  10.         // Separate between BeanPostProcessors that implement PriorityOrdered,  
  11.         // Ordered, and the rest.  
  12.         List priorityOrderedPostProcessors = new ArrayList();  
  13.         List orderedPostProcessorNames = new ArrayList();  
  14.         List nonOrderedPostProcessorNames = new ArrayList();  
  15.         for (int i = 0; i < postProcessorNames.length; i++) {  
  16.             if (isTypeMatch(postProcessorNames[i], PriorityOrdered.class)) {  
  17.                 priorityOrderedPostProcessors.add(beanFactory.getBean(postProcessorNames[i]));  
  18.             }  
  19.             else if (isTypeMatch(postProcessorNames[i], Ordered.class)) {  
  20.                 orderedPostProcessorNames.add(postProcessorNames[i]);  
  21.             }  
  22.             else {  
  23.                 nonOrderedPostProcessorNames.add(postProcessorNames[i]);  
  24.             }  
  25.         }  
  26.   
  27.         // First, register the BeanPostProcessors that implement PriorityOrdered.  
  28.         Collections.sort(priorityOrderedPostProcessors, new OrderComparator());  
  29.         registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);  
  30.   
  31.         // Next, register the BeanPostProcessors that implement Ordered.  
  32.         List orderedPostProcessors = new ArrayList();  
  33.         for (Iterator it = orderedPostProcessorNames.iterator(); it.hasNext();) {  
  34.             String postProcessorName = (String) it.next();  
  35.             orderedPostProcessors.add(getBean(postProcessorName));  
  36.         }  
  37.         Collections.sort(orderedPostProcessors, new OrderComparator());  
  38.         registerBeanPostProcessors(beanFactory, orderedPostProcessors);  
  39.   
  40.         // Finally, register all other BeanPostProcessors.  
  41.         List nonOrderedPostProcessors = new ArrayList();  
  42.         for (Iterator it = nonOrderedPostProcessorNames.iterator(); it.hasNext();) {  
  43.             String postProcessorName = (String) it.next();  
  44.             nonOrderedPostProcessors.add(getBean(postProcessorName));  
  45.         }  
  46.         registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);  
  47.     }  

在第11步中,調用finishBeanFactoryInitialization方法,該方法通過調用DefaultListableBeanFactory.preInstantiateSingletons(),進行相關初始化工作:

從上面的代碼可以看出,對於非抽象類、非延遲初始化的單例bean,在spring容器啓動的時候調用getBean方法來實例化bean,並進行相關初始化工作,getBean方法最終調用AbstractAutowireCapableBeanFactory.doCreateBean方法,該方法的實現如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {  
  2.         // Instantiate the bean.  
  3.         BeanWrapper instanceWrapper = null;  
  4.         if (mbd.isSingleton()) {  
  5.             instanceWrapper = (BeanWrapper) this.factoryBeanInstanceCache.remove(beanName);  
  6.         }  
  7.         if (instanceWrapper == null) {  
  8.             instanceWrapper = createBeanInstance(beanName, mbd, args);  
  9.         }  
  10.         final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);  
  11.         Class beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);  
  12.   
  13.         // Allow post-processors to modify the merged bean definition.  
  14.         synchronized (mbd.postProcessingLock) {  
  15.             if (!mbd.postProcessed) {  
  16.                 applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);  
  17.                 mbd.postProcessed = true;  
  18.             }  
  19.         }  
  20.   
  21.         // Eagerly cache singletons to be able to resolve circular references  
  22.         // even when triggered by lifecycle interfaces like BeanFactoryAware.  
  23.         boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&  
  24.                 isSingletonCurrentlyInCreation(beanName));  
  25.         if (earlySingletonExposure) {  
  26.             if (logger.isDebugEnabled()) {  
  27.                 logger.debug("Eagerly caching bean '" + beanName +  
  28.                         "' to allow for resolving potential circular references");  
  29.             }  
  30.             addSingletonFactory(beanName, new ObjectFactory() {  
  31.                 public Object getObject() throws BeansException {  
  32.                     return getEarlyBeanReference(beanName, mbd, bean);  
  33.                 }  
  34.             });  
  35.         }  
  36.   
  37.         // Initialize the bean instance.  
  38.         Object exposedObject = bean;  
  39.         try {  
  40.             populateBean(beanName, mbd, instanceWrapper);  
  41.             exposedObject = initializeBean(beanName, exposedObject, mbd);  
  42.         }  
  43.         catch (Throwable ex) {  
  44.             if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {  
  45.                 throw (BeanCreationException) ex;  
  46.             }  
  47.             else {  
  48.                 throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);  
  49.             }  
  50.         }  
  51.   
  52.         if (earlySingletonExposure) {  
  53.             Object earlySingletonReference = getSingleton(beanName, false);  
  54.             if (earlySingletonReference != null) {  
  55.                 if (exposedObject == bean) {  
  56.                     exposedObject = earlySingletonReference;  
  57.                 }  
  58.                 else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {  
  59.                     String[] dependentBeans = getDependentBeans(beanName);  
  60.                     Set actualDependentBeans = new LinkedHashSet(dependentBeans.length);  
  61.                     for (int i = 0; i < dependentBeans.length; i++) {  
  62.                         String dependentBean = dependentBeans[i];  
  63.                         if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {  
  64.                             actualDependentBeans.add(dependentBean);  
  65.                         }  
  66.                     }  
  67.                     if (!actualDependentBeans.isEmpty()) {  
  68.                         throw new BeanCurrentlyInCreationException(beanName,  
  69.                                 "Bean with name '" + beanName + "' has been injected into other beans [" +  
  70.                                 StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +  
  71.                                 "] in its raw version as part of a circular reference, but has eventually been " +  
  72.                                 "wrapped. This means that said other beans do not use the final version of the " +  
  73.                                 "bean. This is often the result of over-eager type matching - consider using " +  
  74.                                 "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");  
  75.                     }  
  76.                 }  
  77.             }  
  78.         }  
  79.   
  80.         // Register bean as disposable.  
  81.         registerDisposableBeanIfNecessary(beanName, bean, mbd);  
  82.   
  83.         return exposedObject;  
  84.     }  
在該方法中,首先調用createBeanInstance方法,創建bean實例對象(這個時候執行bean的構造方法),然後調用populateBean方法,對bean進行填充,注入相關依賴,之後再調用方法initializeBean,進行相關初始化工作,initializeBean方法的實現如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. protected Object initializeBean(String beanName, Object bean, RootBeanDefinition mbd) {  
  2.         if (bean instanceof BeanNameAware) {  
  3.             ((BeanNameAware) bean).setBeanName(beanName);  
  4.         }  
  5.   
  6.         if (bean instanceof BeanClassLoaderAware) {  
  7.             ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());  
  8.         }  
  9.   
  10.         if (bean instanceof BeanFactoryAware) {  
  11.             ((BeanFactoryAware) bean).setBeanFactory(this);  
  12.         }  
  13.   
  14.         Object wrappedBean = bean;  
  15.         if (mbd == null || !mbd.isSynthetic()) {  
  16.             wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);  
  17.         }  
  18.   
  19.         try {  
  20.             invokeInitMethods(beanName, wrappedBean, mbd);  
  21.         }  
  22.         catch (Throwable ex) {  
  23.             throw new BeanCreationException(  
  24.                     (mbd != null ? mbd.getResourceDescription() : null),  
  25.                     beanName, "Invocation of init method failed", ex);  
  26.         }  
  27.   
  28.         if (mbd == null || !mbd.isSynthetic()) {  
  29.             wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);  
  30.         }  
  31.         return wrappedBean;  
  32.     }  
從上面的實現可以看出,先調用applyBeanPostProcessorsBeforeInitialization方法,執行每個BeanPostProcessor的postProcessBeforeInitialization,然後調用invokeInitMethods方法,執行bean的初始化方法,最後調用applyBeanPostProcessorsAfterInitialization方法,執行每個BeanPostProcessor的postProcessAfterInitialization方法。這三個方法的實現如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1.     public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)  
  2.             throws BeansException {  
  3.   
  4.         Object result = existingBean;  
  5.         for (Iterator it = getBeanPostProcessors().iterator(); it.hasNext();) {  
  6.             BeanPostProcessor beanProcessor = (BeanPostProcessor) it.next();  
  7.             result = beanProcessor.postProcessBeforeInitialization(result, beanName);  
  8.         }  
  9.         return result;  
  10.     }  
  11.   
  12.     public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)  
  13.             throws BeansException {  
  14.   
  15.         Object result = existingBean;  
  16.         for (Iterator it = getBeanPostProcessors().iterator(); it.hasNext();) {  
  17.             BeanPostProcessor beanProcessor = (BeanPostProcessor) it.next();  
  18.             result = beanProcessor.postProcessAfterInitialization(result, beanName);  
  19.         }  
  20.         return result;  
  21.     }  
  22. protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mbd)  
  23.             throws Throwable {  
  24.   
  25.         boolean isInitializingBean = (bean instanceof InitializingBean);  
  26.         if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {  
  27.             if (logger.isDebugEnabled()) {  
  28.                 logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");  
  29.             }  
  30.             ((InitializingBean) bean).afterPropertiesSet();  
  31.         }  
  32.   
  33.         String initMethodName = (mbd != null ? mbd.getInitMethodName() : null);  
  34.         if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&  
  35.                 !mbd.isExternallyManagedInitMethod(initMethodName)) {  
  36.             invokeCustomInitMethod(beanName, bean, initMethodName, mbd.isEnforceInitMethod());  
  37.         }  
  38.     }  
從invokeInitMethods方法的實現可以看出,先執行afterPropertiesSet方法,然後再通過反射,執行init-method指定的方法。


http://jinnianshilongnian.iteye.com/blog/1762632

問題

如下方式可以成功掃描到@Controller註解的Bean,不會掃描@Service/@Repository的Bean。正確

 

Java代碼  收藏代碼
  1.  <context:component-scan base-package="org.bdp.system.test.controller">   
  2.      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   
  3. </context:component-scan>  

  

但是如下方式,不僅僅掃描@Controller,還掃描@Service/@Repository的Bean,可能造成一些問題

 

Java代碼  收藏代碼
  1.  <context:component-scan base-package="org.bdp">   
  2.      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   
  3. </context:component-scan>  

 

這個尤其在springmvc+spring+hibernate等集成時最容易出問題的地,最典型的錯誤就是:

事務不起作用

 

這是什麼問題呢?

分析

1、<context:component-scan>會交給org.springframework.context.config.ContextNamespaceHandler處理;

 

Java代碼  收藏代碼
  1. registerBeanDefinitionParser("component-scan"new ComponentScanBeanDefinitionParser());  

 

2、ComponentScanBeanDefinitionParser會讀取配置文件信息並組裝成org.springframework.context.annotation.ClassPathBeanDefinitionScanner進行處理;

3、如果沒有配置<context:component-scan>的use-default-filters屬性,則默認爲true,在創建ClassPathBeanDefinitionScanner時會根據use-default-filters是否爲true來調用如下代碼:

 

Java代碼  收藏代碼
  1.   protected void registerDefaultFilters() {  
  2. this.includeFilters.add(new AnnotationTypeFilter(Component.class));  
  3. ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();  
  4. try {  
  5.     this.includeFilters.add(new AnnotationTypeFilter(  
  6.             ((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));  
  7.     logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");  
  8. }  
  9. catch (ClassNotFoundException ex) {  
  10.     // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.  
  11. }  
  12. try {  
  13.     this.includeFilters.add(new AnnotationTypeFilter(  
  14.             ((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));  
  15.     logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");  
  16. }  
  17. catch (ClassNotFoundException ex) {  
  18.     // JSR-330 API not available - simply skip.  
  19. }  

 

 

可以看到默認ClassPathBeanDefinitionScanner會自動註冊對@Component、@ManagedBean、@Named註解的Bean進行掃描。如果細心,到此我們就找到問題根源了。

 

 

4、在進行掃描時會通過include-filter/exclude-filter來判斷你的Bean類是否是合法的:

 

Java代碼  收藏代碼
  1. protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {  
  2.     for (TypeFilter tf : this.excludeFilters) {  
  3.         if (tf.match(metadataReader, this.metadataReaderFactory)) {  
  4.             return false;  
  5.         }  
  6.     }  
  7.     for (TypeFilter tf : this.includeFilters) {  
  8.         if (tf.match(metadataReader, this.metadataReaderFactory)) {  
  9.             AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();  
  10.             if (!metadata.isAnnotated(Profile.class.getName())) {  
  11.                 return true;  
  12.             }  
  13.             AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);  
  14.             return this.environment.acceptsProfiles(profile.getStringArray("value"));  
  15.         }  
  16.     }  
  17.     return false;  
  18. }  

 

首先通過exclude-filter 進行黑名單過濾;

然後通過include-filter 進行白名單過濾;

否則默認排除。

 

結論

Java代碼  收藏代碼
  1. <context:component-scan base-package="org.bdp">   
  2.      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   
  3. </context:component-scan>  

 

爲什麼這段代碼不僅僅掃描@Controller註解的Bean,而且還掃描了@Component的子註解@Service、@Reposity。因爲use-default-filters默認爲true。所以如果不需要默認的,則use-default-filters=“false”禁用掉。

 

 

請參考

《SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常見問題總結》 

《第三章 DispatcherServlet詳解 ——跟開濤學SpringMVC》中的ContextLoaderListener初始化的上下文和DispatcherServlet初始化的上下文關係。

Spring源碼解析之初始化 http://blog.csdn.net/pcceo1/article/details/50921162

 

如果在springmvc配置文件,不使用cn.javass.demo.web.controller前綴,而是使用cn.javass.demo,則service、dao層的bean可能也重新加載了,但事務的AOP代理沒有配置在springmvc配置文件中,從而造成新加載的bean覆蓋了老的bean,造成事務失效。只要使用use-default-filters=“false”禁用掉默認的行爲就可以了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章