Member-only story
Spring Boot Profiles: How to Manage Environment-Based Configurations
Learn how to use Spring Boot Profiles to manage environment-specific configurations (dev, test, prod). Understand @Profile
, application.properties
, and application.yml
settings.
This is a member-only article. For non-members, read this article for free on my blog: Spring Boot Profiles.
๐ Introduction to Spring Boot Profiles
Spring Boot Profiles allow applications to load different configurations based on the environment (e.g., development, testing, production).
โ
Why Use Spring Boot Profiles?
โ Manage Environment-Specific Settings (Database, API Keys, Logging).
โ Easily Switch Configurations without modifying code.
โ Use Different Beans for Different Environments (@Profile
).
โ Avoid Hardcoding Environment Variables.
๐ In this guide, youโll learn:
โ
How Spring Boot Profiles Work.
โ
Using application.properties
and application.yml
for Profiles.
โ
How to Load Beans Conditionally Using @Profile
.
โ
How to Switch Between Profiles.
1๏ธโฃ How Do Spring Boot Profiles Work?
Spring Boot Profiles allow you to define multiple environment-specific configurations and activate them dynamically.
๐ Common Profiles Used in Projects:
2๏ธโฃ Defining Profiles in application.properties
By default, Spring Boot loads application.properties
, but we can create environment-specific files like:
application-dev.properties
application-test.properties
application-prod.properties
๐ Example: application-dev.properties
(Development Settings)
server.port=8081
spring.datasource.url=jdbc:h2:mem:devdb
logging.level.root=DEBUG
๐ Example: application-prod.properties
(Production Settings)