Initialization phase:
Step 1: initialize the spring application module and configure some basic environment variables, resources, listeners and constructors;
In the second step, the specific application startup scheme is realized, including process monitoring module, loading configuration environment module and creating context environment module
Step 3: automatic configuration module, which realizes the automatic configuration of spring boot
Start up phase:
Each springBoot program has a main entry, that is, the main method. The main method calls springApplication.run () to start the whole spring boot program. The class where the method is located has a @ SpringBootApplication annotation, which is a composite annotation
spring startup class:
@SpringBootAppclation public class SpringRunAppliction(){ public static void main(String[] args ){ //The first part of spring to start //Load the file and load the @ SpringBootApplaction annotation SpringAppliction.run(RunApp.calss); } }
@Composition of SpringBootApplication annotation:
@Target(ElementType.TYPE) //Valid for class @Retention(RetentionPolicy.RUNTIME) //Effective execution during operation @Documented //Whether to generate dynamic documents when generating source code @Inherited //Identifies whether annotations can be inherited //Modify the main startup class, identify that this is a configuration class that scans and configures the user's own configuration class, and instantiate the pair. All other configuration classes are loaded by the main startup class @SpringBootConfiguration //Load the jar package according to the user configuration @EnableAutoConfiguration //Scan package configuration @ComponentScan( //Exclude specific types (excludeFilters) excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class ), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { /*Exclude specific autoconfiguration classes and do not use them*/ @AliasFor(annotation = EnableAutoConfiguration.class) Class<?>[] exclude() default {}; /*Exclude specific autoconfiguration class names so that they will never be applied*/ @AliasFor(annotation = EnableAutoConfiguration.class) String[] excludeName() default {}; /**Basic package for scanning annotated components. *Use {@ link scanbasepackageclass} as a type safe alternative to string based package names.*/ @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanBasePackages() default {}; /**Type a security alternative to {@ link scanbasepacks} *Specifies the package to scan for annotated components. *Packages of each specified category will be scanned < p > consider creating a special class or interface without opp tag in each package *There is no use other than this property reference.*/ @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") Class<?>[] scanBasePackageClasses() default {}; /*Specifies whether a proxy should be{@ linkbean@Bean }Method to enforce bean lifecycle behavior*/ @AliasFor(annotation = Configuration.class) boolean proxyBeanMethods() default true;
@SpringBootConfiguration
Modify the main startup class to indicate that it is a Configuration class, which simply inherits from @ Configuration
An architecture can only have one springbootconfiguration
@Target(ElementType.TYPE) //Valid for class @Retention(RetentionPolicy.RUNTIME) //Effective execution during operation @Documented //Whether to generate dynamic documents when generating source code @Cofiguration //@The Configuration profile configures the class and integrates the third-party framework (Configuration) in the current class public @interface SpringBootConfiguration { /*Specifies whether a proxy should be{@ linkbean@Bean }Method to enforce bean lifecycle behavior*/ @AliasFor(annotation = Configuration.class) boolean proxyBeanMethods() default true; }
@Configuration flag. This is a configuration class, It is generally used to identify the class file and inform the SpringBoot program that this class is an integrated third-party framework / configuration class
@EnableAutoConfiguration
Enable automatic configuration of Spring application context and try to guess and configure the beans you may need. Automatic configuration classes are usually based on class paths and defined bean applications.
When you start with the @ SpringBootApplication annotation, the automatic configuration of the context is automatically enabled, so adding this annotation will have no additional effect
Automatic configuration tries to be as intelligent as possible and exits when you define more of your own configurations. You can always manually exclude any configuration you don't want to apply in the exclude property (use excludeName() if you don't have access rights). You can also exclude them through the spring.autoconfigure.exclude property of utility configuration. Automatic configuration is always applied after registering user-defined bean s
The package of a class annotated with @ EnableAutoConfiguration, which is automatically assembled, is usually used as the "default". For example, it will be used when scanning @ Entity classes.
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited //Automatic configuration package: the package name of the class marked with the @ springbootApplication annotation will be obtained, //And scan the package and its sub packages to add components to the container @AutoConfigurationPackage //It can help the springboot application load all qualified @ Configuration configurations into the current springboot //And create and use the IoC container (ApplicationContext) @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { }
@AutoConfigurationPackage
Start the automatic configuration package scanning path. Working principle: automatically and dynamically obtain the package path of the main startup class and start the package scanning mechanism
(the code should be written in the same package and sub package classes of the main startup class). Registering the package components of the same level and subset of the current main program is actually registering the definition of a Bean
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage { }
@Import(AutoConfigurationImportSelector.class)
@Import is also an annotation provided by Spring natively, through which the Registrar class is imported into the container.
springboot embodies the design idea of out of the box. After the user introduces the configuration, it can be used directly with only a small amount of configuration (configuration config file)
Add other packages to your own project and load them without running them
When the main boot is executed, @ Import(AutoConfigurationImportSelector.class) will be loaded internally. The function of this annotation is to scan and load the "initiators" according to the dependency information. Multiple initiators in Springboot will execute the corresponding jar packages according to the user's loading, and finally integrate all the required frameworks
AutoConfigurationImportSelector.class
Import the Selector into the container, help the Spring application load all the qualified Configuration classes in @ Configuration into the factory, tell them which components to import, and enter this class. There are some important methods to resolve
@ComponentScan()
Component scanning can automatically discover and package beans, automatically scan and load matching components (such as @ component and @ Pepository) or bean definitions, and finally download these bean definitions to the IoC container
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM,classes =TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })