BeanDefinition註冊流程、spring 擴充套件點一(NamespaceHandler)

zngsf發表於2019-03-26
前言

    NamespaceHandler簡單來說就是名稱空間處理器,Spring為了開放性提供了NamespaceHandler機制,這樣我們就可以根據需求自己來處理我們設定的標籤元素。本文章解析<context:component-scan base-package="xxxxx"/>如何完成bean定義註冊的以及擴充套件點講解。

BeanDefinition註冊流程

測試程式碼:

程式碼:
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

TestService cs = context.getBean(TestService.class);

xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
   
   <!-- <context:annotation-config />
   <aop:aspectj-autoproxy /> -->
   
   <context:component-scan base-package="com.study.mike.spring.service"/>
   <!--   <context:exclude-filter type="annotation" expression=""/>
      <context:include-filter type="annotation" expression=""/>
   </context:component-scan> -->
  
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations" value="classpath:application.properties"/>
   </bean>
   <alias name="testService" alias="combat"/>
</beans>複製程式碼

流程:

BeanDefinition註冊流程、spring 擴充套件點一(NamespaceHandler)

先看幾個主要的方法:

1、ClassPathXmlApplicationContext構造器

public ClassPathXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {

//設定父容器
   super(parent);
//設定配置檔案spring.xml
   setConfigLocations(configLocations);
//是否重新整理容器
   if (refresh) {
//執行重新整理方法
      refresh();
   }
}
複製程式碼

2、AbstractRefreshableApplicationContext#refreshBeanFactory()重新整理工廠方法,此方法執行此上下文的基礎bean工廠的實際重新整理,關閉先前的bean工廠(如果有)並初始化上一個生命週期的下一階段的新bean工廠。

@Override
protected final void refreshBeanFactory() throws BeansException {
   //如果存在容器就銷燬容器
   if (hasBeanFactory()) {
      destroyBeans();
      closeBeanFactory();
   }
   try {
    //建立bean容器
      DefaultListableBeanFactory beanFactory = createBeanFactory();
//設定序列號id
      beanFactory.setSerializationId(getId());
      customizeBeanFactory(beanFactory);
//載入bean定義
      loadBeanDefinitions(beanFactory);
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory = beanFactory;
      }
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}
複製程式碼

3、AbstractRefreshableApplicationContext#loadBeanDefinitions(DefaultListableBeanFactory beanFactory),通過XmlBeanDefinitionReader載入bean定義

@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
   // Create a new XmlBeanDefinitionReader for the given BeanFactory.
   XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

  //使用此context的資源載入環境配置bean定義讀取器
   beanDefinitionReader.setEnvironment(this.getEnvironment());
   beanDefinitionReader.setResourceLoader(this);
   beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

 //允許子類提供讀取器的自定義初始化
   initBeanDefinitionReader(beanDefinitionReader);
//繼續實際載入bean定義
   loadBeanDefinitions(beanDefinitionReader);
}複製程式碼

4、AbstractXmlApplicationContext#loadBeanDefinitions(XmlBeanDefinitionReader reader),這個方法載入和/或註冊bean定義。

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
   //獲取到配置檔案迴圈載入
  Resource[] configResources = getConfigResources();
   if (configResources != null) {
      reader.loadBeanDefinitions(configResources);
   }
    //獲取到配置檔案(spring.xml)迴圈載入,這裡獲取的是ClassPathXmlApplicationContext的構造器
中setConfigLocations(configLocations)設定的檔案路徑;
   String[] configLocations = getConfigLocations();
   if (configLocations != null) {
      reader.loadBeanDefinitions(configLocations);
   }
}複製程式碼

5、 XmlBeanDefinitionReader#registerBeanDefinitions(Document doc, Resource resource),註冊DOM文件中bean定義。

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
   BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
   int countBefore = getRegistry().getBeanDefinitionCount();
//createReaderContext(resource) 很關鍵
   documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
   return getRegistry().getBeanDefinitionCount() - countBefore;
}

public XmlReaderContext createReaderContext(Resource resource) {
   return new XmlReaderContext(resource, this.problemReporter, this.eventListener,
         this.sourceExtractor, this, getNamespaceHandlerResolver());
}

//懶惰地建立一個預設的NamespaceHandlerResolver,如果之前沒有設定的話
public NamespaceHandlerResolver getNamespaceHandlerResolver() {
   if (this.namespaceHandlerResolver == null) {
      this.namespaceHandlerResolver = createDefaultNamespaceHandlerResolver();
   }
   return this.namespaceHandlerResolver;
}

protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
   ClassLoader cl = 
(getResourceLoader() != null ? getResourceLoader().getClassLoader() : getBeanClassLoader());
  //DefaultNamespaceHandlerResolver類中會將載入META-INF/spring.handlers裡面配置的:詳見下圖
   return new DefaultNamespaceHandlerResolver(cl);}複製程式碼

BeanDefinition註冊流程、spring 擴充套件點一(NamespaceHandler)

紅框圈住的都是spring裡面自己的擴充套件實現。例如:

aop對應處理的標籤:

<aop:config>
        <aop:pointcut id="loggerCutpoint"
            expression=
            "execution(* com.how2java.service.ProductService.*(..)) "/>
             
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:after pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
  </aop:config> 複製程式碼

怎麼區分<aop:config/>由AopNamespaceHandler處理呢?接下來就會講到,parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)這個方法。

6、DefaultBeanDefinitionDocumentReader#doRegisterBeanDefinitions(Element root),在給定的根<beans />元素中註冊每個bean定義。這個方法裡面有個關鍵點!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)啟用環境,比如:

BeanDefinition註冊流程、spring 擴充套件點一(NamespaceHandler)

我配置這個標籤下面的beans是dev環境的,我啟用環境是prd,那麼dev下的bean是不會被載入的。

protected void doRegisterBeanDefinitions(Element root) {
   BeanDefinitionParserDelegate parent = this.delegate;
   this.delegate = createDelegate(getReaderContext(), root, parent);

   if (this.delegate.isDefaultNamespace(root)) {
//獲取beans所屬環境
      String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
      if (StringUtils.hasText(profileSpec)) {
     //配置為Profile="dev,prd"的轉成陣列。
         String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
               profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
        //判斷當前beans標籤的profile和啟用環境是否匹配,不匹配直接返回。
         if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
            if (logger.isDebugEnabled()) {
               logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec +
                     "] not matching: " + getReaderContext().getResource());
            }
            return;
         }
      }
   }
   //前置處理,spring沒有實現
   preProcessXml(root);
  //正式解析
   parseBeanDefinitions(root, this.delegate);
//後置處理,spring沒有實現
 postProcessXml(root);

   this.delegate = parent;
}
複製程式碼

7、DefaultBeanDefinitionDocumentReader#parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate),解析文件中根級別的元素。

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
   if (delegate.isDefaultNamespace(root)) {
      NodeList nl = root.getChildNodes();
      for (int i = 0; i < nl.getLength(); i++) {
         Node node = nl.item(i);
         if (node instanceof Element) {
            Element ele = (Element) node;
          //這裡就是判斷是由預設的解析器去解析,還是由其他擴充套件NamespaceHandler處理。
            if (delegate.isDefaultNamespace(ele)) {
               parseDefaultElement(ele, delegate);
            }
            else {
               delegate.parseCustomElement(ele);
            }
         }
      }
   }
   else {
      delegate.parseCustomElement(root);
   }
}
複製程式碼

8、BeanDefinitionParserDelegate#parseCustomElement(Element ele, @Nullable BeanDefinition containingBd)

@Nullable
public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
   String namespaceUri = getNamespaceURI(ele);
   if (namespaceUri == null) {
      return null;
   }
  //這裡的readerContext就是前面我說的很關鍵的地方,createReaderContext(resource), 
接著看resolve(namespaceUri);
   NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
   if (handler == null) {
      error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
      return null;
   }
   return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}複製程式碼

9、DefaultNamespaceHandlerResolver#resolve(String namespaceUri)。

@Override
@Nullable
public NamespaceHandler resolve(String namespaceUri) {
   //獲取所有配置到spring.handlers(多個配置檔案)裡面的名稱空間->類,
   例:"http://www.springframework.org/schema/aop" -> "org.springframework.aop.config.AopNamespaceHandler"
   Map<String, Object> handlerMappings = getHandlerMappings();
//根據名稱空間獲取到對應的處理類.
   Object handlerOrClassName = handlerMappings.get(namespaceUri);
   if (handlerOrClassName == null) {
      return null;
   }
   else if (handlerOrClassName instanceof NamespaceHandler) {
      return (NamespaceHandler) handlerOrClassName;
   }
   else {
     //載入類
      String className = (String) handlerOrClassName;
      try {
         Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
         if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
            throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri +
                  "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
         }
         NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
         //初始化解析器。
         namespaceHandler.init();
         //重新put進handlerMappings,方便下次獲取時直接是類不是string
         handlerMappings.put(namespaceUri, namespaceHandler);
         return namespaceHandler;
      }
      catch (ClassNotFoundException ex) {
         throw new FatalBeanException("Could not find NamespaceHandler class [" + className +
               "] for namespace [" + namespaceUri + "]", ex);
      }
      catch (LinkageError err) {
         throw new FatalBeanException("Unresolvable class definition for NamespaceHandler class [" +
               className + "] for namespace [" + namespaceUri + "]", err);
      }
   }
}複製程式碼

10、初始化解析器

public class ContextNamespaceHandler extends NamespaceHandlerSupport {

   @Override
   public void init() {
      registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
      registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
      registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
     //這個就是解析<context:component-scan base-package="xxxxx"/>標籤的解析器
      registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
      registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
      registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
      registerBeanDefinitionParser("mbean-export", new MBeanExportBeanDefinitionParser());
      registerBeanDefinitionParser("mbean-server", new MBeanServerBeanDefinitionParser());
   }

}複製程式碼

什麼時候去執行這個解析呢?

在BeanDefinitionParserDelegate#parseCustomElement(Element ele, @Nullable BeanDefinition containingBd)方法裡面的最後一步:

 return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));複製程式碼

我們看一下這個解析方法是怎麼執行的。會調到NamespaceHandlerSupport#parse(Element element, ParserContext parserContext)這個方法.

@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
   BeanDefinitionParser parser = findParserForElement(element, parserContext);
  //parser.parse(element, parserContext)就會調到ComponentScanBeanDefinitionParser#parse方法
   return (parser != null ? parser.parse(element, parserContext) : null);
}
複製程式碼
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
//獲取到要掃描的包.
   String basePackage = element.getAttribute(BASE_PACKAGE_ATTRIBUTE);
   basePackage = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);
   String[] basePackages = StringUtils.tokenizeToStringArray(basePackage,
         ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
   ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);
//開始掃描,繼續看
   Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);
   registerComponents(parserContext.getReaderContext(), beanDefinitions, element);
  //不知道這麼為什麼是返回的null,原始碼就是這樣很奇怪。
   return null;
}


複製程式碼

11、ClassPathBeanDefinitionScanner#doScan(String... basePackages),掃描註冊。

protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
   Assert.notEmpty(basePackages, "At least one base package must be specified");
   Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
   for (String basePackage : basePackages) {
     //掃描到bean定義
      Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
      for (BeanDefinition candidate : candidates) {
         ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
         candidate.setScope(scopeMetadata.getScopeName());
         String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
         if (candidate instanceof AbstractBeanDefinition) {
            postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
         }
         if (candidate instanceof AnnotatedBeanDefinition) {
            AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
         }
         if (checkCandidate(beanName, candidate)) {
            BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
            definitionHolder =
                  AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
            beanDefinitions.add(definitionHolder);
        //註冊bean定義
            registerBeanDefinition(definitionHolder, this.registry);
         }
      }
   }
   return beanDefinitions;
}複製程式碼

12、DefaultListableBeanFactory#registerBeanDefinition(String beanName, BeanDefinition beanDefinition)真正註冊bean定義,註冊bean定義的工作就此完成。

@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
      throws BeanDefinitionStoreException {

   Assert.hasText(beanName, "Bean name must not be empty");
   Assert.notNull(beanDefinition, "BeanDefinition must not be null");
   //判斷是否是抽象bean定義,是做其他的處理.
   if (beanDefinition instanceof AbstractBeanDefinition) {
      try {
         ((AbstractBeanDefinition) beanDefinition).validate();
      }
      catch (BeanDefinitionValidationException ex) {
         throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
               "Validation of bean definition failed", ex);
      }
   }
   //beanDefinitionMap就是放bean定義的物件,判斷bean定義是否存在.
   BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
   if (existingDefinition != null) {
      if (!isAllowBeanDefinitionOverriding()) {
         throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
      }
      else if (existingDefinition.getRole() < beanDefinition.getRole()) {
         // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
         if (logger.isInfoEnabled()) {
            logger.info("Overriding user-defined bean definition for bean '" + beanName +
                  "' with a framework-generated bean definition: replacing [" +
                  existingDefinition + "] with [" + beanDefinition + "]");
         }
      }
      else if (!beanDefinition.equals(existingDefinition)) {
         if (logger.isDebugEnabled()) {
            logger.debug("Overriding bean definition for bean '" + beanName +
                  "' with a different definition: replacing [" + existingDefinition +
                  "] with [" + beanDefinition + "]");
         }
      }
      else {
         if (logger.isTraceEnabled()) {
            logger.trace("Overriding bean definition for bean '" + beanName +
                  "' with an equivalent definition: replacing [" + existingDefinition +
                  "] with [" + beanDefinition + "]");
         }
      }
      //放入最新的bean定義,註冊bean定義的工作就此完成
      this.beanDefinitionMap.put(beanName, beanDefinition);
   }
   else {
      if (hasBeanCreationStarted()) {
         // Cannot modify startup-time collection elements anymore (for stable iteration)
         synchronized (this.beanDefinitionMap) {
            this.beanDefinitionMap.put(beanName, beanDefinition);
            List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
            updatedDefinitions.addAll(this.beanDefinitionNames);
            updatedDefinitions.add(beanName);
            this.beanDefinitionNames = updatedDefinitions;
            if (this.manualSingletonNames.contains(beanName)) {
               Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
               updatedSingletons.remove(beanName);
               this.manualSingletonNames = updatedSingletons;
            }
         }
      }
      else {
         // Still in startup registration phase
         this.beanDefinitionMap.put(beanName, beanDefinition);
         this.beanDefinitionNames.add(beanName);
         this.manualSingletonNames.remove(beanName);
      }
      this.frozenBeanDefinitionNames = null;
   }

   if (existingDefinition != null || containsSingleton(beanName)) {
      resetBeanDefinition(beanName);
   }
}複製程式碼

此次講了,BeanDefinition註冊流程、擴充套件點NamespaceHandler以及裡面的用的策略模式、啟用環境相關的,容器重新整理AbstractApplicationContext#refresh()的第二步ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

其他的註冊流程註解、絕對路徑配置檔案,都是大同小異。



註明:原創文章不易,轉載請註明。https://juejin.im/post/5c9999e56fb9a070a972bbbc


相關文章