Early
Spring
projects need to add cumbersome configurationsxml
, such asMVC
, transactions, database connections and other cumbersome configurations.Spring Boot
With the emergence of , there is no need for these cumbersome configurations, becauseSpring Boot
based on the concept that conventions are greater than configurations , when the project starts, the agreed configuration classes are automatically configured intoIOC
the container. These are all due toSpring Boot
the automatic configuration feature.
How Spring Boot implements automatic configuration
Spring Boot
All need to create a mian
startup class, and the startup class contains @SpringBootApplication
annotations, from the startup class, step by step to explore the source code.
@SpringBootApplication annotation
Spring Boot
There is an annotation on the startup class @SpringBootApplication
:
@EnableAutoConfiguration annotation
@SpringBootApplication
@EnableAutoConfiguration
Notes inside :
AutoConfigurationImportSelector类
@EnableAutoConfiguration
Annotation import AutoConfigurationImportSelector
class:
selectImports() method
AutoConfigurationImportSelector
The class finds selectImports
the method, which has getAutoConfigurationEntry
the method:
SpringFactoriesLoader.loadFactoryNames() 方法
getAutoConfigurationEntry
method by SpringFactoriesLoader.loadFactoryNames()
scanning all packages META-INF/spring.factories
containing :jar
spring-boot-autoconfigure-xxx.jar
The project includes META-INF/spring.factories
a file, spring.factories
which is in the form of a key-value pair, and scans the @EnableAutoConfiguration
corresponding class under the file:
Automatic configuration is mainly implemented
@EnableAutoConfiguration
by adding@EnableAutoConfiguration
annotations and importing classes. The methodsAutoConfigurationImportSelector
in it scan all the included packages and assemble all the classes corresponding to the full name of the annotations into the container.selectImports
SpringFactoriesLoader.loadFactoryNames()
META-INF/spring.factories
jar
key
@EnableAutoConfiguration
value
IOC
Debug verification
Turn on Debug
the debug mode, set a breakpoint in getCandidateConfigurations
the method SpringFactoriesLoader.loadFactoryNames()
, and view the returned configurations
collection:
The first element is tk.mybatis.mapper.autoconfigure.MapperAutoConfiguration
due to the introduced 通用mapper
dependency:
Principle of automatic configuration
Principle process summary
From the source code viewed above, we can know that Spring Boot
the automatic configuration is mainly @EnableAutoConfiguration
realized by @EnableAutoConfiguration
annotating imported AutoConfigurationImportSelector
classes, scanning all packages containing files through selectImports
method calls , and injecting the corresponding classes in the files into the container.SpringFactoriesLoader.loadFactoryNames()
META-INF/spring.factories
jar
spring.factories
@EnableAutoConfiguration
IOC
After these attributes are automatically configured , IOC
there is no need to manually configure them . The idea in the convention is to add the required configuration to the container in an agreed manner.bean
Spring Boot
约定大于配置
IOC
Conditions for automatic configuration to take effect
Does that mean that spring.factories
the configuration corresponding to the file will be loaded into IOC
the container? For example, the following Kafka
automatic configuration class:
@Configuration
@ConditionalOnClass(KafkaTemplate.class)
@EnableConfigurationProperties(KafkaProperties.class)
@Import({ KafkaAnnotationDrivenConfiguration.class, KafkaStreamsAnnotationDrivenConfiguration.class })
public class KafkaAutoConfiguration {
private final KafkaProperties properties;
private final RecordMessageConverter messageConverter;
public KafkaAutoConfiguration(KafkaProperties properties, ObjectProvider<RecordMessageConverter> messageConverter) {
this.properties = properties;
this.messageConverter = messageConverter.getIfUnique();
}
@Bean
@ConditionalOnMissingBean(KafkaTemplate.class)
public KafkaTemplate<?, ?> kafkaTemplate(ProducerFactory<Object, Object> kafkaProducerFactory,
ProducerListener<Object, Object> kafkaProducerListener) {
....
}
There are several annotations in it:
@ConditionalOnClass
@ConditionalOnMissingBean
@ConditionalOnClass
Indicates that the configuration class will only be configured if there is a class in the class path. This configuration class will be automatically configured only when the relevant dependencies of the " Hospital Certificate Template " are introduced.@ConditionalOnMissingBean
Indicates that only if the corresponding class does not exist,bean
the class will be automatically configured.
So spring.factories
not all of them bean
will be assembled into IOC
the container, and only the corresponding ones will be configured on demand bean
.
Summarize
Spring Boot
Principle of automatic configuration- 1.
@EnableAutoConfiguration
Annotate the importedAutoConfigurationImportSelector
class. - 2. Execute
selectImports
the method call toSpringFactoriesLoader.loadFactoryNames()
scan alljar
the correspondingMETA-INF/spring.factories
files below. - 3. Defined as
@EnableAutoConfiguration
correspondingvalue
, assemble these assembly conditions intoIOC
the container.
- 1.
bean
Simply speaking, automatic assembly is to automatically load third-party componentsIOC
into the container without writingbean
related configurations. It conforms to the concept of agreement rather than configuration .Spring Boot
Based on the idea that convention is greater than configuration , if there is no additional configuration for configuration, the default configuration will use the agreed default value, and configure it in theIOC
container according to the agreement, without the need for developers to manually add configuration, speeding up development efficiency.