Mastering Spring Boot @Profile: Conditional Bean Registration Guide
This tutorial demonstrates how to use Spring Boot's @Profile annotation to conditionally register beans based on active profiles, covering simple configuration files, multi‑condition expressions, logical operators, and default profile handling with complete code examples.
Environment
Spring Boot version: 2.4.10
@Profile Annotation Overview
The @Profile annotation indicates that a component should be registered only when one or more specified configuration profiles are active.
1. Simple Application
application.yml
server:
port: 8081
---
spring:
profiles:
active:
- testapplication-test.yml
custom:
name: test
index: 1application-dev.yml
custom:
name: dev
index: 0Property Configuration Class
@Component
@ConfigurationProperties(prefix = "test")
public class CustomProperties {
private String name;
private Integer index;
}When spring.profiles.active=test is set in the main configuration, the properties from application-test.yml are injected.
2. Register Bean According to Profile
Configuration class:
@Configuration
public class ProfileConfig {
@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("dev")
public CustomProperties cp() {
return new CustomProperties();
}
@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("test")
public CustomProperties cp() {
CustomProperties cp = new CustomProperties();
cp.setName("测试环境");
cp.setIndex(10);
return cp;
}
}The above configuration registers different CustomProperties beans depending on the active profile.
3. Multi‑Condition Profile Usage
You can specify multiple values for @Profile; the bean is registered if any one matches.
@Bean
@ConfigurationProperties(prefix = "custom")
@Profile({"dev", "jdbc"})
public CustomProperties devJdbc() {
return new CustomProperties();
}The bean registers when the active profile contains either dev or jdbc.
4. Logical Operators in Profile
Negation (!)
@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("!dev")
public CustomProperties cp() {
return new CustomProperties();
}The bean registers as long as the active profile does not include dev.
AND (&)
@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("dev & jdbc")
public CustomProperties cp() {
return new CustomProperties();
}The bean registers only when both dev and jdbc are present in the active profiles (other profiles may also be present).
OR (|)
@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("dev | jdbc")
public CustomProperties cp() {
return new CustomProperties();
}The bean registers when either dev or jdbc is present.
5. Default Profile Configuration
You can define a default bean that applies when no other profile conditions are satisfied.
@Bean
@Profile("default")
public CustomProperties defCP() {
CustomProperties cp = new CustomProperties();
cp.setName("default");
cp.setIndex(1000);
return cp;
}If spring.profiles.active is not set, this default bean becomes active. If a profile is set but none of the conditions match, the default bean will not be applied.
End of tutorial.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
