Carding several key basic knowledge points of srping boot

Posted by beachcomber on Sun, 08 Sep 2019 18:03:17 +0200

Background:

Before spring boot, the use of ssh or ssm framework development, need a lot of configuration files, development efficiency is low, deployment and maintenance is more troublesome, at this time to solve these problems spring boot came into being.

Official Web Profile: Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Mainly elaborates and records the core functions of spring boot, with official links at the bottom:

  1. A spring project that can run independently
  2. Servlet container embedded
  3. Provide a large number of starter s to simplify maven configuration
  4. Automatically configure spring, and automatically configure beans for the classes in it (mostly, not all) according to the introduced maven package (jar package)
  5. Application monitoring of quasi-production is provided.
  6. Codeless generation and xml configuration via conditional annotations

Summary:

1-3 and 5 are encapsulation and optimization to simplify the development process. 4 and 6 are their core features.

Summary of the overall process: Start, scan all the jar packages containing META-INF/spring.factories, load the configuration class according to this file, load the bean s that meet the conditions according to the configuration class.

Source code reading and analysis:

  1. SpringBoot Application annotation:
    • Core Start Annotations, Composite Annotations
    • Automatic configuration based on which jar packages are introduced through Enable AutoConfiguration
    • @AutoConfigurationPackage
    • @Import({AutoConfigurationImportSelector.class})
    • The AutoConfiguration ImportSelector class is introduced dynamically
public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
            AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);
            return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
        }
 }
 public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) {
        return loadMetadata(classLoader, "META-INF/spring-autoconfigure-metadata.properties");
 }
 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
 }
  1. starter module principle:
    • The most obvious is that it encapsulates all the relevant dependency packages and their corresponding version numbers for a function.
    • How does the encapsulated starter module need to add default configurations?
    • META-INF/spring-autoconfigure-metadata.properties
    • META-INF/spring.factories

If you have doubts about the generation of bean s:

Reference address:

  1. https://spring.io/projects/spring-boot
  2. https://objcoding.com/2018/02/02/Costom-SpringBoot-Starter/

Topics: Programming Spring Maven SpringBoot ssh