새소식

반응형
Java/Spring

@ConditionalOnBean, @ConditionalOnExpression

  • -
반응형

@ConditionalOnBean, @ConditionalOnExpression

두개의 어노테이션들은 조건부 Bean 등록을 하는 어노테이션들이다.

보통 @Configuration 클래스나 @Bean 메서드에 붙여서 특정 조건이 만족될 때만 Bean 등록할 수 있도록 한다.

 

1. @ConditionalOnBean

  • 정의: 어플리케이션 컨텍스트에 특정 Bean 이 존재할 경우 현재 Bean 을 등록하도록 조건을 거는 어노테이션
  • 용도: 어떤 기능이 다른 Bean 이 존재해야만 동작 가능한 경우 사용

예시

@Configuration
public class MyConfig {

    @Bean
    public ServiceA serviceA() {
        System.out.println("=====================");
        System.out.println("Service A Bean 등록");
        System.out.println("=====================");
        return new ServiceA();
    }

    // ServiceA가 존재해야만 ServiceB가 Bean으로 등록됨
    @Bean
    @ConditionalOnBean(ServiceA.class)
    public ServiceB serviceB() {
        System.out.println("=====================");
        System.out.println("Service B Bean 등록");
        System.out.println("=====================");
        return new ServiceB();
    }
}

 

👉 위 예제에서는 ServiceA 가 존재할 경우 ServiceB Bean 을 등록한다.

 

ServiceA 클래스가 Bean 등록되어 있을 경우

// 결과
=====================
Service A Bean 등록
=====================
=====================
Service B Bean 등록
=====================
2025-09-11 16:24:10.493 [restartedMain] INFO  org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer - LiveReload server is running on port 35729
2025-09-11 16:24:10.526 [restartedMain] INFO  org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
2025-09-11 16:24:10.567 [restartedMain] INFO  org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port 8080 (http) with context path '/jisan'
2025-09-11 16:24:10.584 [restartedMain] INFO  com.jisan.store.JisanStoreApplication - Started JisanStoreApplication in 5.189 seconds (process running for 7.471)

 

 

ServiceA 클래스가 Bean 등록이 안되어 있을 경우

2025-09-11 16:24:10.493 [restartedMain] INFO  org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer - LiveReload server is running on port 35729
2025-09-11 16:24:10.526 [restartedMain] INFO  org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
2025-09-11 16:24:10.567 [restartedMain] INFO  org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port 8080 (http) with context path '/jisan'
2025-09-11 16:24:10.584 [restartedMain] INFO  com.jisan.store.JisanStoreApplication - Started JisanStoreApplication in 5.189 seconds (process running for 7.471)

 

애초에 ServiceB 클래스가  Bean 등록되지 않는다.

 

2. @ConditionalOnExpression

  • 정의: SpEL(Spring Expression Language) 표현식을 평가해서 true 일 경우에만 Bean 을 등록
  • 용도: 프로퍼티 값이나 특정 조건식 기반으로 Bean 등록 여부를 제어할 때 사용

예시

@Configuration
public class MyExpressionConfig {

    // application.properties 에서 my.feature.enabled=true 로 설정했을 때만 등록
    @Bean
    @ConditionalOnExpression("${my.feature.enabled:true}")
    public FeatureService featureService() {
        return new FeatureService();
    }
}

 

👉 application.properties

my.feature.enabled=true
  • 값이 true 면 FeatureService Bean 으로 등록된다.
  • 값이 false 일 경우 등록되지 않음

 

✅ 정리

  • @ConditionalOnBean → 다른 Bean 이 있을 때만 등록
  • @ConditionalOnExpression → SpEL 표현식이 true 일 경우 등록

 

 

반응형
Contents