Backend Development 5 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring Boot @Profile: Conditional Bean Registration Guide

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

<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=test

is set in the main configuration, the properties from

application-test.yml

are 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

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.

<code>@Bean
@ConfigurationProperties(prefix = "custom")
@Profile({"dev", "jdbc"})
public CustomProperties devJdbc() {
    return new CustomProperties();
}</code>

The bean registers when the active profile contains either

dev

or

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

dev

and

jdbc

are 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

dev

or

jdbc

is 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.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.

JavaSpringBootprofileconfiguration-propertiesConditional Bean
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.