0
0
SpringbootHow-ToBeginner · 4 min read

How to Configure Production Spring Boot Applications

To configure Spring Boot for production, use application-prod.properties or application-prod.yml with the spring.profiles.active=prod setting. Configure production-specific settings like database URLs, logging levels, and security in this profile to separate them from development configurations.
📐

Syntax

Spring Boot uses application.properties or application.yml files to configure settings. For production, create a profile-specific file like application-prod.properties. Activate it by setting spring.profiles.active=prod in application.properties or as a command-line argument.

  • application-prod.properties: Holds production settings.
  • spring.profiles.active: Chooses which profile to use.
  • Logging and security: Customize for production needs.
properties
spring.profiles.active=prod

# application-prod.properties
server.port=8080
spring.datasource.url=jdbc:mysql://prod-db-server:3306/proddb
spring.datasource.username=produser
spring.datasource.password=prodpass
logging.level.root=INFO
management.endpoint.health.show-details=when_authorized
management.endpoints.web.exposure.include=health,info
💻

Example

This example shows how to set up a production profile with database connection, logging level, and management endpoints. The spring.profiles.active=prod activates the production settings when the app runs.

properties
# src/main/resources/application.properties
spring.profiles.active=prod

# src/main/resources/application-prod.properties
server.port=8080
spring.datasource.url=jdbc:mysql://prod-db-server:3306/proddb
spring.datasource.username=produser
spring.datasource.password=prodpass
logging.level.root=INFO
management.endpoint.health.show-details=when_authorized
management.endpoints.web.exposure.include=health,info
Output
Application starts on port 8080 connecting to the production database with INFO logging level.
⚠️

Common Pitfalls

Common mistakes include:

  • Not activating the production profile, so the app uses default or development settings.
  • Hardcoding sensitive data like passwords in properties files instead of using environment variables or secrets management.
  • Setting logging to DEBUG in production, which can slow down the app and expose sensitive info.
  • Exposing management endpoints without security, risking data leaks.
properties
# Wrong: No profile active, uses default dev settings
# application.properties

# Right: Activate production profile
spring.profiles.active=prod

# Wrong: Hardcoded password in repo
spring.datasource.password=secret123

# Right: Use environment variable
spring.datasource.password=${DB_PASSWORD}
📊

Quick Reference

Summary tips for production Spring Boot configuration:

  • Use application-prod.properties or .yml for production settings.
  • Activate production profile with spring.profiles.active=prod.
  • Keep sensitive info out of code; use environment variables or vaults.
  • Set logging to INFO or WARN in production.
  • Secure management endpoints and actuator features.

Key Takeaways

Use Spring profiles to separate production settings from development.
Activate the production profile with spring.profiles.active=prod.
Avoid hardcoding sensitive data; use environment variables instead.
Set logging levels appropriately to avoid performance and security issues.
Secure management and actuator endpoints in production.