1. 程式人生 > >Spring事務配置解惑

Spring事務配置解惑

一、專案中spring+mybaits xml配置解析

一般我們會在datasource.xml中進行如下配置,但是其中每個配置項原理和用途是什麼,並不是那麼清楚,如果不清楚的話,在使用時候就很有可能會遇到坑,所以下面對這些配置項進行一一解說

1)配置資料來源
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"
/> <property name="url" value="${db_url}" /> <property name="username" value="$db_user}" /> <property name="password" value="${db_passwd}" /> <property name="maxWait" value="${db_maxWait}" /> <property name="maxActive" value="28"
/> <property name="initialSize" value="2" /> <property name="minIdle" value="0" /> <property name="timeBetweenEvictionRunsMillis" value="db_time" /> </bean> (2)建立sqlSessionFactory <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
> <property name="mapperLocations" value="classpath*:com/**/mapper/*Mapper*.xml" /> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.test.***.dal" /> </bean> (3)配置掃描器,掃描指定路徑的mapper生成資料庫操作代理類 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="javax.annotation.Resource"></property> <property name="basePackage" value="com.test.***.dal.***.mapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> (4)配置事務管理器 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> (5)宣告使用註解式事務 <tx:annotation-driven transaction-manager="transactionManager" /> (6)註冊各種beanfactory處理器 <context:annotation-config /> (7)該配置建立了一個TransactionInterceptor的bean,作為事務切面的執行方法 <tx:advice id="defaultTxAdvice"> <tx:attributes> <tx:method name="*" rollback-for="Exception" /> </tx:attributes> </tx:advice> (8)該配置建立了一個DefaultBeanFactoryPointcutAdvisor的bean,該bean是一個advisor,裡面包含了pointcut和advice.前者說明切面加在哪裡,後者是執行邏輯。此處可以配多個advisor <aop:config> <aop:pointcut id="myCut" expression="(execution(* *..*BoImpl.*(..))) "/> <aop:advisor pointcut-ref="myCut" advice-ref="defaultTxAdvice" /> </aop:config>

1.1 資料來源配置

(1)是資料來源配置,這個沒啥好說的。

1.2 配置SqlSessionFactory

(2) 作用是根據配置建立一個SqlSessionFactory,看下SqlSessionFactoryBean的程式碼知道它實現了FactoryBean和InitializingBean類,由於實現了InitializingBean,所以自然它的afterPropertiesSet方法,由於實現了FactoryBean類,所以自然會有getObject方法。下面看下時序圖:

screenshot.png

從時序圖可知,SqlSessionFactoryBean類主要是通過屬性配置建立SqlSessionFactory例項,具體是解析配置中所有的mapper檔案放到configuration,然後作為建構函式引數例項化一個DefaultSqlSessionFactory作為SqlSessionFactory。

1.3 配置掃描器,掃描指定路徑的mapper生成資料庫操作代理類

MapperScannerConfigurer 實現了 BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware介面,所以會重寫一下方法:

1.3.1
//在bean註冊到ioc後建立例項前修改bean定義和新增bean註冊,這個是在context的refresh方法呼叫
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
1.3.2
//在bean註冊到ioc後建立例項前修改bean定義或者屬性值
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
1.3.3
//set屬性設定後呼叫
void afterPropertiesSet() throws Exception;
1.3.4
//獲取IOC容器上下文,在context的prepareBeanFactory中呼叫
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
1.3.5
//獲取bean在ioc容器中名字,在context的prepareBeanFactory中呼叫
void setBeanName(String name);

先上個掃描mapper生成代理類並註冊到ioc時序圖:

screenshot.png

首先MapperScannerConfigurer實現的afterPropertiesSet方法用來確保屬性basePackage不為空

public void afterPropertiesSet() throws Exception {
    notNull(this.basePackage, "Property 'basePackage' is required");
  }

postProcessBeanFactory裡面啥都沒做,setBeanName獲取了bean的名字,setApplicationContext裡面獲取了ioc上下文。下面看重要的方法postProcessBeanDefinitionRegistry,由於mybais是執行時候才通過解析mapper檔案生成代理類注入到ioc,所以postProcessBeanDefinitionRegistry正好可以幹這個事情。

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
    if (this.processPropertyPlaceHolders) {
      processPropertyPlaceHolders();
    }

    //構造一個ClassPathMapperScanner查詢mapper
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
    scanner.setAddToConfig(this.addToConfig);
    //javax.annotation.Resource
    scanner.setAnnotationClass(this.annotationClass);
    scanner.setMarkerInterface(this.markerInterface);
    //引用sqlSessionFactory
    scanner.setSqlSessionFactory(this.sqlSessionFactory);
    scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
    scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
    scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
    //ioc上下文
    scanner.setResourceLoader(this.applicationContext);
    scanner.setBeanNameGenerator(this.nameGenerator);
    scanner.registerFilters();
   //basePackage=com.alibaba.***.dal.***.mapper,com.alibaba.rock.auth.mapper,com.alibaba.rock.workflow.dal.workflow.mapper
    scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
  }

下面重點看下scan方法:

 public Set<BeanDefinitionHolder> doScan(String... basePackages) {
    //根據指定路徑去查詢對應mapper的介面類,並轉化為beandefination
    Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

    if (beanDefinitions.isEmpty()) {
      logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");
    } else {
      //修改介面類bean的beandefination
      processBeanDefinitions(beanDefinitions);
    }

    return beanDefinitions;
  }

其中super.doScan(basePackages);根據指定路徑查詢mapper介面類,並生成bean的定義物件,物件中包含beanclassname,beanclass屬性,最後註冊該bean到ioc容器。下面看下最重要的processBeanDefinitions方法對bean定義的改造。

 private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
    GenericBeanDefinition definition;
    for (BeanDefinitionHolder holder : beanDefinitions) {
      definition = (GenericBeanDefinition) holder.getBeanDefinition();

      // 上面講的掃描後beanclass設定的為mapper介面類,但是這裡修改為MapperFactoryBean,MapperFactoryBean代理了mapper介面類,並且實際mapper介面類作為建構函式傳入了      definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); 
      definition.setBeanClass(this.mapperFactoryBean.getClass());
      definition.getPropertyValues().add("addToConfig", this.addToConfig);

      //設定屬性配置中的sqlSessionFactory
      boolean explicitFactoryUsed = false;
      if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
        definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
        explicitFactoryUsed = true;
      } else if (this.sqlSessionFactory != null) {
        definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
        explicitFactoryUsed = true;
      }

      if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
        if (explicitFactoryUsed) {
          logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
        explicitFactoryUsed = true;
      } else if (this.sqlSessionTemplate != null) {
        if (explicitFactoryUsed) {
          logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
        explicitFactoryUsed = true;
      }

      if (!explicitFactoryUsed) {
        if (logger.isDebugEnabled()) {
          logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'.");
        }
        definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
      }
    }
  }

注:這裡修改了mapper介面類的beandefination中的beanclass為MapperFactoryBean,它則負責生產資料類操作代理類,實際mapper介面類作為建構函式傳入了 。由於只修改了beanclass,沒有修改beanname,所以我們從容器中獲取時候無感知的。

在上一個代理bean如何構造的時序圖:

screenshot.png

下面看下MapperFactoryBean是如何生成代理類的:
首先,上面程式碼設定了MapperFactoryBean的setSqlSessionFactory方法:

  public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    if (!this.externalSqlSession) {
      this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
    }
  }

上面方法建立了sqlSession,由於MapperFactoryBean為工廠bean所以例項化時候會呼叫getObject方法:

 public T getObject() throws Exception {
    return getSqlSession().getMapper(this.mapperInterface);
  }

其實是呼叫了SqlSessionTemplate->getMapper,其中mapperInterface就是建立MapperFactoryBean時候的建構函式引數。

public <T> T getMapper(Class<T> type) {
    return getConfiguration().getMapper(type, this);
  }

這裡呼叫getConfiguration().getMapper(type, this);實際是DefaultSqlSessionFactory裡面的configration的getMapper方法:

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
   //knownMappers是上面時序圖中步驟6設定進入的。
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }
 protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  public T newInstance(SqlSession sqlSession) {
   //代理回撥類為MapperProxy
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

在上一個實際執行sql時候呼叫代理類的序列圖:

screenshot.png

所以當呼叫實際的資料庫操作時候會呼叫MapperProxy的invoke方法:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (Object.class.equals(method.getDeclaringClass())) {
      try {
        return method.invoke(this, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

mapperMethod.execute(sqlSession, args);裡面實際是呼叫當前mapper對應的SqlSessionTemplate的資料庫操作,而它有委託給了代理類sqlSessionProxy,sqlSessionProxy是在SqlSessionTemplate的建構函式裡面建立的:

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
      PersistenceExceptionTranslator exceptionTranslator) {

    notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
    notNull(executorType, "Property 'executorType' is required");

    this.sqlSessionFactory = sqlSessionFactory;
    this.executorType = executorType;
    this.exceptionTranslator = exceptionTranslator;
    this.sqlSessionProxy = (SqlSession) newProxyInstance(
        SqlSessionFactory.class.getClassLoader(),
        new Class[] { SqlSession.class },
        new SqlSessionInterceptor());
  }

所以最終資料庫操作有被代理SqlSessionInterceptor執行:

 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      //有TransactionSynchronizationManager管理
      SqlSession sqlSession = getSqlSession(
          SqlSessionTemplate.this.sqlSessionFactory,
          SqlSessionTemplate.this.executorType,
          SqlSessionTemplate.this.exceptionTranslator);
      try {
        Object result = method.invoke(sqlSession, args);
        if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
          // force commit even on non-dirty sessions because some databases require
          // a commit/rollback before calling close()
          sqlSession.commit(true);
        }
        return result;
      } catch (Throwable t) {
          .....
      }
    }

public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) {

    notNull(sessionFactory, NO_SQL_SESSION_FACTORY_SPECIFIED);
    notNull(executorType, NO_EXECUTOR_TYPE_SPECIFIED);

    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);

    SqlSession session = sessionHolder(executorType, holder);
    if (session != null) {
      return session;
    }

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Creating a new SqlSession");
    }
   //這裡看到了使用sessionfactory熟悉的打開了一個session
    session = sessionFactory.openSession(executorType);

    registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session);

    return session;
  }

注意:這裡3裡面配置的掃描檔案在4的掃描檔案裡面一定要有,因為3給每個掃描檔案生成了一個代理,如果4裡面多了一個mapper,那麼在4中將找不到。

1.4 配置事務管理器

事務管理器作用見名知意,是用來管理事務的。

1.5 advice配置

作用是建立了一個TransactionInterceptor的bean,作為事務切面的執行方法。標籤解析的流程圖:

screenshot.png

由於是tx標籤,自然要查詢TxNamespaceHandler,程式碼如下:

public class TxNamespaceHandler extends NamespaceHandlerSupport {

    static final String TRANSACTION_MANAGER_ATTRIBUTE = "transaction-manager";
    static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";

    static String getTransactionManagerName(Element element) {
        return (element.hasAttribute(TRANSACTION_MANAGER_ATTRIBUTE) ?
                element.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE) : DEFAULT_TRANSACTION_MANAGER_BEAN_NAME);
    }

    @Override
    public void init() {
        registerBeanDefinitionParser("advice", new TxAdviceBeanDefinitionParser());
        registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
        registerBeanDefinitionParser("jta-transaction-manager", new JtaTransactionManagerBeanDefinitionParser());
    }

}

從init方法知道advice標籤需要TxAdviceBeanDefinitionParser這個解析類。
結合流程圖第一步設定了事務管理器的引用,我們看下引用的bean的名字:

static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";

static String getTransactionManagerName(Element element) {
        return (element.hasAttribute(TRANSACTION_MANAGER_ATTRIBUTE) ?
                element.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE) : DEFAULT_TRANSACTION_MANAGER_BEAN_NAME);
    }

可以知道如果沒有配置這個屬性,那麼預設查詢依賴beanname=transactionManager。
然後parseAttributeSource主要迴圈解析我們配置的method標籤,和設定的方法的事務屬性。
另外程式碼:

protected Class<?> getBeanClass(Element element) {
        return TransactionInterceptor.class;
    }

可以知道這個advice標籤實際是創了TransactionInterceptor物件,並且通過呼叫setTransactionManager設定了事務管理器,通過setTransactionAttributeSources設定了事務屬性。

1.6 設定advisor

標籤<aop:config>作用是建立了DefaultBeanFactoryPointcutAdvisor作為攔截器,把滿足切點的bean進行代理使用事務攔截器進行攔截。具體標籤邏輯先看流程圖:

screenshot.png

從標籤<aop:config>可知要查詢AopNamespaceHandler,程式碼如下:

public class AopNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {
        registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
        registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
        registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());

        registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
    }
}

可知config標籤是ConfigBeanDefinitionParser來解析的,根據流程圖知configureAutoProxyCreator註冊了AspectJAwareAdvisorAutoProxyCreator類,然後createAdvisorBeanDefinition建立了DefaultBeanFactoryPointcutAdvisor,它是個advisor,並且設定引用了advice,這個adivce就是上面1.5講解的,然後createPointcutDefinition建立了切點AspectJExpressionPointcut,最後把切點設定到了advisor。
DefaultBeanFactoryPointcutAdvisor作用就是對滿足pointcut表示式的類的方法進行代理,並且使用advice進行攔截處理,而advice就是事務攔截器。

1.7 設定註解式事務

上面介紹完後就可以使用事務切面了,但是有時候還需要在具體類或者方法上進行註解行事務,那麼這就需要加 <tx:annotation-driven transaction-manager=”transactionManager” />配置
先上時序圖:

screenshot.png

!
同理1.6 不同是這裡建立了advisor,設定了advice(事務攔截器),但是好像沒有設定pointcut,看下BeanFactoryTransactionAttributeSourceAdvisor原始碼知道:

public class BeanFactoryTransactionAttributeSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor {

    private TransactionAttributeSource transactionAttributeSource;

    private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {
        @Override
        protected TransactionAttributeSource getTransactionAttributeSource() {
            return transactionAttributeSource;
        }
    };
}

直接內建了pointcut,只不過1.6是AspectJExpressionPointcut表示式的切點,這裡是註解。
那麼這個BeanFactoryTransactionAttributeSourceAdvisor什麼時候被用來增強註解事務的類那,那是InfrastructureAdvisorAutoProxyCreator所做的事情,InfrastructureAdvisorAutoProxyCreator是個BeanPostProcessor,會在bean建立初始化後時候呼叫postProcessAfterInitialization,就是這個方法。

另外注意如果配置了多個註解式標籤在datasource.xml裡面時候只有第一個生效

public static void configureAutoProxyCreator(Element element, ParserContext parserContext) {
            AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);

            String txAdvisorBeanName = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME;
            //如果配置了多個註解式標籤在datasource.xml裡面時候只有第一個生效
            if (!parserContext.getRegistry().containsBeanDefinition(txAdvisorBeanName)) {
                Object eleSource = parserContext.extractSource(element);

                // Create the TransactionAttributeSource definition.
                RootBeanDefinition sourceDef = new RootBeanDefinition(
                        "org.springframework.transaction.annotation.AnnotationTransactionAttributeSource");
                sourceDef.setSource(eleSource);
                sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
                String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);

                // Create the TransactionInterceptor definition.
                RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
                interceptorDef.setSource(eleSource);
                interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
                registerTransactionManager(element, interceptorDef);
                interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
                String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);

                // Create the TransactionAttributeSourceAdvisor definition.
                RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
                advisorDef.setSource(eleSource);
                advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
                advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
                advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
                if (element.hasAttribute("order")) {
                    advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
                }
                parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef);

                CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
                compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
                compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
                compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, txAdvisorBeanName));
                parserContext.registerComponent(compositeDef);
            }
        }

1.8 註冊各種beanfactory處理器

當我們需要使用BeanPostProcessor時,最直接的使用方法是在Spring配置檔案中定義這些Bean。單這些會顯得比較笨拙,
例如:使用@Autowired註解,必須事先在Spring容器中宣告

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>

使用 @Required註解,就必須宣告:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

通過標籤<context:annotation-config/> ,我們可以同時自動註冊這些常用的beanfactory處理器,避免了我們一個個配置的繁瑣步驟:

public class ContextNamespaceHandler extends NamespaceHandlerSupport {

                ...
        registerJava5DependentParser("annotation-config",
                "org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser");
        ....
}

public class AnnotationConfigBeanDefinitionParser implements BeanDefinitionParser {

    public BeanDefinition parse(Element element, ParserContext parserContext) {
                ...
        // Obtain bean definitions for all relevant BeanPostProcessors.
        Set<BeanDefinitionHolder> processorDefinitions =
                AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source);

        // Register component for the surrounding <context:annotation-config> element.
        CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
        parserContext.pushContainingComponent(compDefinition);

        // Nest the concrete beans in the surrounding component.
        for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
            parserContext.registerComponent(new BeanComponentDefinition(processorDefinition));
        }

        // Finally register the composite component.
        parserContext.popAndRegisterContainingComponent();

        return null;
    }

}


public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
            BeanDefinitionRegistry registry, Object source) {

        Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>(4);

        // Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
        if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition();
            try {
                ClassLoader cl = AnnotationConfigUtils.class.getClassLoader();
                def.setBeanClass(cl.loadClass(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME));
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException(
                        "Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
            }
            def.setSource(source);
            def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            beanDefinitions.add(registerBeanPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
        if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
            def.setSource(source);
            def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            beanDefinitions.add(registerBeanPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
            def.setSource(source);
            def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            beanDefinitions.add(registerBeanPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
            def.setSource(source);
            def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            beanDefinitions.add(registerBeanPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
        }

        return beanDefinitions;
    }

主要註冊常用的:
RequiredAnnotationBeanPostProcessor
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
平時我們使用autowired或者required之所以能生效,就是因為這個自動注入ioc已經。


加多

加多

高階 Java 攻城獅 at 阿里巴巴加多,目前就職於阿里巴巴,熱衷併發程式設計、ClassLoader,Spring等開源框架,分散式RPC框架dubbo,springcloud等;愛好音樂,運動。微信公眾號:技術原始積累。知識星球賬號:技術原始積累