<>1 pom.xml
* The core dependency is in the parent project
* spring-boot-dependencies
* We're introducing some Springboot When dependent , No version required , It's because of these version warehouses
<>1.1 stay pom.xml Middle click View parent project
<>1.2 There is also a parent project in this parent project
<>1.3 this parent Not inside Parent project
<>1.4 Managed a large number of jar Package version
<>1.5 stay Spring-boot-starter-parent in Resource filtering configured , We don't need to configure it .
<>2 starter
<dependencies> <!-- starter --> <dependency> <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
1. starter : That's it SpringBoot Startup scenario for ;
2. such as :Spring-boot-starter-web, He will help us import automatically web Environment all dependencies !
3.SpringBoot It will turn all functional scenarios into initiators one by one
<>3 main program
There are only two things in the main program , An annotation and SpringApplication.run()
1.@SpringBootApplication Label this class as a Springboot Application of
2.SpringApplication.run(DemoApplication.class, args); take SpringBoot
Application startup , Load by reflection DemoApplication Object of class
Surface meaning
<>3.1 annotation
Click in. It's a composite annotation
There are many others in this
1.@SpringBootConfiguration
SpringBoot Configuration of
2.@EnableAutoConfiguration
Auto import configuration
3.@ComponentScan
Scan package , Remove what
4.@ConfigurationPropertiesScan
Configure scan
@SpringBootConfiguration
Click in @SpringBootConfiguration
By one configuration Configured
@Configuration : It means this is a Spring Configuration class
Description the startup class is also a configuration class
Click in @Configuration
It was found that there was only one Component annotation , That means it's the same Spring One component
<>3.2@EnableAutoConfiguration
Auto import configuration
Click in
Found in addition to 4 In addition to the default configuration annotation , Also found @AutoConfigurationPackage Auto configuration package
@AutoConfigurationPackage
Click in @AutoConfigurationPackage annotation `
Import selector found
@Import(AutoConfigurationPackages.Registrar.class)
Click in AutoConfigurationPackages class
// Some problems of import initialization bean static class Registrar implements ImportBeanDefinitionRegistrar,
DeterminableImports { //metadata metadata @Override public void
registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry
registry) { //PackageImport Import metadata register(registry, new PackageImport(metadata)
.getPackageName()); } @Override public Set<Object> determineImports(
AnnotationMetadata metadata) { return Collections.singleton(new PackageImport(
metadata)); } }
go back to @EnableAutoConfiguration annotation
View another annotation @Import(AutoConfigurationImportSelector.class) Auto configure import selection
@Import(AutoConfigurationImportSelector.class)
Click in AutoConfigurationImportSelector have a look
notice AutoConfigurationImportSelector Class
public String[] selectImports(AnnotationMetadata annotationMetadata)
Select component : Is to choose which we configure pom.xml Something in
Click in loadMetadata
come AutoConfigurationMetadataLoader Class method
go back to AutoConfigurationImportSelector class
* Continue analysis selectImports method
* getCandidateConfigurations Get all configurations List<String> configurations =
getCandidateConfigurations(annotationMetadata, attributes);
* Core method
* getCandidateConfigurations Get candidate configuration
* loadFactoryNames First, load all through the loader protected List<String>
getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes
attributes) { List<String> configurations = SpringFactoriesLoader.
loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), 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; }
@SpringBootApplication Annotated @EnableAutoConfiguration
Explain one thing
@SpringBootApplication : Label this class as a SpringBoot Application of : All resources under the startup class are imported
Keep looking getCandidateConfigurations method
* This configuration is not empty
* META-INF/spring.factories
* Auto configured core file
see spring.factories file
spring.factories File content
* Initializers Initialized
* Application Listeners Monitored
* Auto Configuration Import Listeners Automatically select imported packages
* Auto Configuration Import Filters
* Auto Configure Auto configuration
* Failure analyzers
* Template availability providers
see spring.factories In file WebMvcAutoConfiuration
WebMvcAutoConfiuration class
WebMvcProperties class
spring.factories Where did the file come from ?
&emspl Read the configuration file first , Configuration class found
Keep looking getCandidateConfigurations method
Click in
come springFactoriesLoader class
see loadSpringFactories method
loadSpringFactories method Can see springboot Load all resources into the configuration class
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
click FACTORIES_RESOURCE_LOCATION constant
Loaded spring.factories Everything in the file
<>4 conclusion :
* SpringBoot All automatic configurations are scanned and loaded at startup ;
* All auto configuration classes are here spring.factories
* But it does not necessarily take effect , To determine whether the condition is true
* Only the corresponding start, There is a corresponding starter
* With the starter , Our automatic assembly will take effect , Then the configuration is successful !
* Springboot When starting , From classpath /META-INF/spring.factories Gets the specified value
* Import these autoconfiguration classes into the container , Automatic configuration will take effect , Help me with automatic configuration
* Things we used to need to configure automatically ,Springboot Did it for us
* integration Java2E , Solutions and autoconfiguration are all there spring-boot-autoconfigure-2.2.5.RELEASE.jar Under this bag
* It will import all the components that need to be imported , Return as class name , These components are added to the container
* There will also be a lot of in the container xxAutoConfiguration File (@Bean), It is these classes that import all the components required for this scenario into the container
@Configuration,JavaConfig!
* With autoconfiguration classes , It eliminates the need for us to write configuration files manually
Technology