The principle of automatic configuration
Derived annotation
How to configure the configuration file
server.port
spring.config.location=”D://application.properties”
Automatic Configuration principle succinctly
- The main configuration class is loaded when Spring Boot starts , and the automatic configuration function is enabled @EnableAutoConfiguration
View A utoConfigurationImportSelector.class
Enter G ETc and idateConfigurations ()
Enter loadFactoryNames ()
Enter loadSpringFactories ()
to sum up
- SpringBoot will start automatically configured to load a lot of class
-We see whether the functions we need are automatically configured by SpringBoot by default;
-Let's take a look at which components are configured in this auto-configuration class; (as long as there are components we want to use, we don't need to configure them again)
-When adding components to the auto-configuration class in the container, certain properties are obtained from the properties class. We can specify the values of these attributes in the configuration file;
-xxxxAutoConfigurartion: automatic configuration class; add components to the container
-xxxxProperties: the default configuration in the package configuration file
@Conditional derived annotation
Does the java version of @ConditionalOnJava system meet the requirements
@ConditionalOnBean The specified bean exists in the container;
@ConditionalOnMissingBean The specified bean does not exist in the container;
@ConditionalOnExpression satisfies SpEL expression specification
@ConditionalOnClass There is a specified class in the system
@ConditionalOnMissingClass There is no specified class in the system
@ConditionalOnSingleCandidate There is only one specified Bean in the container, or this Bean is the preferred Bean
@ConditionalOnProperty Whether the specified property in the system has the specified value
@ConditionalOnResource Whether the specified resource file exists under the class path
@ConditionalOnWebApplication is currently a web environment
@ConditionalOnNotWebApplication is not currently a web environment
@ConditionalOnJndi JNDI has the specified item
The relationship between all conditional configurations is and
How do we know which auto-configuration classes are in effect?
We can enable the debug = true attribute (configured in the configuration file);
Let the console print the automatic configuration report, so that we can easily know which automatic configuration classes take effect;