DEV Community

Cover image for Managing Application Properties with Spring Boot Profiles: A Best Practices Guide
AYON KARMAKAR
AYON KARMAKAR

Posted on

Managing Application Properties with Spring Boot Profiles: A Best Practices Guide

Managing environment-specific configurations can be challenging, but Spring Boot simplifies this with profiles. Profiles allow you to define distinct configurations for different environments like development, testing, and production, ensuring your application runs smoothly across all stages. In this article, we'll dive into using Spring Boot profiles, focusing on practical code examples and minimal theory.

Setting Up Profiles in Spring Boot

Spring Boot uses properties files to manage configurations. You can create environment-specific configurations using application-{profile}.properties files. Here's how to set up and use these files.

Project Structure

src/
└── main/
    ├── java/
       └── com/
           └── example/
               └── DemoApplication.java
    └── resources/
        ├── application.properties
        ├── application-dev.properties
        ├── application-test.properties
        └── application-prod.properties

Enter fullscreen mode Exit fullscreen mode

General Configuration

The application.properties file contains common configurations and a setting to activate the desired profile.

#######################################################
# Spring Profiles Configuration
#######################################################

# Active profile for development
# spring.profiles.active=dev

# Active profile for production
# Uncomment the following line for the production environment
spring.profiles.active=prod

#######################################################
# End of Configuration
#######################################################
Enter fullscreen mode Exit fullscreen mode

This file serves as the base configuration. You can activate specific profiles by uncommenting the relevant spring.profiles.active line.

Environment-Specific Configuration

Development Configuration (application-dev.properties)

#######################################################
# Application Configuration: DEV
#######################################################

spring.application.name=licensing-dev
server.port=8080

spring.datasource.url=jdbc:h2:mem:devdb
spring.datasource.username=devuser
spring.datasource.password=devpass
spring.datasource.driver-class-name=org.h2.Driver

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=create-drop

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=dev.mail@example.com
spring.mail.password=devMailPassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

mail.enabled=false

application.security.jwt.secret-key=devSecretKey
application.security.jwt.access-token-expiration=86400000
application.security.jwt.refresh-token-expiration=604800000

client.url=http://localhost:3000

application.default.admin.username=devAdmin
application.default.admin.password=devAdminPassword

#######################################################
# End of Configuration
#######################################################
Enter fullscreen mode Exit fullscreen mode

Production Configuration (application-prod.properties)

#######################################################
# Application Configuration: PROD
#######################################################

spring.application.name=licensing
server.port=8080

spring.datasource.url=jdbc:mysql://<PROD_DB_HOST>:<PROD_DB_PORT>/<PROD_DB_NAME>
spring.datasource.username=<PROD_DB_USERNAME>
spring.datasource.password=<PROD_DB_PASSWORD>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<PROD_MAIL_USERNAME>
spring.mail.password=<PROD_MAIL_PASSWORD>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

mail.enabled=true

application.security.jwt.secret-key=<PROD_JWT_SECRET_KEY>
application.security.jwt.access-token-expiration=86400000
application.security.jwt.refresh-token-expiration=604800000

client.url=http://<PROD_FRONTEND_HOST>:<PROD_FRONTEND_PORT>

application.default.admin.username=<PROD_ADMIN_USERNAME>
application.default.admin.password=<PROD_ADMIN_PASSWORD>

#######################################################
# End of Configuration
#######################################################
Enter fullscreen mode Exit fullscreen mode

Activating Profiles

You can activate a profile in multiple ways:

  • JVM Argument: java -jar -Dspring.profiles.active=prod demo-0.0.1-SNAPSHOT.jar
  • Environment Variable: export SPRING_PROFILES_ACTIVE=prod Programmatically:
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setAdditionalProfiles("dev");
app.run(args);
Enter fullscreen mode Exit fullscreen mode

Best Practices for Using Profiles

  1. Secure Sensitive Data: Use environment variables or secure secrets management solutions for sensitive data like passwords or API keys.
  2. Consistent Configuration: Maintain consistency in critical configurations, such as logging or CORS, across environments.
  3. Documentation: Clearly document the purpose of each configuration to aid future maintenance and onboarding.

Conclusion :
Spring Boot profiles provide a streamlined approach to managing environment-specific configurations. By organizing your settings into separate properties files and following best practices, you can ensure that your application is both secure and easy to manage across different stages of development and deployment. With minimal theory and a focus on practical setup, this guide equips you to handle Spring Boot profiles effectively in your projects.

Gift 🎁:

#######################################################
# Application Configuration: PROD
#######################################################

# Application Name
spring.application.name=licensing

# Server Configuration
server.port=8080

#######################################################
# Database Configuration
#######################################################

spring.datasource.url=jdbc:mysql://<PROD_DB_HOST>:<PROD_DB_PORT>/<PROD_DB_NAME>
spring.datasource.username=<PROD_DB_USERNAME>
spring.datasource.password=<PROD_DB_PASSWORD>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#######################################################
# JPA and Hibernate Configuration
#######################################################

spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update

#######################################################
# Mail SMTP Configuration
#######################################################

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<PROD_MAIL_USERNAME>
spring.mail.password=<PROD_MAIL_PASSWORD>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

# Application Properties
mail.enabled=true

#######################################################
# Security Configuration
#######################################################

# Secret key for JWT & token expiration
application.security.jwt.secret-key=<PROD_JWT_SECRET_KEY>
application.security.jwt.access-token-expiration=86400000
application.security.jwt.refresh-token-expiration=604800000

#######################################################
# CORS Configuration
#######################################################

# Allowed Origins
client.url=http://<PROD_FRONTEND_HOST>:<PROD_FRONTEND_PORT>

#######################################################
# Default Admin Configuration
#######################################################

application.default.admin.username=<PROD_ADMIN_USERNAME>
application.default.admin.password=<PROD_ADMIN_PASSWORD>

#######################################################
# End of Configuration
#######################################################
Enter fullscreen mode Exit fullscreen mode

Top comments (0)