Annotation Interface ConditionalProperty


@Retention(RUNTIME) @Target({TYPE,METHOD}) @Documented @Conditional(PropertyCondition.class) public @interface ConditionalProperty
Registers the annotated @Bean or @Component only if the property matches one value in the given set. The bean can be registered when the property is absent by setting matchesWhenMissing = true. It can be used to implement feature toggling in a declarative way. Given a set of properties in the context environment:

   prop.a = true
   prop.b = sad
   prop.c = happy
 
the following bean definitions are registered

   @Bean
   @ConditionalProperty(name = "prop.a")
   [...]

   @Bean
   @ConditionalProperty(name = "prop.b", matches = "sad")
   [...]

   @Bean
   @ConditionalProperty(name = "prop.c", matches = {"happy", "tired"})
   [...]

   @Bean
   @ConditionalProperty(name = "prop.d", matchesWhenMissing = true)
   [...]
 
while the following bean definitions are discarded

   @Bean
   @ConditionalProperty(name = "prop.b") // "sad" does not match "true"
   [...]

   @Bean
   @ConditionalProperty(name = "prop.c", matches = {"sad", "tired"}) // none matches "happy"
   [...]

   @Bean
   @ConditionalProperty(name = "prop.d") // property is absent
   [...]
 
  • Element Details

    • name

      String name
    • matches

      String[] matches
      Default:
      {"true"}
    • matchesWhenMissing

      boolean matchesWhenMissing
      Default:
      false