
Managing Environments in Spring Boot: Creating Profiles for Development and Production
In software development, ability to manage environments is crucial. Profiles in Spring Boot allow developers to define sets of configuration properties that are specific to certain environments, such as development, test, staging, or production. By activating a specific profile, Spring Boot loads the corresponding configuration properties, ensuring that the application behaves differently based on the chosen environment.
We will create two profiles in this article one for development and another for production environments.
We need to create 3 files in resources directory of your application that defines the configurations to your application.
application.yml
application-development.yml
application-production.yml
In application-production.yml file we need to add section config that defines the configurations of spring file to be only active when the profile is set to production mode.
Note: r2dbc connections and other configurations in the below yaml file are only for reference and may change with your requirements.
spring:
config:
activate:
on-profile: production
r2dbc:
url: r2dbc:postgresql://your-application-production.bneslso0f2t3c.us-east-1.rds.amazonaws.com:5432/postgres?sslMode=require
username: postgres
password: postgres
springdoc:
swagger-ui:
enabled: true
path: v1/your-application/swagger-ui.html
api-docs:
path: /v1/your-application-docs
In application-development.yml file we need to add section config that defines the configurations of spring file to be only active when the profile is set to development mode.
Note: r2dbc connections and other configurations in the below yaml file are only for reference and may change with your requirements.
spring:
config:
activate:
on-profile: development
r2dbc:
url: r2dbc:postgresql://your-application-development.bneslso0f2t3c.us-east-1.rds.amazonaws.com:5432/postgres?sslMode=require
username: postgres
password: postgres
springdoc:
swagger-ui:
enabled: true
path: v1/your-application/swagger-ui.html
api-docs:
path: /v1/your-application-docs
In application.yml file we add the profile to load when the application runs that is in below example we are adding development as our active profile such that it loads the configurations from application-development.yml file.
spring:
profiles:
active: development
application:
name: your-application-name
Above example configures the application to connect to different Amazon RDS databases with respect to environments.