Configuration Condition

조건에 따라 환경을 설정할 수 있다.
RabbitMQ를 사용하는 서버와 사용하지 않는 서버의 환경을 달리 설정하는데 적용했다.

- Example
amqp.enabled 프로퍼티로 amqp 사용여부를 판단한다.
amqp를 사용하는 경우, 해당 클래스에 정의한 빈들을 등록한다.
amqp를 사용하지 않는 경우, 빈을 등록하지 않는다.

@Configuration
@Conditional(AmqpConfig.Condition.class)
public class AmqpConfig {

  @Autowired
  private Environment env;

  public static class Condition implements ConfigurationCondition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
      return context.getEnvironment().getProperty("amqp.enabled", Boolean.class);
    }

    @Override
    public ConfigurationPhase getConfigurationPhase() {
      return ConfigurationPhase.PARSE_CONFIGURATION;
    }
  }

  // @Bean
}

컨텍스트에 특정 빈이 등록되어 있는가? 조건 등으로도 사용할 수 있다.

+ Recent posts