Spring Boot configuration file details

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=valueformat 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.ymlSame 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:

  1. classpath:/config/configuration files in the directory.
  2. classpath:/Configuration files in the root path.
  3. config/directory under the current working directory .
  4. current working directory.

In addition to the default location, we can also specify other configuration file locations by specifying spring.config.locationthe attribute .

Priority of configuration properties

In Spring Boot, the precedence of configuration properties is as follows:

  1. Command line parameters: The parameters passed through the command line will override the property values ​​​​of other configuration methods.
  2. System properties: System properties can be set by using -Dparameters , and they override properties in the configuration file.
  3. Environment variables: Properties in environment variables override properties in configuration files.
  4. 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 @ConfigurationPropertiesannotations , 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, @ConfigurationPropertiesthe annotation binds the configuration properties myappprefixed with to MyAppPropertiesthe corresponding properties in the class.

Use configuration properties

Using configuration properties in your application is very simple. We can inject configuration properties through @Valueannotations or directly in code.

Here is an example of using @Valueannotation injection to configure properties:

@Value("${myapp.name}")
private String appName;

In the above example, @Valuethe annotation injects the value myapp.nameof into appNamethe 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.

Guess you like

Origin blog.csdn.net/run65536/article/details/131320634