Concept
In the process of learning spring boot, the most contact is starter. You can think of starter as a service--making it possible for a developer using a feature to not focus on the processing of a variety of dependent libraries, without the need for specific configuration information, by spring boot automatically discovers the required beans through classes under the Classpath path, and weaves the bean
Steps
Introduction of Spring Boot configuration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--Optional Dependencies--
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<optional>true</optional>
</dependency>
<!--@ConfigurationProperties annotation processing (metadata for IDEs)--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Add an automatic configuration file
@org. Springframework.context.annotation.configuration@conditionalonclass ({ Sqlsessionfactory. class , Sqlsessionfactorybean. class }) @ConditionalOnBean (DataSource. class ) @EnableConfigurationProperties (mybatisproperties. class ) @AutoConfigureAfter (datasourceautoconfiguration. class ) public Class mybatisautoconfiguration {}
Analysis
The regular starter is a standalone project, and then the new warehouse in MAVEN is registered for release, and other developers can use your starter.
The common starter will include the following:
- An automatic configuration file that determines whether or not to perform an automatic configuration of the feature, depending on whether the specified class exists for the classpath.
- Spring.factories, it is important to instruct spring boot to locate the specified automatic configuration file.
- Endpoint: Can be understood as an admin, including the description of the service, interface, interaction (Business information query)
- Heath Indicator: Health metrics for the services provided by the starter
During application startup, Spring boot uses the Springfactoriesloader class loader to find the Java configuration file that corresponds to the Org.springframework.boot.autoconfigure.EnableAutoConfiguration keyword. Spring boot iterates through the spring.factories file in the Meta-inf directory of each jar package and builds it into a list of profiles. In addition to the configuration file that corresponds to the enableautoconfiguration keyword, there are other types of configuration files:
- Org.springframework.context.ApplicationContextInitializer
- Org.springframework.context.ApplicationListener
- Org.springframework.boot.SpringApplicationRunListener
- Org.springframework.boot.env.PropertySourceLoader
- Org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider
- Org.springframework.test.contex.TestExecutionListener
Attention
@ConditionalOnMissingBean function is: only the corresponding Bean is not created in the system, its decorated initialization code block will be executed, the user manually created by the bean first ;
How does Spring Boot starter Find automatic configuration files (files such as xxxxautoconfiguration)?
- Spring.factories: The class in the Classpath directory triggered by Spring boot is detected and configured automatically;
- @Enable: Sometimes the process of finding an automatic configuration file is triggeredby a user of starter .
Spring Spring Boot: Customizing your Own starter