2. Main program class, main entry class

/** * @springBootApplication to annotate a main program class, Application shows that this is a Spring Boot * / @ SpringBootApplication public class HelloWorldMainApplication {public static void main (String [] The args) {/ / Spring application start up SpringApplication. Run (HelloWorldMainApplication. Class, args); }}Copy the code

SpringBootApplication: The SpringBoot application annotation indicates on a class that this class is the main configuration class for SpringBoot, and SpringBoot should run the main method of that class to start the SpringBoot application

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
      @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
      @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
Copy the code

@ SpringBootConfiguration: Spring Boot configuration class; Marked on a class to indicate that this is a Spring Boot configuration class;

@configuration: The Configuration class annotates this annotation;

Configuration —– configuration file; The configuration class is also a component in the container; component@enableAutoConfiguration: enables automatic configuration.

Spring Boot automatically configured things we needed to configure before; @enableAutoConfiguration tells SpringBoot to enable automatic configuration. In this way, automatic configuration takes effect.

@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
Copy the code

@autoConfigurationPackage: Automatic configuration package

@ Import (AutoConfigurationPackages. The Registrar. The class) :

Spring’s underlying annotation @import imports a component into the container; The imported components by AutoConfigurationPackages. The Registrar. The class;

Scan the package of the main configuration class (annotated by @SpringBootApplication) and all components in the following subpackages into the Spring container;

@ Import (EnableAutoConfigurationImportSelector. Class);

Import components into a container?

EnableAutoConfigurationImportSelector: import the selector which components;

Return all components that need to be imported as full class names; These components are added to the container;

There are a lot of auto-configuration classes imported into the container (xxxAutoConfiguration). Import and configure all the components needed for the scenario into the container;



With the automatic configuration class, we do not need to manually write configuration injection function components;

SpringFactoriesLoader. LoadFactoryNames (EnableAutoConfiguration. Class, this);

Spring Boot obtains the values specified by EnableAutoConfiguration from the meta-INF/Spring. factories under the classpar and imports these values into the container. Before we need to configure things ourselves, automatic configuration classes help us;

J2EE’s overall integration solution and automatic configuration are in spring-boot-Autoconfigure-1.5.9.relex.jar;