Boot load auto configuration
@SpringBootApplication analysis
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} )
ssdss@SpringBootApplication = @SpringBootConfiguration➕@ComponentScan➕@EnableAutoConfiguration
s ddsss1. @SpringBootConfiguration: represents a configuration class
s ddsss2. @ComponentScan: specify which to scan. Refer to the Spring annotation
s ddsss3. @EnableAutoConfiguration = @AutoConfigurationPackage ➕ @ Import (key)
@AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; Class<?>[] exclude() default {}; String[] excludeName() default {}; }
ss @EnableAutoConfiguration core note 1: @ AutoConfigurationPackage
Role of sdssdss: make default package rules and automatically configure packages
@Import({Registrar.class}) ❤🧡Import into container Registrar assembly public @interface AutoConfigurationPackage { String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; } 💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦💦 Registrar Source code: static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports { Registrar() { } public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0])); } public Set<Object> determineImports(AnnotationMetadata metadata) { return Collections.singleton(new AutoConfigurationPackages.PackageImports(metadata)); } } 💖💖 Incoming parameters: AnnotationMetadata metadata ==> Annotation source information ==> @AutoConfigurationPackage On which class is it marked💖💖
sdssdss breakpoint debugging:
ddasdsdsssd obtains the package name according to the metadata, and then registers all components in the package. The package name defaults to the package where the main program class is located
AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)). getPackageNames().toArray(new String[0]));
sdsssd summary:
sds dsssd1.AutoConfigurationPackage uses Register to import a series of components into the container
sddssssd 2. Import all components under the specified package: the default is under the package of MainApplication
ss @EnableAutoConfiguration core note 2: @ Import(AutoConfigurationImportSelector.class)
public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!this.isEnabled(annotationMetadata)) { return NO_IMPORTS; } else { AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata); return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations()); } }
sdssdss1.getAutoConfigurationEntry(annotationMetadata); Import some components into the container
sdssdss2. Click getAutoConfigurationEntry and call list < string > configurations = getcandidateconfigurations (annotationmetadata, attributes); Get all the configuration classes that need to be imported into the container, a total of 131
sdssdss3. Click getCandidateConfigurations(). It is found that the factory is used to load map < string, list < string > > loadspringfactories (classloader, classloader); Get all components
sdssdss4. Load a file from META-INF/spring.factories
Sdssdss scans all META-INF/spring.factories in our current system by default
There is also META-INF/spring.factories in the sdsspring-boot-autoconfigure-2.5.6.release.jar package
Sdssdss [note]: this file is written to death. As soon as springboot starts, all configuration classes to be loaded into the container are required
Turn on automatic configuration items on demand
sdsssd although all automatic configurations of 131 scenarios are loaded by default when started, they will be configured on demand through Conditional assembly rules (@ Conditional).
Modify default configuration
@Bean( name = {"multipartResolver"} ) @ConditionalOnMissingBean({MultipartResolver.class}) //There is no component with this name multipartResolver in the container public StandardServletMultipartResolver multipartResolver() { StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily()); //If the method labeled @ Bean passes in an object parameter, the value of this parameter will be found from the container. //SpringMVC multipartResolver. Prevent the file upload parser configured by some users from not conforming to the specification // Detect if the user has created a MultipartResolver but named it incorrectly return multipartResolver; } Add a file upload parser to the container;
sdsssdSpringBoot will configure all components at the bottom by default. However, if the user has configured it himself, the user's priority shall prevail.
Summary:
sdsssd1 and SpringBoot load all the automatic configuration classes xxxxxxautoconfiguration first
sdsssd2. Each automatic configuration class takes effect according to conditions. By default, it will bind the value specified in the configuration file. Take it from xxproperties. xxxProperties is bound to the configuration file
For example: @ EnableConfigurationProperties(WebMvcProperties.class)
sdsssd3, the effective configuration class will assemble many components in the container
sdsssd4. As long as these components are in the container, they are equivalent to these functions
sdsssd5, customized configuration = = "as long as the user has its own configuration, the user's configuration takes precedence.
sdssdssd5.1 • users directly replace the underlying components with @ Bean
sddssssd5.2 • users can modify the value of the configuration file obtained by this component in application.properties.
technological process:
Sdsssdxxxxconfiguration auto import component = = = "component gets value from xxxproperties = = =" xxxproperties gets property value from application.properties
SpringBoot development steps
sdsssd1. Introduce scenario dependency. There are two methods: sdsssd • view the underlying sdsssd • view the document:
sdsssd2. View which are automatically configured (optional)
Sdsssd • after self analysis, the automatic configuration corresponding to the introduced scenario generally takes effect
sddssssd • debug=true in the application.properties configuration file enables the automatic configuration report to check whether the configuration is effective.
sdsssd3. Need to modify
Sdsssd • modify the configuration item by referring to the document
Sdsssd • analyze and modify configuration items by yourself. Which properties of the configuration file are bound by xxxproperties
Sdsssd • custom add or replace components
sdsddssssd• @Bean,@Componen
Sddssssd • customizer xxxxxxcustomizer;