org.springframework.boot.autoconfigure.condition

Annotation Type ConditionalOnProperty



  • @Retention(value=RUNTIME)
     @Target(value={TYPE,METHOD})
     @Documented
     @Conditional(value=org.springframework.boot.autoconfigure.condition.OnPropertyCondition.class)
    public @interface ConditionalOnProperty
    Conditional that checks if the specified properties have a specific value. By default the properties must be present in the Environment and not equal to false. The havingValue() and matchIfMissing() attributes allow further customizations.

    The havingValue() attribute can be used to specify the value that the property should have. The table below shows when a condition matches according to the property value and the havingValue() attribute:

    Property Value havingValue="" havingValue="true" havingValue="false" havingValue="foo"
    "true" yes yes no no
    "false" no no yes no
    "foo" yes no no yes

    If the property is not contained in the Environment at all, the matchIfMissing() attribute is consulted. By default missing attributes do not match.

    This condition cannot be reliably used for matching collection properties. For example, in the following configuration, the condition matches if spring.example.values is present in the Environment but does not match if spring.example.values[0] is present.

     @ConditionalOnProperty(prefix = "spring", name = "example.values")
     class ExampleAutoConfiguration {
     }
     
    It is better to use a custom condition for such cases.
    Since:
    1.1.0
    Author:
    Maciej Walkowiak, Stephane Nicoll, Phillip Webb
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element and Description
      String havingValue
      The string representation of the expected value for the properties.
      boolean matchIfMissing
      Specify if the condition should match if the property is not set.
      String[] name
      The name of the properties to test.
      String prefix
      A prefix that should be applied to each property.
      String[] value
      Alias for name().
    • Element Detail

      • value

        public abstract String[] value
        Alias for name().
        Returns:
        the names
        Default:
        {}
      • prefix

        public abstract String prefix
        A prefix that should be applied to each property. The prefix automatically ends with a dot if not specified. A valid prefix is defined by one or more words separated with dots (e.g. "acme.system.feature").
        Returns:
        the prefix
        Default:
        ""
      • name

        public abstract String[] name
        The name of the properties to test. If a prefix has been defined, it is applied to compute the full key of each property. For instance if the prefix is app.config and one value is my-value, the full key would be app.config.my-value

        Use the dashed notation to specify each property, that is all lower case with a "-" to separate words (e.g. my-long-property).

        Returns:
        the names
        Default:
        {}
      • havingValue

        public abstract String havingValue
        The string representation of the expected value for the properties. If not specified, the property must not be equal to false.
        Returns:
        the expected value
        Default:
        ""
      • matchIfMissing

        public abstract boolean matchIfMissing
        Specify if the condition should match if the property is not set. Defaults to false.
        Returns:
        if should match if the property is missing
        Default:
        false