How to Manage Multiple Environments in Spring Boot Easily
This guide explains why manual configuration changes across development, test, and production environments are error‑prone and shows step‑by‑step how Spring Boot’s profile‑specific property files, code snippets, and activation methods let you configure and switch environments quickly and reliably.
In real‑world projects each environment (development, test, production) often requires different database, Redis, and other settings, and manually editing these configurations is time‑consuming and error‑prone. Spring Boot provides a concise solution for multi‑environment configuration.
1. Create profile‑specific configuration files
Spring Boot follows the naming convention application-{profile}.properties, where {profile} is the environment identifier. In the src/main/resources directory create three files: application-dev.properties – development environment application-test.properties – test environment application-prod.properties – production environment
The main application.properties holds common settings shared by all profiles.
2. Modify the configuration files
Set the common server port in application.properties: server.port=8088 Configure the development database in application-dev.properties:
# Specify driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# JDBC URL
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/myapp_dev
# Credentials
spring.datasource.username=root
spring.datasource.password=rootSimilarly adjust application-test.properties and application-prod.properties with their respective database URLs and credentials.
3. Switch the active profile
Spring Boot activates a profile based on the spring.profiles.active property. There are three common ways to set it:
(1) In the property file
spring.profiles.active=devThis selects the development profile.
(2) Via IntelliJ IDEA run configuration
In IDEA’s Run/Debug Configuration panel you can add the VM option, program argument, or use the “Active profiles” field to specify the profile. The screenshot below illustrates the UI.
(3) Command‑line argument
java -jar xxx.jar --spring.profiles.active=devWhen the JAR is launched with this argument, Spring Boot loads the application-dev.properties file. The startup log will show which profile was activated, as demonstrated in the following log screenshot.
By following these steps you can configure and switch between multiple environments in Spring Boot quickly and reliably, laying the groundwork for more advanced configuration management such as Spring Cloud Config.
Conclusion
Spring Boot’s profile‑based configuration is a fundamental and essential feature for backend development, simplifying environment management and reducing the risk of manual errors.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
