Implement multi-environment configuration in Spring Boot
In the actual development process, we usually encounter multiple different operating environments, such as development environment, test environment, production environment, etc. Each environment may have different configuration requirements, such as database connections, log levels, etc. Spring Boot provides a very simple way to implement multi-environment configuration, which makes it easier for us to load the corresponding configuration files according to different environments.
This article will explain in detail how to set up and manage multi-environment configurations in Spring Boot projects.
1. Create a multi-environment configuration file
In Spring Boot, configuration files are usually placed insrc/main/resources
In the directory, and the default file name isor
. To support multi-environment configuration, we can create multiple configuration files, each for a specific environment.
Example:
src/main/resources ├── # Default configuration file├── #Development environment configuration file├── # Test environment configuration file├── # Production environment configuration file
in,It is the default configuration file, and other files are configured for different environments.
2. Define environment variables in configuration file
Each configuration file can contain configurations of different environments, such as database connection information, log levels, URLs of external services, etc. We can、
and
Different configurations are defined in .
(Default configuration)
spring: application: name: myapp profiles: active: dev # Develop environment configuration is enabled by default
(Development Environment)
spring: datasource: url: jdbc:mysql://localhost:3306/dev_db username: dev_user password: dev_password jpa: hibernate: ddl-auto: update logging: level: root: debug
(Test environment)
spring: datasource: url: jdbc:mysql://localhost:3306/test_db username: test_user password: test_password jpa: hibernate: ddl-auto: validate logging: level: root: info
(Production Environment)
spring: datasource: url: jdbc:mysql://localhost:3306/prod_db username: prod_user password: prod_password jpa: hibernate: ddl-auto: none logging: level: root: warn
In the above example, different database connection information and log levels are used in different environments. For example, debug logs are enabled in the development environment (debug
), while the production environment only records warnings (warn
) and above logs.
3. Activate different environment configurations
Spring Boot allows us to activate different environment configurations in a variety of ways:
3.1. Pass command line parameters
When you start the application using the command line, you can use--
Parameters to specify the configuration environment to use. For example:
$ java -jar --=prod
This will activate the production environment configuration ()。
3.2. Through environment variables
You can also specify the activated configuration environment by setting system environment variables:
export SPRING_PROFILES_ACTIVE=prod
Then when the app is started, Spring Boot will automatically loadconfiguration in.
3.3. Specify in
If you don't want to switch the environment through the command line or environment variables, you can also directlySpecify the default activation environment in:
spring: profiles: active: dev
in this case,Will be loaded by default.
4. Priority for multi-environment configuration
In Spring Boot, the priority of multiple configuration files is loaded in the following order:
- Command line parameters: The configuration specified via the command line has the highest priority.
-
Configuration in:
Will load first, then according to
Load the corresponding configuration file.
-
Environment variables and system properties: Specified in environment variables and system properties
The default configuration will be overridden.
Notice:
or
The public configuration in the first place will be loaded, and the configuration files related to the specific environment (e.g.
) will overwrite the same part of the common configuration.
5. Use @Profile annotation to distinguish environment
In addition to switching environments through configuration files, Spring Boot also supports@Profile
Annotation to load beans in a specific environment. For example, you can define different data source beans for different environments.
@Configuration public class DataSourceConfig { @Bean @Profile("dev") public DataSource devDataSource() { return () .url("jdbc:mysql://localhost:3306/dev_db") .username("dev_user") .password("dev_password") .build(); } @Bean @Profile("prod") public DataSource prodDataSource() { return () .url("jdbc:mysql://localhost:3306/prod_db") .username("prod_user") .password("prod_password") .build(); } }
In the above code, when activateddev
During the environment, Spring will load the development environment data source, andprod
In the environment, the data source of the production environment will be loaded.
6. Conclusion
Spring Boot provides flexible multi-environment configuration support, allowing developers to easily set different configurations for different environments according to project needs. By using the correct use of multi-environment configuration, it is possible to ensure that projects maintain consistent behavior in development, testing, and production, while effectively reducing environmental configuration errors.
Through command line, environment variable or@Profile
The combination of annotations can make Spring Boot projects more flexible and efficient.
This is the end of this article about implementing multi-environment configuration in Spring Boot. For more related content on Spring Boot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!