Spring Boot multi-environment configuration
In actual development, applications usually need to run in different environments, such as development environment, test environment and production environment. Each environment may require different configurations, including database connections, log levels, interface addresses, and more. Spring Boot provides a variety of ways to handle multi-environment configurations. This article will introduce how to use Spring Boot to handle multi-environment configurations, and provide sample code to help readers better understand.
Basic principles of Spring Boot multi-environment configuration
The basic principle of Spring Boot multi-environment configuration is to use different configuration files to provide different configurations for different environments. Spring Boot uses application.properties
or application.yml
as the configuration file by default, but it also supports using different configuration files to provide different configurations for different environments. Specifically, Spring Boot supports the following configuration file naming conventions:
application-{profile}.properties
application-{profile}.yml
where {profile}
represents the name of the environment, such as dev
, test
, and prod
. When the application starts, Spring Boot will select the corresponding configuration file to load the configuration according to the current environment.
By default, Spring Boot uses application.properties
or application.yml
as the default configuration file. If you need to use other configuration files, you can specify them through the spring.profiles.active
attribute. For example, if you want to use application-dev.properties
or application-dev.yml
configuration files in the development environment, you can use application.properties
or Add the following configuration in application.yml
:
spring.profiles.active=dev
When the application starts, Spring Boot will automatically load the configuration in the application-dev.properties
or application-dev.yml
file. If no spring.profiles.active
property is specified, the configuration in the application.properties
or application.yml
file will be used by default.
Configure multi-environment configuration using YAML
YAML is an indentation-based format for representing data structures. In Spring Boot, multi-environment configurations can be configured using the YAML format. Here is an example YAML configuration file:
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost/mydb username: root password: password --- spring: profiles: dev datasource: url: jdbc:mysql://localhost/mydb_dev username: dev password: dev_password --- spring: profiles: prod datasource: url: jdbc:mysql://localhost/mydb_prod username: prod password: prod_password
In this sample YAML configuration file, three configuration blocks are defined, which are default configuration, development environment configuration and production environment configuration. In the default configuration, the port number and username and password for the database connection are defined. In the development environment configuration, the URL, username and password of the database connection are redefined. In the production environment configuration, the URL, username and password of the database connection are redefined.
Use Properties to configure multi-environment configuration
In addition to the YAML format, Spring Boot also supports the use of the Properties format to configure multi-environment configurations. Here is an example Properties configuration file:
server.port=8080 spring.datasource.url=jdbc:mysql://localhost/mydb spring.datasource.username=root spring.datasource.password=password --- spring.profiles=dev spring.datasource.url=jdbc:mysql://localhost/mydb_dev spring.datasource.username=dev spring.datasource.password=dev_password --- spring.profiles=prod spring.datasource.url=jdbc:mysql://localhost/mydb_prod spring.datasource.username=prod spring.datasource.password=prod_password
In this sample Properties configuration file, three configuration blocks are also defined, which are default configuration, development environment configuration and production environment configuration. In the default configuration, the port number and username and password for the database connection are defined. In the development environment configuration, the URL, username and password of the database connection are redefined. In the production environment configuration, the URL, username and password of the database connection are redefined.
Use command line parameters to configure multi-environment configuration
In addition to using configuration files, multi-environment configurations can also be configured via command line parameters. Spring Boot supports using the --spring.profiles.active
command-line parameter to specify the current environment. For example, the following command will start the application in the development environment:
$ java -jar myapp.jar --spring.profiles.active=dev
Use environment variables to configure multi-environment configuration
In addition to using configuration files and command-line arguments, environment variables can also be used to configure multi-environment configurations. Spring Boot supports using the SPRING_PROFILES_ACTIVE
environment variable to specify the current environment. For example, the following command will start the application in production:
$ export SPRING_PROFILES_ACTIVE=prod $ java -jar myapp.jar
Example code
Below is a sample code to configure a multi-environment configuration using YAML. In this sample code, a simple REST API is defined to get the configuration information of the current environment. You can get the configuration information of the current environment by accessing http://localhost:8080/env
.
@RestController public class EnvController { private final Environment environment; public EnvController(Environment environment) { this.environment = environment; } @GetMapping("/env") public Map<String, String> getEnv() { Map<String, String> env = new LinkedHashMap<>(); env.put("activeProfiles", Arrays.toString(environment.getActiveProfiles())); env.put("serverPort", environment.getProperty("server.port")); env.put("dataSourceUrl", environment.getProperty("spring.datasource.url")); env.put("dataSourceUsername", environment.getProperty("spring.datasource.username")); env.put("dataSourcePassword", environment.getProperty("spring.datasource.password")); return env; } }
In the application.yml
configuration file, three configuration blocks are defined, which are default configuration, development environment configuration and production environment configuration. In the default configuration, the port number and username and password for the database connection are defined. In the development environment configuration, the URL, username and password of the database connection are redefined. In the production environment configuration, the URL, username and password of the database connection are redefined.
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost/mydb username: root password: password --- spring: profiles: dev datasource: url: jdbc:mysql://localhost/mydb_dev username: dev password: dev_password --- spring: profiles: prod datasource: url: jdbc:mysql://localhost/mydb_prod username: prod password: prod_password
In the application.properties
configuration file, the port number of the default configuration and the username and password of the database connection are defined.
server.port=8080 spring.datasource.url=jdbc:mysql://localhost/mydb spring.datasource.username=root spring.datasource.password=password
In this sample code, the Environment
object is used to obtain the configuration information of the current environment. In the getEnv()
method, use the environment.getActiveProfiles()
method to get the current active environment, and use the environment.getProperty()
method to get it configuration information. For example, environment.getProperty("server.port")
will return the port number of the current environment.
Summary
This article describes how Spring Boot handles multi-environment configurations. Different configurations can be provided for different environments by using different configuration files, command line parameters, environment variables, etc. In actual development, multi-environment configuration is very important, which can help developers better manage the configuration of the application, thereby improving the maintainability and scalability of the application.
This article provides sample code for configuring a multi-environment configuration using YAML format and Properties format, and describes how to configure a multi-environment configuration using command-line parameters and environment variables. Through these sample codes, readers can better understand how Spring Boot handles multi-environment configurations and apply these techniques in actual development.
If you need a deeper understanding of Spring Boot’s multi-environment configuration, you can refer to the official Spring Boot documentation, or read related books and articles. I hope this article is helpful to readers, thank you for reading.
References
- Spring Boot Reference Guide. https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
- “Spring Boot – Profiles”. https://www.tutorialspoint.com/spring_boot/spring_boot_profiles.htm
- “Spring Boot – Externalized Configuration”. https://www.tutorialspoint.com/spring_boot/spring_boot_externalized_configuration.htm
- “Detailed Explanation of Spring Boot 2 Configuration Files”. https://www.cnblogs.com/jpfss/p/10041344.html
Attachment: complete sample code
application.yml
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost/mydb username: root password: password --- spring: profiles: dev datasource: url: jdbc:mysql://localhost/mydb_dev username: dev password: dev_password --- spring: profiles: prod datasource: url: jdbc:mysql://localhost/mydb_prod username: prod password: prod_password
application.properties
server.port=8080 spring.datasource.url=jdbc:mysql://localhost/mydb spring.datasource.username=root spring.datasource.password=password
EnvController.java
@RestController public class EnvController { private final Environment environment; public EnvController(Environment environment) { this.environment = environment; } @GetMapping("/env") public Map<String, String> getEnv() { Map<String, String> env = new LinkedHashMap<>(); env.put("activeProfiles", Arrays.toString(environment.getActiveProfiles())); env.put("serverPort", environment.getProperty("server.port")); env.put("dataSourceUrl", environment.getProperty("spring.datasource.url")); env.put("dataSourceUsername", environment.getProperty("spring.datasource.username")); env.put("dataSourcePassword", environment.getProperty("spring.datasource.password")); return env; } }
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies>
Remarks
The above sample code can be run in Spring Boot 2.x environment and uses MySQL database as an example. If it needs to run in other environments, it needs to be modified according to the actual situation.