In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use @ ConditionalOnProperty in SpringBoot, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
Use in Spring Boot
@ ConditionalOnProperty annotations are widely used in Spring Boot source code, such as automatic configuration of Http encoding, automatic configuration of data source types, and so on.
Part of the source code in the HttpEncodingAutoConfiguration class:
@ 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 the internal code}
Part of the code in 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.DataSource", matchIfMissing = true) static class Tomcat {/ / omit the internal code}
Obviously, the above two automatic configuration classes use @ ConditionalOnProperty to control whether the automatic configuration takes effect, so let's take a look at its source code and specific use.
@ ConditionalOnProperty source code description
The source code of @ ConditionalOnProperty annotation class is as follows:
@ Retention (RetentionPolicy.RUNTIME) @ Target ({ElementType.TYPE, ElementType.METHOD}) @ Documented@Conditional (OnPropertyCondition.class) public @ interface ConditionalOnProperty {/ / array to get the value of the corresponding property name. String [] value () default {} cannot be used with name at the same time; / / configure the prefix of attribute name, such as spring.http.encoding String prefix () default "" / / array, the full name or partial name of the configuration attribute can be combined with prefix to form a complete configuration attribute name. String [] name () default {} cannot be used with value at the same time; / / can be combined with name to compare whether the obtained attribute value is the same as the value given by havingValue before loading the configuration String havingValue () default ""; / / whether it can be loaded when the configuration attribute is missing. If true, it will load normally without this configuration property; otherwise, it will not take effect boolean matchIfMissing () default false;}
There is also a relaxedNames property in the historical version:
/ / whether boolean relaxedNames () default true can be loosely matched
This property no longer exists in the latest version.
By annotating the @ Conditional (OnPropertyCondition.class) code on ConditionalOnProperty, you can see that ConditionalOnProperty belongs to the derivative annotation of @ Conditional. The effective conditions are judged by OnPropertyCondition.
Usage
We have seen the use of @ ConditionalOnProperty in Spring Boot above.
The core functionality of @ ConditionalOnProperty is achieved through the properties name and havingValue.
First, take a look at the matchIfMissing attribute, which is used to specify the default processing if the corresponding property is not configured in the configuration file: matchIfMissing is false by default, that is, if the property is not configured, the automatic configuration does not take effect. If matchIfMissing is true, it means that automatic configuration takes effect by default if there is no corresponding property configuration.
Let's take a look at the name property, which is used by name to read a property value from application.properties. For example, the automatic configuration of Tomcat above is as follows:
Spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
If matchIfMissing is false, false; is returned if name is not empty, the value is compared with the value specified by havingValue if it is not empty, true is returned if it is the same, false is returned otherwise. Returning false means that the automatic configuration will not take effect.
However, if you look at the property configuration on the HttpEncodingAutoConfiguration class, you will find that it is not exactly used in conjunction with name and havingValue as mentioned above. It is configured with "prefix+value" as the name of the property:
Spring.http.encoding.enabled=true
Where prefix specifies the configured uniform prefix "spring.http.encoding" and value specifies the specific attribute name "enabled". The value of havingValue is not set here. If havingValue does not specify a value, the value set in the property configuration is true by default (as configured above), but false does not take effect.
The above is all the content of the article "how to use @ ConditionalOnProperty in SpringBoot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 205
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.