SoFunction
Updated on 2025-04-13

A comprehensive guide to switching SpringBoot configuration files

1. Overview of Spring Boot Configuration Files

Spring Boot supports multiple types of configuration files, most commonly and. These configuration files are used to store various configuration properties of the application, such as database connection information, server ports, log levels, etc.

(I) properties files

In, the configuration properties are presented as key-value pairs, for example:

=8080
=jdbc:mysql://localhost:3306/mydb
=root
=123456

(II) yml file

Use indents and colons to represent hierarchical relationships for better readability. The same configuration can be written like this:

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: 123456

2. Multi-environment configuration files

In order to achieve configuration distinction between different environments, Spring Boot allows us to create multiple configuration files. The usual naming method is application-{profile}.properties or application-{profile}.yml, where {profile} represents different environments, such as dev (development environment), test (test environment), and prod (production environment).

(I) Example of creating multi-environment configuration files

Suppose we have three environments: development, testing and production, and the corresponding configuration files are as follows:

1.

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://dev-db:3306/mydb
    username: devuser
    password: devpass

2.

server:
  port: 8082
spring:
  datasource:
    url: jdbc:mysql://test-db:3306/mydb
    username: testuser
    password: testpass

3.

server:
  port: 80
spring:
  datasource:
    url: jdbc:mysql://prod-db:3306/mydb
    username: produser
    password: prodpass

3. Configuration file switching method

(I) Pass the command line parameters

When starting a Spring Boot application, you can specify the configuration file to be activated via the -- parameter. For example, use the command line to start the application and activate the development environment configuration:

java -jar  --=dev

(II) Through system attributes

Set system properties in the script that starts the application. For example, in a Linux environment:

export JAVA_OPTS="-=dev"
java $JAVA_OPTS -jar 

(III) Configure in or

You can also set the default activated profile in the main configuration file or in the main configuration file. In:

=dev

In:

spring:
  profiles:
    active: dev

4. Configuration file priority

When multiple configuration files exist and all contain the same properties, Spring Boot will load the configuration according to certain priorities. The priority order is as follows:

  • Configuration properties in command line parameters.
  • Properties in SPRING_APPLICATION_JSON (for example, configuration in JSON format set through environment variables or system properties).
  • ServletConfig initialization parameters.
  • ServletContext initialization parameters.
  • JNDI attribute in java:comp/env.
  • System properties returned by ().
  • The environment variable returned by ().
  • random.* attribute generated by RandomValuePropertySource.
  • The application-{profile}.properties or property in the file, where {profile} is the activated configuration file.
  • or properties in the file.

Understanding configuration file priorities helps us correctly handle configuration conflicts in actual development.

5. Dynamically switch configuration files

In some scenarios, we may want to dynamically switch configuration files while the application is running. Spring Cloud Config provides the capability to centrally manage configuration files and support real-time refresh of configurations. By combining Spring Cloud Bus, it is also possible to implement configuration broadcast updates, allowing multiple instances to obtain the latest configuration at the same time.

(I) Introduce dependencies

Add dependencies for Spring Cloud Config and Spring Cloud Bus in:

<dependency>
    <groupId></groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

(II) Configure the client

Config Server's address and configuration file to be retrieved in or:

spring:
  application:
    name: myapp
  cloud:
    config:
      uri: http://config-server:8888
      fail-fast: true
      retry:
        max-attempts: 5
        initial-interval: 1000
        multiplier: 1.5

(III) Enable configuration refresh

Add the @RefreshScope annotation on the controller or service class that needs to be refreshed, and then trigger the configuration refresh by sending a POST request to the /actuator/refresh endpoint. For example:

import ;
import ;
import ;
import ;
@RestController
@RefreshScope
public class ConfigController {
    @Value("${}")
    private String configProperty;
    @GetMapping("/config")
    public String getConfig() {
        return configProperty;
    }
}

6. Summary

Spring Boot's configuration file switching mechanism provides great convenience for us to manage application configurations in different environments. By using multiple environment profiles rationally, choosing a flexibly switch method, and understanding configuration priorities, we can efficiently develop and deploy stable and reliable Spring Boot applications. At the same time, the ability to dynamically switch configuration files further improves the flexibility and maintainability of applications. I hope this article can help you understand and proficiently use Spring Boot's configuration file switching function.

The above is the detailed content of the comprehensive guide to switching SpringBoot configuration file. For more information about switching SpringBoot configuration file, please follow my other related articles!