@SpringBootApplication
Use IDEA to quickly build a SpringBoot project with SpringBoot version 2.1.6.release
@Configuration
@EnableAutoConfiguration
@ComponentScan
So if we use the following SpringBoot boot class, the entire SpringBoot application can still function as the previous boot class.
@Configuration
The @Configuration is the same @Configuration used by the Spring IoC container Configuration class. Since the SpringBoot application is a Spring reference, it also needs to load the Configuration of an IoC container. The startup class marked @Configuration is itself an IoC container Configuration class! Many SpringBoot code examples like to label the boot class @Configuration or @SpringBootApplication directly, which is confusing to developers who are new to SpringBoot. If we split the above SpringBoot boot class into two separate Java classes, the whole pattern becomes clear:
public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoConfiguration.class, args); }}@Configuration
@EnableAutoConfiguration
@ComponentScan
public class DemoConfiguration(a){
@Bean
public Controller controller(a){
return newController(); }}Copy the code
Therefore, DemoApplication is a standard main function startup class, and the DemoConfiguration annotation of @Configuration is actually a normal IoC container Configuration class in JavaConfig form, all Spring framework concepts.
@EnableAutoConfiguration
@enableAutoConfiguration is similar to @enable annotations provided in the Spring framework, such as @enablesCheduling and @enablembeanexport, which are supported by @import. Collect and register bean definitions for specific scenarios:
- @enablescheduling loads Spring scheduling-framework related bean definitions into the IoC container via @import.
- @enablembeanExport loads jMX-related bean definitions into the IoC container via @import.
With the help of @import, @enableAutoConfiguration loads all bean definitions that meet the automatic configuration criteria into the IoC container.
The most critical is @ Import (AutoConfigurationImportSelector. Class), with the aid of AutoConfigurationImportSelector, @enableAutoConfiguration helps SpringBoot applications load all eligible @Configuration configurations into the IoC container currently created and used by SpringBoot. So, how AutoConfigurationImportSelector loaded?
See AutoConfigurationImportSelector class source code, it depends on the SpringFactoriesLoader loadFactoryNames method, to obtain a set of automatic configuration class name.
The hero behind automatic configuration: SpringFactoriesLoader
The “Key = Value” configuration is loaded from the meta-INF /spring.factories file. The “Key = Value” configuration is the full class name of Java type.
example.MyServer=example.MyServerImpl1,example.MyServerImpl2
Copy the code
And then framework can depending on the type of a certain type as the Key to query the corresponding list of names, for AutoConfigurationImportSelector, According to the full name of the class of the @ EnableAutoConfiguration org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration as a lookup Key, Get the corresponding set of @Configuration classes:
The meta-INF/Spring. factories configuration file in SpringBoot’s autoconfigure dependency package is a good example.
So the @enableAutoConfiguration auto-configuration is to search all meta-INF/Spring. factories configuration files from your classpath, And the org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration corresponding Configuration items by reflecting instantiation corresponding @ JavaConfig form of the Configuration of the IoC container Configuration class, It is then summarized into one and loaded into the IoC container.
@ComponentScan
@ComponentScan corresponds to the < Context: Component-scan > element in the XML configuration form, which is used with annotations such as @Component and @Repository. The bean definition classes annotated with these annotations are collected in bulk into Spring’s IoC container.
We can fine-grained customize the scope that @ComponentScan automatically scans through properties like basePackages. If not specified, the default Spring framework implementation scans from the package of the class in which @ComponentScan is declared.
Reference materials: SpringBoot Reveals the Secrets of fast Building microservice System