spring @ConditionalOnProperty 注释的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26394778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is purpose of @ConditionalOnProperty annotation?
提问by user3409583
I just modified spring boot configuration, and encountered
我刚刚修改了spring boot配置,遇到了
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
from org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration
从 org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration
@Bean(name = { "connect/twitterConnect", "connect/twitterConnected" })
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
public View twitterConnectView() {
return new GenericConnectionStatusView("twitter", "Twitter");
}
I don't understand purpose of this annotation. I guess this might be enable to use bean only if property value exist(e.g. "spring.social", "auto-connection-views").
我不明白这个注释的目的。我想这可能只有在属性值存在时才能使用 bean(例如“spring.social”、“auto-connection-views”)。
回答by Andy Wilkinson
The annotation is used to conditionally create a Spring bean depending on the configuration of a property. In the usage you've shown in the question the bean will only be created if the spring.social.auto-connection-viewsproperty exists and it has a value other than false. This means that, for this Viewbean to be created, you need to set the spring.social.auto-connection-viewsproperty and it has to have a value other than false.
注释用于根据属性的配置有条件地创建 Spring bean。在您在问题中显示的用法中,仅当spring.social.auto-connection-views属性存在并且它的值不是false. 这意味着,View要创建此bean,您需要设置该spring.social.auto-connection-views属性,并且它必须具有非 false 的值。
You can find numerous other uses of this annotation throughout the Spring Boot code base. Another example is:
您可以在整个 Spring Boot 代码库中找到此注释的许多其他用途。另一个例子是:
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
public AmqpAdmin amqpAdmin(CachingConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
Note the use of matchIfMissing. In this case the AmqpAdminbean will be created if the spring.rabbitmq.dynamicproperty exists and has a value other than falseorthe property doesn't exist at all. This makes the creation of the bean opt-out rather than the example in the question which is opt-in.
注意使用matchIfMissing. 在这种情况下,AmqpAdmin如果该spring.rabbitmq.dynamic属性存在并且具有不同于该属性的值false或该属性根本不存在,则将创建 bean 。这使得 bean 的创建选择退出,而不是问题中选择加入的示例。
回答by Jaroslav Záruba
In case you are using this property on TYPE-level, i.e. on one of your @Configuration classes... Keep in mind that in such case the annotation is evaluated/checked against the default properties file, i.e. application.properties
如果您在 TYPE 级别上使用此属性,即在您的 @Configuration 类之一上...请记住,在这种情况下,将根据默认属性文件对注释进行评估/检查,即 application.properties
回答by Anderson Marques
Rather, it is the opposite. A precondition for implementing the method, if the property is set in the environment (development, approval, production) and is true value with the method can be executed.
恰恰相反。执行该方法的前提条件,如果该属性在环境(开发、批准、生产)中设置并且是该方法的真值,则可以执行该方法。
If the property is not set in the environment annotation not prevented the execution of the method.
如果未在环境注释中设置该属性,则不会阻止方法的执行。

