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
@Profileannotation indicates that a component should be registered only when one or more specified configuration profiles are active.
1. Simple Application
application.yml
<code>server:
port: 8081
---
spring:
profiles:
active:
- test</code>application-test.yml
<code>custom:
name: test
index: 1</code>application-dev.yml
<code>custom:
name: dev
index: 0</code>Property Configuration Class
<code>@Component
@ConfigurationProperties(prefix = "test")
public class CustomProperties {
private String name;
private Integer index;
}</code>When
spring.profiles.active=testis set in the main configuration, the properties from
application-test.ymlare injected.
2. Register Bean According to Profile
Configuration class:
<code>@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;
}
}</code>The above configuration registers different
CustomPropertiesbeans 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.
<code>@Bean
@ConfigurationProperties(prefix = "custom")
@Profile({"dev", "jdbc"})
public CustomProperties devJdbc() {
return new CustomProperties();
}</code>The bean registers when the active profile contains either
devor
jdbc.
4. Logical Operators in Profile
Negation (!)
<code>@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("!dev")
public CustomProperties cp() {
return new CustomProperties();
}</code>The bean registers as long as the active profile does not include
dev.
AND (&)
<code>@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("dev & jdbc")
public CustomProperties cp() {
return new CustomProperties();
}</code>The bean registers only when both
devand
jdbcare present in the active profiles (other profiles may also be present).
OR (|)
<code>@Bean
@ConfigurationProperties(prefix = "custom")
@Profile("dev | jdbc")
public CustomProperties cp() {
return new CustomProperties();
}</code>The bean registers when either
devor
jdbcis present.
5. Default Profile Configuration
You can define a default bean that applies when no other profile conditions are satisfied.
<code>@Bean
@Profile("default")
public CustomProperties defCP() {
CustomProperties cp = new CustomProperties();
cp.setName("default");
cp.setIndex(1000);
return cp;
}</code>If
spring.profiles.activeis 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.
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.