Spring Boot configuration file details
introduction
Configuration files play a vital role when developing a Spring Boot application. Spring Boot provides a variety of configuration files for configuring various properties and behaviors of the application. This article will introduce the use and related features of Spring Boot configuration files in detail.
Type of configuration file
Spring Boot supports the following types of configuration files:
- application.properties : A configuration file based on attribute key-value pairs, using a simple
key=value
format with high readability. - application.yml : A configuration file based on YAML format, which uses indentation and colons to indicate the hierarchy of attributes, which is more readable.
- application.yaml :
application.yml
Same as , but with a different file extension. - application.{profile}.properties or application-{profile}.properties : property configuration files for different configuration environments (such as development, testing, production).
- application.{profile}.yml or application-{profile}.yml : YAML format configuration files for different configuration environments.
The location of the configuration file
Spring Boot will automatically load the default configuration file at startup, usually located in the following locations:
classpath:/config/
configuration files in the directory.classpath:/
Configuration files in the root path.config/
directory under the current working directory .- current working directory.
In addition to the default location, we can also specify other configuration file locations by specifying spring.config.location
the attribute .
Priority of configuration properties
In Spring Boot, the precedence of configuration properties is as follows:
- Command line parameters: The parameters passed through the command line will override the property values of other configuration methods.
- System properties: System properties can be set by using
-D
parameters , and they override properties in the configuration file. - Environment variables: Properties in environment variables override properties in configuration files.
- Configuration file: The properties in the configuration file provide default values, but will be overridden by the property values in the above ways.
Use of configuration properties
In the configuration file, we can set various properties to configure the behavior of the application. Here are some examples of commonly used configuration properties:
# 设置服务器端口
server.port=8080
# 设置日志级别
logging.level.com.example=DEBUG
# 配置数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
# 配置缓存
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379
The above examples show how to set configuration properties for server port, log level, database connection and cache.
Custom configuration properties
In addition to using the configuration properties provided by Spring Boot by default, we can also customize our own configuration properties. By using @ConfigurationProperties
annotations , we can bind properties to custom Java classes.
The following is an example of a custom configuration property:
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
private String name;
private String version;
// getters and setters
}
In the above example, @ConfigurationProperties
the annotation binds the configuration properties myapp
prefixed with to MyAppProperties
the corresponding properties in the class.
Use configuration properties
Using configuration properties in your application is very simple. We can inject configuration properties through @Value
annotations or directly in code.
Here is an example of using @Value
annotation injection to configure properties:
@Value("${myapp.name}")
private String appName;
In the above example, @Value
the annotation injects the value myapp.name
of into appName
the field.
in conclusion
Spring Boot's configuration files provide a flexible and powerful way to configure every aspect of your application. By understanding the different types of configuration files, the priority and usage of configuration properties, developers can better grasp and configure the behavior and properties of Spring Boot applications.