spring dependencies help us rely on many commonly used jar packages. Importing these jar packages does not require a version number
For example:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
Configuration file configuration debug: true can print the automatic configuration report on the console. You can print all started automatic configuration and unstarted automatic configuration classes.
@SpringBootApplication
Marked in a certain class, indicating that this class is the main startup class of springboot.
@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 {
@EnableAutoConfiguration: Enable automatic configuration, so we don’t have to do a lot of configuration manually
@AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {
@ AutoConfigurationPackage
Enter the main configuration class All components under the package will be scanned into the spring container.
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({Registrar.class}) public @interface AutoConfigurationPackage {
AutoConfigurationImportSelector
Use @import: to import a component into the container, which will load all automatic configuration classes, such as mysql, web, etc. Wait
Finally, we will go to META-INF/spring.factories to find all the automatic configuration classes and load them into the container. These automatic configuration classes have done a lot of the configuration we used to do with spring.
Literal
Strings do not need to be quoted by default, single quotes and double quotes have special purposes
Single quotes are special Escape, such as \n output or \n
double quotation mark special characters will not be escaped, such as \n output is a space
will be escaped the same as adding single quotes
Loose binding
The way to write attributes in camel case is the same as adding a dash - or underline_. When converted to entity classes, they are all camel case. However, this can only be used in configurationProperties and cannot be used. Using the
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
annotation in the @Value annotation can allow the custom configuration in the yaml configuration to have prompts
@PropertySource annotation together with the @PropertySource annotation You can load other specified files
@PropertySource(value = "classpath:user.properties")
Import the spring configuration file and make it take effect
@ImportResource(locations={"classpath:mybatis.xml"})
${random.int} Use the random number provided by yaml
${server.port} Use the previously configured value
${server.name: Hello} If there is no value, use the default value
Activate to specify different configuration environments
Command line activation can add –spring.profiles.active=dev
Virtual machine parameters Activate -Dspring.profiles.active=dev
file: ./config/ config directory under the project root path
file: ./ project Root directory
classpath: config/
classpath: /
All files will be loaded, with priorities from top to bottom, from high to low, with higher ones overwriting lower ones. Different configurations will take effect. , complementary.
You can also change the configuration file location through –spring.config.location when deploying the project. The configuration file loaded in the project is complementary to the configuration file specified here.
The above is the detailed content of Analysis of the principles of Springboot-yaml configuration and automatic configuration. For more information, please follow other related articles on the PHP Chinese website!