The @conditionalonproperty annotation is often used in Spring Boot auto-configuration, and this article takes you through its capabilities.

Use in Spring Boot

In the Spring Boot source code, @conditionalonproperty annotations are used extensively for Http encoding and data source type auto-configuration.

Part HttpEncodingAutoConfiguration class source code:

@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(HttpProperties.class) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass(CharacterEncodingFilter.class) @ConditionalOnProperty(prefix = "spring.http.encoding", Value = "enabled", matchIfMissing = true) public class HttpEncodingAutoConfiguration} {/ / omit internal codeCopy the code

Section of the DataSourceConfiguration class

@Configuration(proxyBeanMethods = false) @ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class) @ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", HavingValue = "org. Apache. Tomcat. JDBC. Pool. The DataSource", matchIfMissing = true) static class tomcat} {/ / omit internal codeCopy the code

ConditionalOnProperty: ConditionalOnProperty: ConditionalOnProperty: ConditionalOnProperty: ConditionalOnProperty: ConditionalOnProperty: ConditionalOnProperty

ConditionalOnProperty

ConditionalOnProperty (ConditionalOnProperty)

@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented @Conditional(OnPropertyCondition.class) public @interface ConditionalOnProperty { // String[] value() default {}; // Configure the prefix of the attribute name, such as spring.http.encoding String prefix() default ""; String[] name() default {}; String[] name() default {}; String havingValue() default ""; String havingValue() default "; // Whether it can be loaded without the configuration attribute. If true, it will load normally without the configuration property. Boolean matchIfMissing() default false; }Copy the code

There is also a relaxedNames attribute in the historical version:

Boolean relaxedNames() default true;Copy the code

This property no longer exists in the latest version.

Through comments on ConditionalOnProperty @ Conditional (OnPropertyCondition. Class) code, you can see ConditionalOnProperty belong to @ Conditional derivative annotations. The effective conditions are judged by OnPropertyCondition.

Method of use

Regarding the use of @conditionalonProperty, we have seen it in Spring Boot above.

The core functionality of @conditionalonProperty is achieved through the name attribute and havingValue attribute.

First look at the matchIfMissing attribute, which is used to specify the default treatment if the corresponding attribute is not configured in the configuration file: by default, matchIfMissing is false, that is to say, if the attribute is not configured, the automatic configuration does not take effect. If matchIfMissing is true, it means that automatic configuration takes effect by default if no corresponding attribute is configured.

Let’s look at the name attribute, which is used to read a property value from application.properties. For example, the Tomcat automatic configuration file is:

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSourceCopy the code

When matchIfMissing is false, false is returned if name is null; If name is not empty, the value is compared to the value specified by havingValue, returning true if the same, false otherwise. Returning false means that automatic configuration does not take effect.

Found the property on the configuration, but if you look at HttpEncodingAutoConfiguration has not completely in accordance with the above name and havingValue cooperate to use. It is configured with “prefix+value” as the name of the property:

spring.http.encoding.enabled=trueCopy the code

Prefix specifies the unified prefix spring.http.encoding, and value specifies the specific attribute name as Enabled. HavingValue is not set here. If havingValue is not specified, by default the value set in the property configuration to true (as configured above) takes effect, and false does not.

CONDITIONALONPROPERTY in SPRING BOOT


Program new horizon