Spring 4 부터 사용 가능하며 Java Configuration 에서 조건적으로 Spring Bean 을 등록할 수 있다.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Conditional {
/**
* All {@link Condition}s that must {@linkplain Condition#matches match}
* in order for the component to be registered.
*/
Class? extends Condition[] value();
}
// Condtion.class
public interface Condition {
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}
@ConditionalOnWebApplication
: 프로젝트가 웹 애플리케이션이면 Bean 등록@ConditionalOnBean
: 해당 Bean이 존재하면 자동 설정 등록@ConditionalOnMissingBean
: 해당 Bean이 존재하지 않으면 자동 설정 등록@ConditionalOnClass
: 해당 클래스가 존재하면 자동 설정 등록@ConditionalOnMissingClass
: 해당 클래스가 클래스 패스에 존재하지 않으면 Bean 등록@ConditionalOnResource
: 해당 자원(file 등)이 존재하면 자동 설정 등록@ConditionalOnProperty
: 설정한 프로퍼티가 존재하면 자동 설정 등록