Spring Boot Profiles provide a way to segregate parts of your application configuration and make it available only in certain environments. This is particularly useful for managing different configurations for development, testing, and production environments.
Key Concepts
- Profiles: Logical grouping of configuration settings.
- Application Properties: Different property files for different profiles.
- Activating Profiles: Methods to activate a specific profile.
- Profile-Specific Beans: Defining beans that are only available in certain profiles.
Application Properties
Spring Boot allows you to define multiple property files for different profiles. The default property file is application.properties
or application.yml
. For profile-specific configurations, you can create files like application-dev.properties
, application-test.properties
, and application-prod.properties
.
Example
application.properties
application-dev.properties
server.port=8081 spring.datasource.url=jdbc:h2:mem:devdb spring.datasource.username=devuser spring.datasource.password=devpass
application-prod.properties
server.port=8082 spring.datasource.url=jdbc:mysql://prod-db-server/mydb spring.datasource.username=produser spring.datasource.password=prodpass
Activating Profiles
You can activate a profile in several ways:
-
Command Line: Use the
--spring.profiles.active
argument.java -jar myapp.jar --spring.profiles.active=dev
-
Environment Variable: Set the
SPRING_PROFILES_ACTIVE
environment variable.export SPRING_PROFILES_ACTIVE=dev
-
Application Properties: Set the active profile in
application.properties
.spring.profiles.active=dev
-
Programmatically: Activate profiles in your main application class.
@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication app = new SpringApplication(MyApp.class); app.setAdditionalProfiles("dev"); app.run(args); } }
Profile-Specific Beans
You can define beans that are only available in certain profiles using the @Profile
annotation.
Example
@Configuration public class DataSourceConfig { @Bean @Profile("dev") public DataSource devDataSource() { return new HikariDataSource(); } @Bean @Profile("prod") public DataSource prodDataSource() { return new HikariDataSource(); } }
Practical Exercise
Task
- Create a Spring Boot application with two profiles:
dev
andprod
. - Define different
server.port
values for each profile. - Create a
DataSource
bean that is specific to each profile.
Solution
-
Create the Project Structure:
src/main/resources/application.properties
src/main/resources/application-dev.properties
src/main/resources/application-prod.properties
src/main/java/com/example/demo/DemoApplication.java
src/main/java/com/example/demo/DataSourceConfig.java
-
Define Properties:
application.properties
application-dev.properties
server.port=8081 spring.datasource.url=jdbc:h2:mem:devdb spring.datasource.username=devuser spring.datasource.password=devpass
application-prod.properties
server.port=8082 spring.datasource.url=jdbc:mysql://prod-db-server/mydb spring.datasource.username=produser spring.datasource.password=prodpass
- Main Application Class:
DemoApplication.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
- DataSource Configuration:
DataSourceConfig.java
package com.example.demo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import javax.sql.DataSource; import com.zaxxer.hikari.HikariDataSource; @Configuration public class DataSourceConfig { @Bean @Profile("dev") public DataSource devDataSource() { HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:h2:mem:devdb"); dataSource.setUsername("devuser"); dataSource.setPassword("devpass"); return dataSource; } @Bean @Profile("prod") public DataSource prodDataSource() { HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://prod-db-server/mydb"); dataSource.setUsername("produser"); dataSource.setPassword("prodpass"); return dataSource; } }
Common Mistakes and Tips
- Forgetting to Activate Profiles: Ensure you activate the correct profile when running your application.
- Profile-Specific Beans Not Loaded: Make sure the
@Profile
annotation matches the active profile. - Property File Naming: Ensure profile-specific property files follow the
application-{profile}.properties
naming convention.
Conclusion
Spring Boot Profiles are a powerful feature for managing different configurations for various environments. By understanding how to define and activate profiles, you can create flexible and maintainable applications that adapt to different deployment scenarios. In the next module, we will explore how to use Docker with Spring Boot to further enhance your application's deployment capabilities.
Spring Boot Course
Module 1: Introduction to Spring Boot
- What is Spring Boot?
- Setting Up Your Development Environment
- Creating Your First Spring Boot Application
- Understanding Spring Boot Project Structure
Module 2: Spring Boot Basics
- Spring Boot Annotations
- Dependency Injection in Spring Boot
- Spring Boot Configuration
- Spring Boot Properties
Module 3: Building RESTful Web Services
- Introduction to RESTful Web Services
- Creating REST Controllers
- Handling HTTP Methods
- Exception Handling in REST
Module 4: Data Access with Spring Boot
- Introduction to Spring Data JPA
- Configuring Data Sources
- Creating JPA Entities
- Using Spring Data Repositories
- Query Methods in Spring Data JPA
Module 5: Spring Boot Security
- Introduction to Spring Security
- Configuring Spring Security
- User Authentication and Authorization
- Implementing JWT Authentication
Module 6: Testing in Spring Boot
Module 7: Advanced Spring Boot Features
Module 8: Deploying Spring Boot Applications
Module 9: Performance and Monitoring
- Performance Tuning
- Monitoring with Spring Boot Actuator
- Using Prometheus and Grafana
- Logging and Log Management