@Enable驅動邏輯

這個系列分爲5篇
4.spring,springBoot事件(最晚20200719)
5.自定義springboot starter(最晚20200726)

前言

工做中常常用到,以下註解:
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
他們都是@Enable開頭,各自實現不一樣的功能,解析這種@Enable的邏輯是什麼呢?

@Enable驅動邏輯

找入口

@Enable的模塊驅動,依賴於@Import實現。
@Import做用是裝載導入類,主要包括@Configuration class,ImportSelector實現類,ImportBeanDefinitionRegistrar實現類。
XML時代,常常是@Import,<context:component-scan>,<context:annotation-config>.一塊兒使用。
<context:annotation-config>(註解配置)中大機率有咱們須要找的邏輯。
根據 Spring Framework2.0引入的可擴展的XML編程機制,XML Schema命名空間須要與Handler創建映射關係。
該關係配置在相對於classpath下的/META-INF/spring.handlers中。

查看ContextNamespaceHandler 源碼
public class ContextNamespaceHandler extends NamespaceHandlerSupport {
   @Override
   public void init() {
       //省略其餘代碼
    registerBeanDefinitionParser("annotation-config", 
                    new AnnotationConfigBeanDefinitionParser());
  }
}複製代碼
<context:annotation-config> 對應AnnotationConfigBeanDefinitionParser這個就是要找的入口


找核心類

從AnnotationConfigBeanDefinitionParser的parse方法開始一路向下,找到
AnnotationConfigUtils.registerAnnotationConfigProcessors中註冊了ConfigurationClassPostProcessor。


ConfigurationClassPostProcessor類註釋說明
1. 用於的引導處理@Configuration類
2. <context:annotation-config/>或 <context:component-scan/>時會註冊
不然須要手工編程
3. ConfigurationClassPostProcessor第一優先級,保證
@Configuration}類中聲明@Bean,在其餘 BeanFactoryPostProcessor執行以前被註冊
擴展
AnnotationConfigApplicationContext中new AnnotationBeanDefinitionReader也調用了 AnnotationConfigUtils .
registerAnnotationConfigProcessors
從類註釋中,能夠看出ConfigurationClassPostProcessor就是要找的核心類
找核心方法
查看 ConfigurationClassPostProcessor 的層級關係爲

Aware系列注入相應資源,Ordered設置優先級,值得關注的就是
postProcessBeanDefinitionRegistry了。
postProcessBeanDefinitionRegistry其內部有2個方法
  1. postProcessBeanDefinitionRegistry在BeanDefinition註冊以後,BeanFactoryPostProcessor執行以前,修改或重寫BeanDefinition
  2. 繼承自BeanFactoryPostProcessor的postProcessBeanFactory,BeanDefinition加載以後,Bean實例化以前,重寫或添加BeanDefinition,修改BeanFactory

瀏覽2個方法,都有processConfigBeanDefinitions,從名稱能夠看出是處理配置類Bean定義java

ConfigurationClassPostProcessor#processConfigBeanDefinitions就是要找的核心方法


梳理流程

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
   List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
   String[] candidateNames = registry.getBeanDefinitionNames();

   for (String beanName : candidateNames) {
      BeanDefinition beanDef = registry.getBeanDefinition(beanName);
      if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) ||
            ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) {
         if (logger.isDebugEnabled()) {
            logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
         }
      }
      else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
         configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
      }
   }

   // 沒有找到 @Configuration classes 當即返回
   if (configCandidates.isEmpty()) {
      return;
   }
   //根據@Order 值進行排序
   configCandidates.sort((bd1, bd2) -> {
      int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());
      int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());
      return Integer.compare(i1, i2);
   });
   //經過封閉的應用程序上下文, 檢測任何自定義bean名稱生成策略
   supplied through the enclosing application context
   SingletonBeanRegistry sbr = null;
   if (registry instanceof SingletonBeanRegistry) {
      sbr = (SingletonBeanRegistry) registry;
      if (!this.localBeanNameGeneratorSet) {
         BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);
         if (generator != null) {
            this.componentScanBeanNameGenerator = generator;
            this.importBeanNameGenerator = generator;
         }
      }
   }
   if (this.environment == null) {
      this.environment = new StandardEnvironment();
   }
   // 解析@Configuration class
   ConfigurationClassParser parser = new ConfigurationClassParser(
         this.metadataReaderFactory, this.problemReporter, this.environment,
         this.resourceLoader, this.componentScanBeanNameGenerator, registry);

   Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
   Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
   do {
      parser.parse(candidates);
      parser.validate();

      Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());
      configClasses.removeAll(alreadyParsed);
      // 讀ConfigurationClass的信息,建立BeanDefinition
      if (this.reader == null) {
         this.reader = new ConfigurationClassBeanDefinitionReader(
               registry, this.sourceExtractor, this.resourceLoader, this.environment,
               this.importBeanNameGenerator, parser.getImportRegistry());
      }
      this.reader.loadBeanDefinitions(configClasses);
      alreadyParsed.addAll(configClasses);

      candidates.clear();
      if (registry.getBeanDefinitionCount() > candidateNames.length) {
         String[] newCandidateNames = registry.getBeanDefinitionNames();
         Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames));
         Set<String> alreadyParsedClasses = new HashSet<>();
         for (ConfigurationClass configurationClass : alreadyParsed) {
            alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
         }
         for (String candidateName : newCandidateNames) {
            if (!oldCandidateNames.contains(candidateName)) {
               BeanDefinition bd = registry.getBeanDefinition(candidateName);
               if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
                     !alreadyParsedClasses.contains(bd.getBeanClassName())) {
                  candidates.add(new BeanDefinitionHolder(bd, candidateName));
               }
            }
         }
         candidateNames = newCandidateNames;
      }
   }
   while (!candidates.isEmpty());

   // 將ImportRegistry註冊爲bean以支持importware@Configuration類
   if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
      sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
   }
   if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
      // Clear cache in externally provided MetadataReaderFactory; this is a no-op
      // for a shared cache since it'll be cleared by the ApplicationContext. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); } }複製代碼
ConfigurationClassPostProcessor#processConfigBeanDefinitions核心以下:
  1. 根據@Order 值進行排序
  2. 解析@Configuration class 爲ConfigurationClass對象
  3. 讀ConfigurationClass的信息,建立BeanDefinition
  4. 將ImportRegistry註冊爲bean以支持importware@Configuration類

重點關注解析方法

ConfigurationClassParser#parse方法負責解析@Configuration class 爲ConfigurationClass對象
查閱其源碼以下:
ConfigurationClassParser#doProcessConfigurationClass代碼以下:
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
      throws IOException {

   if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
      // Recursively process any member (nested) classes first
      processMemberClasses(configClass, sourceClass);
   }

   // Process any @PropertySource annotations
   for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
         sourceClass.getMetadata(), PropertySources.class,
         org.springframework.context.annotation.PropertySource.class)) {
      if (this.environment instanceof ConfigurableEnvironment) {
         processPropertySource(propertySource);
      }
      else {
         logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
               "]. Reason: Environment must implement ConfigurableEnvironment");
      }
   }

   // Process any @ComponentScan annotations
   Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
         sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
   if (!componentScans.isEmpty() &&
         !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
      for (AnnotationAttributes componentScan : componentScans) {
         // The config class is annotated with @ComponentScan -> perform the scan immediately
         Set<BeanDefinitionHolder> scannedBeanDefinitions =
               this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
         // Check the set of scanned definitions for any further config classes and parse recursively if needed
         for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
            BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
            if (bdCand == null) {
               bdCand = holder.getBeanDefinition();
            }
            if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
               parse(bdCand.getBeanClassName(), holder.getBeanName());
            }
         }
      }
   }

   // Process any @Import annotations
   processImports(configClass, sourceClass, getImports(sourceClass), true);

   // Process any @ImportResource annotations
   AnnotationAttributes importResource =
         AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
   if (importResource != null) {
      String[] resources = importResource.getStringArray("locations");
      Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
      for (String resource : resources) {
         String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
         configClass.addImportedResource(resolvedResource, readerClass);
      }
   }

   // Process individual @Bean methods
   Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
   for (MethodMetadata methodMetadata : beanMethods) {
      configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
   }

   // Process default methods on interfaces
   processInterfaces(configClass, sourceClass);

   // Process superclass, if any
   if (sourceClass.getMetadata().hasSuperClass()) {
      String superclass = sourceClass.getMetadata().getSuperClassName();
      if (superclass != null && !superclass.startsWith("java") &&
            !this.knownSuperclasses.containsKey(superclass)) {
         this.knownSuperclasses.put(superclass, configClass);
         // Superclass found, return its annotation metadata and recurse
         return sourceClass.getSuperClass();
      }
   }

   // No superclass -> processing is complete
   return null;
}複製代碼
ConfigurationClassParser#doProcessConfigurationClass(ConfigurationClass,AnnatationMetatdata)將@PropertySource @ComponentScan@Import,@ImportResource,@Bean等一塊兒處理了。
看到這裏基本邏輯已經理清 了,可是有一個疑問
@Configuration中的@Bean沒有其餘特殊處理嗎?


瀏覽代碼解決疑問

從上邊瀏覽的代碼能夠看到徹底模式,會被AOP加強
那什麼是徹底模式呢? 在ConfigurationClassUtils找到以下方法:
public class ConfigurationClassUtils{
//省略其餘方法
public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata) {
   return metadata.isAnnotated(Configuration.class.getName());
}
}複製代碼
@Configuration class是徹底模式,@Component,@Bean是輕量級模式
那AOP加強了做用是什麼呢?查看 ConfigurationClassEnhancer 的類註釋以下:


/**
* Enhances {@link
Configuration
} classes by generating a CGLIB subclass which
* interacts with the Spring container to respect bean scoping semantics for
* {@code @Bean} methods. Each such {@code @Bean} method will be overridden in
* the generated subclass, only delegating to the actual {@code @Bean} method
* implementation if the container actually requests the construction of a new
* instance. Otherwise, a call to such an {@code @Bean} method serves as a
* reference back to the container, obtaining the corresponding bean by name.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.0
* @see #enhance
* @see
ConfigurationClassPostProcessor
*/
class ConfigurationClassEnhancer {


大概意思以下:
經過CGLIB加強@Configuration class。
每一個@Bean方法會生成子類。
首次被調用時,@Bean方法會被執行用於建立bean實例;
再次被調用時,不會再執行建立bean實例,而是根據bean名稱返回首次該方法被執行時建立的bean實例。


總結

1.ConfigurationClassPostProcessor負責篩選@Component Class、@Configuration Class以及@Bean定義的Bean,**
2.ConfigurationClassParser從候選的Bean定義中解析出ConfigurationClass集合,隨後被3.ConfigurationClassBeanDefinitionReader轉換爲BeanDefinition
4.ConfigurationClassParser的解析順序,
@PropertySource->@ComponentScan->@Import->@ImportResource->@Bean->接口的默認方法->處理父類
5.@Configuration class是徹底模式,@Component,@Bean是輕量級模式
6.CGLIB加強@Configuration class。每一個@Bean方法會生成子類。
首次被調用時,@Bean方法會被執行用於建立bean實例;
再次被調用時,不會再執行建立bean實例,而是根據bean名稱返回首次該方法被執行時建立的bean實例。
相關文章
相關標籤/搜索