Version used:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.5.0</version> </dependency>
Starter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
spring-boot-starter-==web==:
Spring boot starter: spring boot scenario initiator; Help us import the components that the web module depends on for normal operation;
Spring Boot extracts all functional scenarios and makes them into starters. It only needs to introduce these starters into the project, and all dependencies of related scenarios will be imported. You can import the initiator of any scene with any function you want
2. Main program class
/** * @SpringBootApplication To mark a main program class, indicating that this is a Spring Boot application */ @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { // Spring The application starts SpringApplication.run(HelloWorldMainApplication.class,args); } }
3. Process analysis
@SpringBoot application: the spring boot application label indicates that this class is the main configuration class of SpringBoot, and SpringBoot should run the main method of this 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 {
@Springbootconfiguration: configuration class of spring boot;
Marked on a class, indicating that it is a Spring Boot configuration class;
@ Configuration: mark this annotation on the Configuration class;
Configuration class - configuration file; The configuration class is also a component in the container@ Component
@EnableAutoConfiguration: enable the auto configuration function;
Spring Boot helps us configure things we need to configure before@ EnableAutoConfiguration tells SpringBoot to enable the auto configuration function; Only in this way can the automatic configuration take effect;
@AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {
@AutoConfigurationPackage: autoconfiguration package
@Import(AutoConfigurationPackages.Registrar.class):
Spring's bottom annotation @ Import imports a component into the container; The imported components are controlled by autoconfigurationpackages Registrar. class;
==Scan the package of the main configuration class (@ SpringBootApplication marked class) and all components in all sub packages below to the Spring container==
@Import(EnableAutoConfigurationImportSelector.class);
Import components into container?
EnableAutoConfigurationImportSelector: select which components to import;
Return all components to be imported as full class names; These components are added to the container;
Many automatic configuration classes (xxxAutoConfiguration) will be imported into the container; Import all the components needed for this scenario into the container and configure them;
AutoConfigurationImportSelector.class
@Override public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return NO_IMPORTS; } AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata); return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations()); }
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return EMPTY_ENTRY; } AnnotationAttributes attributes = getAttributes(annotationMetadata); List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
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; }
4. Analyze with HttpEncodingAutoConfiguration as column
// Indicates that this is a configuration class. Like the previously written configuration file, you can also add components to the container
@Configuration(proxyBeanMethods = false)
// Start the ConfigurationProperties function of the specified class; Bind the corresponding value in the configuration file with ServerProperties; And add httpencoding properties to the ioc container @EnableConfigurationProperties(ServerProperties.class)
// Spring bottom @ Conditional annotation (spring annotation version), according to different conditions, if the specified conditions are met, the configuration in the whole configuration class will take effect; Judge whether the current application is a web application. If so, the current configuration class will take effect @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
// Judge whether the current project has this class CharacterEncodingFilter; Filter for garbled code resolution in spring MVC; @ConditionalOnClass(CharacterEncodingFilter.class)// Determine whether there is a spring. In the configuration file http. encoding. enabled; If it does not exist, the judgment is also valid
// Even if pring. Is not configured in our configuration file http. encoding. Enabled = true, which is also effective by default;@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true) public class HttpEncodingAutoConfiguration { private final Encoding properties;
// When there is only one constructor with parameters, the value of the parameter will be taken from the container public HttpEncodingAutoConfiguration(ServerProperties properties) { this.properties = properties.getServlet().getEncoding(); }
// Add a component to the container. Some values of this component need to be obtained from properties @Bean
// Determine that the container does not have this component? @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE)); return filter; }
Determine whether this configuration class is effective according to different current conditions?
Once the configuration class takes effect; This configuration class will add various components to the container; The properties of these components are obtained from the corresponding properties classes, and each property in these classes is bound to the configuration file;
All properties that can be configured in the configuration file are encapsulated in the xxxproperties class; The attribute class corresponding to a function can be referenced for what can be configured in the configuration file
// Get the specified value from the configuration file and bind it with the properties of the bean
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties { /** * Server HTTP port. */ private Integer port; /** * Network address to which the server should bind. */ private InetAddress address;
5. Summary:
1) spring boot will load a large number of automatic configuration classes
2) let's see if the functions we need have the automatic configuration class written by SpringBoot by default;
3) let's see which components are configured in this automatic configuration class; (as long as the components we want to use are, we don't need to configure them again)
4) when adding components to the automatic configuration class in the container, some properties will be obtained from the properties class. We can specify the values of these attributes in the configuration file;
Source: Shang Silicon Valley course