Table of Contents
- Introduction to Spring Profiles
- Why Use Profiles?
- Defining Profiles
- Activating Profiles
- Profile-Specific Configuration Files
- Programmatic Profile Activation
- Conditional Beans with
@Profile
- Default Profiles
- Multi-Profile Configurations
- Best Practices
- Conclusion
1. Introduction to Spring Profiles
Spring Boot supports a powerful feature called profiles, which allows you to define sets of configuration that can be selectively activated depending on the environment—such as development, testing, or production. Profiles make it easy to switch between different application behaviors and settings without modifying your source code.
2. Why Use Profiles?
In a real-world application, you might need:
- Different databases for dev, test, and prod
- Logging settings that vary by environment
- Bean configurations specific to environments
- Separate APIs or third-party integrations
Spring Profiles help manage these seamlessly by loading only relevant configurations for the currently active profile.
3. Defining Profiles
To define a profile-specific configuration file, you follow a naming pattern:
application-dev.properties
orapplication-dev.yml
application-prod.properties
orapplication-prod.yml
application-test.properties
orapplication-test.yml
These will override the base application.properties
or application.yml
values when the respective profile is active.
4. Activating Profiles
You can activate a profile in various ways:
A. In application.properties
:
propertiesCopyEditspring.profiles.active=dev
B. As command-line argument:
bashCopyEditjava -jar app.jar --spring.profiles.active=prod
C. As environment variable:
bashCopyEditSPRING_PROFILES_ACTIVE=prod
D. In programmatic context:
javaCopyEditSpringApplication app = new SpringApplication(MyApplication.class);
app.setAdditionalProfiles("test");
app.run(args);
5. Profile-Specific Configuration Files
Suppose you have the following:
application.yml:
yamlCopyEditserver:
port: 8080
spring:
datasource:
url: jdbc:h2:mem:default
application-dev.yml:
yamlCopyEditserver:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/devdb
When dev
is active, the application will override the base config with values from application-dev.yml
.
6. Programmatic Profile Activation
In some advanced use cases (like deciding profiles based on a condition), you can activate profiles in code:
javaCopyEditpublic static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setAdditionalProfiles("dev");
app.run(args);
}
This is useful when external factors decide the environment at runtime.
7. Conditional Beans with @Profile
You can conditionally load beans for specific environments using @Profile
.
javaCopyEdit@Configuration
@Profile("dev")
public class DevDataSourceConfig {
@Bean
public DataSource dataSource() {
return new HikariDataSource(...);
}
}
javaCopyEdit@Configuration
@Profile("prod")
public class ProdDataSourceConfig {
@Bean
public DataSource dataSource() {
return new HikariDataSource(...);
}
}
Only one of these will be active at a time, based on the current profile.
8. Default Profiles
If no profile is explicitly set, Spring Boot uses the default profile. You can assign beans or configurations to the default profile by not marking them with @Profile
or by marking them as:
javaCopyEdit@Profile("default")
9. Multi-Profile Configurations
You can activate multiple profiles at once:
bashCopyEditjava -jar app.jar --spring.profiles.active=dev,cloud
Spring will merge properties from application-dev.properties
and application-cloud.properties
, with latter keys overriding earlier ones if duplicated.
10. Best Practices
- Keep shared configuration in
application.yml
orapplication.properties
- Use separate profile-specific files for changing configurations like database URLs or ports
- Use
@Profile
to manage bean lifecycles per environment - Never hard-code profile names in your core application logic
- Use environment variables or system properties in production deployment pipelines
11. Conclusion
Spring Boot Profiles provide a clean and powerful way to handle multiple environments. By externalizing configuration and using profile-specific settings, your application becomes flexible, maintainable, and ready for deployment across varied infrastructure.