Spring Boot core configuration file


Spring Boot is a framework based on the Spring framework for rapid development, simplified configuration, and convention over configuration. In a Spring Boot application, the core configuration file is application.propertiesor application.yml, both of which can be used to configure the application's properties and environment.

1、application.properties

application.properties It is a configuration format based on property files and supports key-value pairs in the form of key-value. In Spring Boot, various application properties such as port number, database connection, log level, etc. can be configured in the application.properties file.

Here is an example application.propertiesfor :

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
logging.level.org.springframework=DEBUG

In the above example, the port number of the application is configured as 8080, the database connection is MySQL database, and the log level is DEBUG.

2、application.yml

application.ymlIt is a configuration file based on YAML format, which supports the configuration form of hierarchical structure, and can express the relationship between configuration items more clearly. More readable, maintainable, and flexible than application.properties.application.yml

Here is an example application.ymlfor :

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: root
logging:
  level:
    org.springframework: DEBUG

In the above example, it is the same as the application.properties sample configuration item, but uses the hierarchical structure in YAML format. In YAML, indentation is used to represent hierarchical relationships. For example, spring.datasource.url is a child of spring, datasource is a child of spring, and url is a child of datasource.

Recommendations

Try to use application.yml, because it is more flexible, easy to read, and easy to maintain; it can be used for simple configuration items; application.propertieswhen writing configuration files, pay attention to the use of indentation and spaces, which are important features of the YAML format; In the file, you can use the @Value annotation to inject configuration properties, or use @ConfigurationPropertiesto inject configuration properties in batches.

3. Common configuration items

The following are some commonly used Spring Boot configuration items:

server configuration

server.port: The port number that the application monitors, the default is 8080;
server.servlet.context-path: The context path of the application, the default is empty;
server.tomcat.access-log-enabled: Whether to enable the access log, the default is false.

database configuration

spring.datasource.url: database connection URL;
spring.datasource.username: database user name;
spring.datasource.password: database password;
spring.datasource.driver-class-name: database driver class name;
spring.datasource.hikari. *: Configuration item of Hikari connection pool.

log configuration

logging.level.*: Set the log level, you can set different log levels for different packages;
logging.file.name: specify the log file name;
logging.file.path: specify the log file path;
logging.pattern.console: set Console log output format;
logging.pattern.file: Set the file log output format.

other configuration

spring.profiles.active: Specifies the active profile of the application, such as dev, prod, etc.;
spring.main.allow-bean-definition-overriding: Whether to allow Bean overriding, the default is false;
spring.main.banner-mode: Set Banner mode displayed at startup, such as off, console, log, etc.

4. Loading order of configuration files

In Spring Boot, configuration files can be loaded in various ways, including:

  • application.properties or application.yml under classpath:/;
  • application.properties or application.yml under classpath:/config/;
  • External configuration files such as /etc/myapp/application.properties or file:///opt/myapp/application.yml.

The order in which these configuration files are loaded is:

  • classpath:/config/application.yml(或 .properties);
  • classpath:/application.yml(或 .properties);
  • External configuration files.

Among them, the order of 1 and 2 can be reversed, that is, load classpath:/application.yml (or .properties) first, and then load classpath:/config/application.yml (or .properties).

If there are multiple configuration files at the same time, they will be merged in the above order, and the later configuration will override the previous configuration.

5. Placeholders for configuration files

In configuration files, placeholders of the form ${…} can be used to refer to the values ​​of other configuration properties. For example:

server:
  port: 8080
myapp:
  name: myapp
  version: 1.0
  greeting: Hello, ${myapp.name} ${myapp.version}!

In the example above, the myapp.greeting property uses the ${myapp.name} and ${myapp.version} placeholders to refer to the values ​​of other properties.

6. Dynamic refresh of configuration files

In a Spring Boot application, if you want to refresh configuration files dynamically, you can use the @RefreshScope annotation. This annotation will automatically update the Bean injected with this annotation when the configuration file changes.

@RestController
@RefreshScope
public class MyController {
    @Value("${myapp.greeting}")
    private String greeting;

    @GetMapping("/greeting")
    public String greeting() {
        return greeting;
    }
}

In the above example, the @RefreshScope annotation is used to dynamically refresh the value of the greeting attribute. When the myapp.name or myapp.version properties change, the value of the greeting property also changes.

7. Attribute grouping of configuration files

In actual development, the configuration file of the application may become very complicated, and the number of attributes will also be large. In order to better organize and manage properties, you can use Spring Boot's property grouping feature.

Define attribute groupings

In configuration files, you can use the @ConfigurationProperties annotation to define property groupings. For example:

@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
    
    
    private String name;
    private String version;
    // getters and setters
}

In the above example, a property group named myapp is defined using @ConfigurationProperties annotation, and two properties named name and version are declared. In the application.yml file, the values ​​of these two properties can be accessed through myapp.name and myapp.version.

Binding property grouping

In Spring Boot, property groups can be bound via @EnableConfigurationProperties or @ConfigurationPropertiesScan annotations. For example:

@SpringBootApplication
@EnableConfigurationProperties(MyAppProperties.class)
public class MyAppApplication {
    
    
    // ...
}

In the above example, the @EnableConfigurationProperties annotation is used to bind the MyAppProperties property group.

Use attribute grouping

In an application, groups of properties can be injected using the @Autowired or @Inject annotations. For example:

@RestController
public class MyController {
    
    
    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/info")
    public MyAppProperties info() {
    
    
        return myAppProperties;
    }
}

In the above example, the MyAppProperties attribute group is injected using the @Autowired annotation, and the value of the attribute group is returned in the info interface.

Summarize

In Spring Boot, attributes in configuration files can be organized and managed by attribute grouping. Use @ConfigurationPropertiesto define property groups, use @EnableConfigurationPropertiesor @ConfigurationPropertiesScanannotations to bind property groups, and use @Autowiredor @Inject annotations to inject property groups in the application. By properly using attribute grouping, the configuration of the application can be made clearer, easier to maintain, and easier to expand.

Guess you like

Origin blog.csdn.net/qq_54351538/article/details/129681549