1. Definition

Spring Boot is a sub-project under the Spring open source organization. It is a one-stop solution for Spring components. It mainly simplifies the difficulty of using Spring, saves heavy configuration, provides various starters, and developers can quickly get started.

2. Provide multiple sets of configuration files to define multiple sets of different environment configurations

applcation.properties application-dev.properties application-test.properties application-prod.properties

3 How do you understand Starters in Spring Boot?

Starters can be understood as a starter. It contains a series of dependency packages that can be integrated into the application. You can integrate Spring and other technologies in one stop without having to look for sample codes and dependency packages everywhere. If you want to use Spring JPA to access the database, just add the spring-boot-starter-data-jpa starter dependency.

All third-party components xxx-spring-boot-starter will have a spring.factories file under classpath:/META-INF. Spring scans the spring.factories file under classpath:/META-INF and reads the configuration of the class File, and then loaded into the Ioc container. For example, druid connection pool

The official package spring-boot-starter-xxx will record all configuration files in spring.factories in the /META-INF directory under spring-boot-autoconfigure. Such as redis, aop, websocket. Plug-ins that have not been imported are eliminated through ConditionOnClass.

Once I know the location of the configuration file, how do I scan and process it?

@Import(EnableAutoConfigurationImportSelector.class) in @EnableAutoConfiguration in @SpringBootApplication composite annotation ----> From the @Import class above, you can find the mapping for auto-loading and auto-configuration below. This method will load the classpath and all automatically configured classes mapped in the META-INF/spring.factories configuration under the jar package. Then return an array containing configuration class information through the selectImports method.

We can simulate the scanning and injection process by ourselves

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Import(MyDefineImportSelector.class)public @interface EnableConfiguration { }
public class MyDefineImportSelector implements ImportSelector{    @Override    public String[] selectImports(AnnotationMetadata importingClassMetadata) {        //动态导入bean, 告诉了Spring  , 两个配置类在哪里         //TODO 在这里去加载所有的配置类就行?        // 通过某种机制去完成指定路径的配置类的扫描就行?        //package.class.classname        return new String[]{GpSqlSessionFactory.class.getName(), RedisConfiguration.class.getName()};    }}

4. SPI mechanism: provide an extended interface, and the third party can implement it by itself

JVM extension mechanism, such as Durid

The following conditions:

  • Need to create a META-INF/services in the classpath directory
  • Create the full path name of an extension point in this directory.
  • Fill in the file to realize the file encoding format of this extension point UTF-8
  • ServiceLoader to load

5. What is the core annotation of Spring Boot? Which annotations are mainly composed of?

The annotation above the startup class is @SpringBootApplication, which is also the core annotation of Spring Boot. The main combination includes the following 3 annotations:

@SpringBootConfiguration: A combination of @Configuration (define an Ioc container configuration class) annotations to realize the function of configuration files.

@EnableAutoConfiguration: Turn on the automatic configuration function, you can also turn off a certain automatic configuration option, such as turning off the data source automatic configuration function: @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }).

The most important compound annotation is @import(EnableAutoConfigurationImportSelector.class) ----> From the @Import class above, you can find the mapping for automatic loading and automatic configuration below. This method will load the classpath and all automatically configured classes mapped in the META-INF/spring.factories configuration under the jar package.

@ComponentScan: Spring component or bean scan

6. Spring Boot core configuration file

Application and bootstrap configuration files.

The application configuration file is easy to understand and is mainly used for automatic configuration of Spring Boot projects.

The bootstrap configuration file has the following application scenarios.

  • When using the Spring Cloud Config configuration center, you need to add configuration properties connected to the configuration center in the bootstrap configuration file to load the configuration information of the external configuration center;
  • Some fixed attributes that cannot be overridden;
  • Some encryption/decryption scenarios;