Home  >  Article  >  Java  >  What is the configuration file format of SpringBoot?

What is the configuration file format of SpringBoot?

WBOY
WBOYforward
2023-05-11 08:37:051339browse

1 Spring Boot configuration file format

application.properties or application.yml, the main difference between them is the writing format.

1).properties:

springboot.user.name = testname

2).yml:

springboot: user : name: testname

In addition, the properties of the .properties format file are unordered, and the properties of the .yml format file are ordered, but it does not support the @PropertySource annotation to import the configuration.

2 Spring Boot’s core annotations

The annotation above the startup class is @SpringBootApplication, which is also the core annotation of Spring Boot. The main combinations include The following 3 annotations are added:

@SpringBootConfiguration: combines the @Configuration annotation to implement the function of the configuration file.

@EnableAutoConfiguration: Turn on the automatic configuration function, or turn off a certain automatic configuration option, such as turning off the data source automatic configuration function: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }).

@ComponentScan: Spring component scan.

3 How to enable Spring Boot features

1) Inherit the spring-boot-starter-parent project

2) Import spring-boot-dependencies Project dependencies

4 How to run Spring Boot

1) Directly execute the main method to run

2) Run with the Maven/Gradle plug-in

3) Run into a jar package and run directly through the java -jar command

5 How Spring Boot reads configuration

Spring Boot can pass @PropertySource, @Value, @Environment, @ConfigurationProperties to bind variables.

6 Monitor in Spring Boot

Spring Boot actuator is one of the important functions in the Spring Boot framework. Spring Boot monitor helps you access the current status of your running application in your production environment. For example, what beans are created, mappings in controllers, CPU usage, etc. Health and metrics can then be automatically collected and audited into your application.

7 The principle of Spring Boot automatic configuration

Add @SpringBootApplication or @EnableAutoConfiguration in the main method of the Spring program. It will automatically read the spring.factories file in each starter in maven, which configures all the beans that need to be created in the spring container.

Spring Boot scans the JAR packages that the project depends on at startup, looking for JARs containing spring.factories files

Loads the AutoConfigure class according to the spring.factories configuration

According to @Conditional Annotated conditions, automatic configuration and injection of beans into Spring Context

8spring-boot-maven-plugin

spring-boot-maven-plugin provides some jar-like The same commands as packaging or running the application.

run: Run your Spring Boot application.

repackage: Repackage your jar package or war package to make it executable

start and stop: manage the life cycle of the Spring Boot application, which can also be said to be for integration testing.

build-info: Generates construction information that can be used by the executor.

9 Use configuration files to configure the configuration of a specific environment through Spring Boot

For example, there are two configuration files application-dev.propertiesand application-prod.properties. Use spring.profiles.active=prod in application.properties to load the configuration of the application-prod.properties configuration file.

10How to disable a specific auto-configuration class

@SpringBootApplication(exclude= {Order.class})

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration .class})

@EnableAutoConfiguration(excludeName={Foo.class})

11 Loading of Spring Boot factory mode

Spring Framework uses a Factory Loading Mechanism. This mechanism is completed using SpringFactoriesLoader. SpringFactoriesLoader uses the loadFactories method to load and instantiate factories from the spring.factories files in the META-INF directory. These spring.factories files are found from the jar packages in the classpath.

The above is the detailed content of What is the configuration file format of SpringBoot?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete