SpringBoot (three) SpringBoot autoconfiguration

    We all know SpringBoot help us to integrate a number of components and configurations, so SpringBoot is how to integrate these configurations and is automatically configured at start of it. Speaking of which you may not need to go back and look at @SpringBootApplication this annotation, and have said before this comment is @ Configuration, @ EnableAutoConfiguration, @ ComponentScan combination of three notes, there needs @EnableAutoConfiguration special emphasis on this comment, not this is the wrong notes SpringBoot complete the automatic configuration. @EnableAutoConfiguration ----- @ Import (EnableAutoConfigurationImportSelector.class) ----- AutoConfigurationImportSelector this order we can automatically configure SpringBoot introduced into the selector. Next we will look at how to complete SpringBoot is automatically configured and observe at source.

. 1    public String [] selectImports (AnnotationMetadata annotationMetadata) {     // select Import 
2          IF (! IsEnabled (annotationMetadata)) { // determines whether the original data is not empty empty empty return empty import related operations 
. 3            return NO_IMPORTS;
 . 4          }
 . 5          the try {
 . 6              autoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader 
 . 7                       .loadMetadata ( the this .beanClassLoader);
 . 8              AnnotationAttributes attributes = getAttributes (annotationMetadata);   // load and acquires attribute 
9             List<String> configurations = getCandidateConfigurations(annotationMetadata, //获取配置信息
10                     attributes);
11             configurations = removeDuplicates(configurations); 
12             configurations = sort(configurations, autoConfigurationMetadata);
13             Set<String> exclusions = getExclusions(annotationMetadata, attributes);
14             checkExcludedClasses(configurations, exclusions);
15             configurations.removeAll(exclusions);
16             configurations = filter(configurations, autoConfigurationMetadata);  //No fully integrated for removal from the line 11 and 16 is configured to sort and filter 
. 17              fireAutoConfigurationImportEvents (Configurations, Exclusions);        // components after import filter 
18 is              return configurations.toArray ( new new String [configurations.size ()]);
 . 19          }
 20 is          the catch (IOException EX) {
 21 is              the throw  new new IllegalStateException (EX);
 22 is          }
 23 is      }

    The code above is the primary method for automatic configuration, in fact, operating procedures I have a simple explanation on the comments

     1. First, if the original data is determined to be empty then the specific operation

     2. Load the original data and obtaining property

     3. Get configuration information    (in fact, the most important thing is how to get the third point because the configuration information and load it is what we are most concerned about is the core part of the SpringBoot)

     4. Remove not fully integrated configuration and ordering filter    (because some configuration SpringBoot not be fully configured to us, we ourselves need to be manually configured here SpringBoot did so personally think that if all the components are integrated so it started on the project itself will cause some pressure, after all, some components do not need to be used in some projects)

     The import component configuration after treatment

    Here we need to emphasize at getCandidateConfigurations this method and see how SpringBoot is to obtain components.

. 1  protected List <String> getCandidateConfigurations (AnnotationMetadata Metadata,
 2              AnnotationAttributes Attributes) {
 . 3          List <String> Configurations = SpringFactoriesLoader.loadFactoryNames (    // the Spring factory loading configuration information 
. 4                  getSpringFactoriesLoaderFactoryClass (), getBeanClassLoader ());
 . 5          Assert.notEmpty (Configurations,
 . 6                  "no Auto configuration classes found in the META-INF / spring.factories the If you."
 . 7                          + "are the using A Custom Packaging, that the make Sure File iS correct.");   // If the configuration is arranged to return empty information or explanation did not find the configuration information in the META-INF / spring.factories this directory
8         return configurations;
9 }
 1 public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
 2 
 3 
 4 public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
 5         String factoryClassName = factoryClass.getName();
 6         try {
 7             Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :  //获取资源路径
 8                     ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
 9             List<String> result = new ArrayList<String>();
10             while (urls.hasMoreElements()) {                //如果存在将资源存入结果集中
11                 URL url = urls.nextElement();
12                 Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
13                 String factoryClassNames = properties.getProperty(factoryClassName);
14                 result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
15             }
16             return result;
17         }
18         catch (IOException ex) {
19             throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
20                     "] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
21         }
22     }

 

 

 

 I believe that here do not need too much explanation, and in a nutshell is SpringBoot will go to "META-INF / spring.factories" directory to obtain the configuration information, and then do auto-configuration, and now we look back initially SpringBoot term method for automatic configuration, we take a look at debug.

 

 

 Here we can see SpringBoot him to get in the directory prior to removal and no filtration 96 assembly following another look after filtration.

 

 

 

     After filtration there are 20, that is to say SpringBoot 20 components to our fully integrated and automatically configured, if other components need to be manually configured ourselves. (Fixed number of components supported versions might change here I use the version 1.5.9 of)

    In addition to this mode there are other ways to determine which components SpringBoot automatically configure it for us? If you really look at it this trouble, after all, we can not write every time when it starts to verify what is automatically configured through debug, SpringBoot provides debug startup mode in the console let us see what can be automated What configuration did not complete the auto-configuration, did not talk much on the map

 

 

 

 

    Positive matches: already automatically configured Negative matches: no automatic configuration, that's how SpringBoot complete the automatic configuration, in fact, want to automatically configure this for a long time or else to write, after all SpringBoot core configuration, also fear in some places was wrong to mislead others would like to see this article, see the wrong place hoping to correct it, learn from each other mutual progress, I am not a hard working person, but I want to be able to change the current sluggish state, until now still remember I am a university teacher's words, as long as the never too late to start, as long as efforts will be successful , there may be components of chicken soup, but still never too late to start as long as hope.

 

 

    For a long time I become decadent dawdle, even forget the original intention had chosen this profession, was always fond of complaining about the injustice, I hope I can get back the day after the efforts of their own, no one wants flat light mediocrity over a lifetime, not to be worthy of others, but to be worthy themselves, but also want each of whom insisted partner in this industry we can be worthy of their own, becoming the imagination of their own.

Guess you like

Origin www.cnblogs.com/wjt096/p/12585939.html