Step 1: Configure Development and Production Profiles in Spring Boot
Lecture Slides
Either click on the slide area below or click here to view it in fullscreen. Use your keypad to navigate the slides.
A PDF printable handout version of the slides is available here
Introduction
In this step, you’ll configure separate application profiles for development and production environments, ensuring your Spring Boot application can seamlessly transition from local development to AWS Lightsail deployment.
Understanding Spring Boot Profiles
Spring Boot profiles allow you to segregate parts of your application configuration and make it available only in certain environments. This is crucial when deploying to cloud platforms like AWS Lightsail, where database connections, logging levels, and other configurations differ from your local development setup.
Using profiles prevents hardcoding production credentials in your code and allows you to test locally with development databases while deploying with production settings.
Setting Up Application Properties Files
Creating the Base Configuration
First, let’s set up your main application.properties file with common configurations that apply to all environments:
# src/main/resources/application.properties
# Application name and basic settings
spring.application.name=my-spring-app
server.servlet.context-path=/
# Default active profile (will be overridden in production)
spring.profiles.active=dev
# JPA/Hibernate common settings
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
# Logging configuration
logging.level.root=INFO
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
Development Profile Configuration
Create application-dev.properties for your local development environment:
# src/main/resources/application-dev.properties
# Local MySQL database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/myapp_dev
spring.datasource.username=dev_user
spring.datasource.password=dev_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Development-specific settings
spring.jpa.show-sql=true
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate.SQL=DEBUG
# Server configuration for local development
server.port=8080
Production Profile Configuration
Create application-prod.properties for your AWS Lightsail deployment:
# src/main/resources/application-prod.properties
# Production MySQL database configuration (will be set via environment variables)
spring.datasource.url=${DB_URL:jdbc:mysql://localhost:3306/myapp_prod}
spring.datasource.username=${DB_USERNAME:prod_user}
spring.datasource.password=${DB_PASSWORD:secure_password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Production optimizations
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate
# Security and performance settings
server.port=8080
server.compression.enabled=true
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
server.compression.min-response-size=1024
# Logging for production
logging.level.org.springframework.web=WARN
logging.level.org.hibernate.SQL=WARN
logging.file.name=/var/log/myapp/application.log
Notice how the production profile uses ${DB_URL:default_value} syntax. This allows you to override database settings using environment variables on your Lightsail server without changing your code.
Updating Your Build Configuration
Maven Configuration
If you’re using Maven, ensure your pom.xml includes the MySQL connector dependency:
<!-- pom.xml -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring Boot Starter Data JPA (if not already included) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>Also add the Maven plugin configuration for building executable JARs:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>Gradle Configuration
If you’re using Gradle, update your build.gradle:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java'
// ... other dependencies
}
jar {
enabled = false
}
bootJar {
enabled = true
}Testing Profile Configuration
Testing Development Profile
Run your application locally to verify the development profile works:
# Run with development profile (default)
mvn spring-boot:run
# Or explicitly specify the development profile
mvn spring-boot:run -Dspring-boot.run.profiles=devTesting Production Profile Locally
You can test the production profile locally by setting environment variables:
# Set environment variables for production database
export DB_URL="jdbc:mysql://localhost:3306/myapp_test"
export DB_USERNAME="test_user"
export DB_PASSWORD="test_password"
# Run with production profile
mvn spring-boot:run -Dspring-boot.run.profiles=prodMake sure you have a test database available when testing the production profile locally. The application will fail to start if it can’t connect to the specified database.
Creating a Production-Ready JAR
Build your application for deployment:
# Clean and build the project
mvn clean package
# The executable JAR will be created in the target directory
ls -la target/*.jarYou should see output similar to:
-rw-r--r-- 1 user user 45M Dec 1 10:30 my-spring-app-1.0.0.jar
Checkpoint
Before moving on, verify that:
Testing Your Configuration
Run this quick test to ensure everything is working:
# Test that profiles are being loaded correctly
java -jar target/my-spring-app-1.0.0.jar --spring.profiles.active=prod --spring.datasource.url=jdbc:mysql://localhost:3306/testIf you see a database connection error, that’s expected since we haven’t set up the production database yet. The important thing is that the application attempts to start with the production profile.
What Just Happened?
You’ve successfully configured your Spring Boot application with separate development and production profiles. This separation allows you to:
- Develop locally with a development database and debug logging
- Deploy to production with optimized settings and environment-specific configurations
- Maintain security by using environment variables for sensitive production data
- Ensure consistency across different deployment environments
The production profile is now ready to use environment variables for database configuration, which we’ll set up on the AWS Lightsail instance in the next step. Your application can also be packaged as an executable JAR that’s ready for deployment.
Key Takeaways
- Spring Boot profiles enable environment-specific configurations
- Environment variables provide a secure way to manage production credentials
- Executable JARs make deployment simpler and more portable
- Proper profile configuration is essential for cloud deployments
In the next step, we’ll create and configure your AWS Lightsail instance where this production-ready application will be deployed.