Spring Boot provides the Profile feature to configure different environments such as development, testing, and production. This feature helps in managing application settings efficiently without hardcoding environment-specific configurations.
What is a Spring Boot Profile?
A Spring Profile is a way to group application configurations and beans based on a specific environment. This allows the application to load only relevant configurations at runtime, making it easier to maintain environment-specific settings.
Defining Profiles in Spring Boot
Spring Boot allows defining profiles using:
- application.properties or application.yml
- @Profile annotation
1. Using application.properties
or application.yml
Spring Boot supports different profile-specific property files, which are named as:
application-dev.properties
application-test.properties
application-prod.properties
Example:
application-dev.properties
server.port=8081
datasource.url=jdbc:mysql://localhost:3306/devdb
datasource.username=devuser
datasource.password=devpassword
application-prod.properties
server.port=8080
datasource.url=jdbc:mysql://localhost:3306/proddb
datasource.username=produser
datasource.password=prodpassword
To activate a specific profile, add the following property in application.properties
:
spring.profiles.active=dev
Or set it via command-line:
java -jar myapp.jar --spring.profiles.active=prod
2. Using @Profile
Annotation
Spring allows defining beans specific to a profile using the @Profile
annotation.
Example:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile("dev")
public DataSource devDataSource() {
return new DataSource("jdbc:mysql://localhost:3306/devdb", "devuser", "devpassword");
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
return new DataSource("jdbc:mysql://localhost:3306/proddb", "produser", "prodpassword");
}
}
Switching Profiles Programmatically
Profiles can also be switched dynamically in Java:
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
public class ProfileSwitcher {
public static void main(String[] args) {
Environment env = new StandardEnvironment();
((StandardEnvironment) env).setActiveProfiles("test");
System.out.println("Active Profile: " + env.getActiveProfiles()[0]);
}
}
Using Spring Boot Profiles in application.yml
Spring Boot also supports YAML-based profile configuration:
spring:
profiles:
active: dev
---
spring:
profiles: dev
server:
port: 8081
---
spring:
profiles: prod
server:
port: 8080
Conclusion
Spring Boot’s Profile feature provides a clean and efficient way to manage multiple environments with minimal effort. By using profile-specific property files, annotations, or programmatic configurations, developers can streamline environment management and ensure smooth application deployment across different settings.