什么是@ConditionalOnProperty注释的目的是什么? [英] What is purpose of @ConditionalOnProperty annotation?

查看:13591
本文介绍了什么是@ConditionalOnProperty注释的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚修改后的弹簧引导配置,和遇到@ConditionalOnProperty(preFIX =spring.social,值=自动连接意见),从org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration。 java的

I just modified spring boot configuration, and encountered @ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views") from org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration.java

    @Bean(name = { "connect/twitterConnect", "connect/twitterConnected" })
    @ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
    public View twitterConnectView() {
        return new GenericConnectionStatusView("twitter", "Twitter");
    }

我不明白这个注解的目的。我想这可能是使用Bean只有当属性值存在(例如,spring.social,自动连接意见)。

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").

推荐答案

注解用于有条件地创建一个Spring bean依赖于一个属性的配置。在使用您曾表示,如果 spring.social.auto连接的视图属性存在bean将只被创建的问题,它与<以外的值code>假。这意味着,要创建的这个查看豆,你需要设置 spring.social.auto连接的视图属性,它必须有false以外的值。

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-views property exists and it has a value other than false. This means that, for this View bean to be created, you need to set the spring.social.auto-connection-views property and it has to have a value other than false.

您可以找到这个注释的整个春季启动code碱基许多其他用途。另一个例子是:

You can find numerous other uses of this annotation throughout the Spring Boot code base. Another example is:

@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
public AmqpAdmin amqpAdmin(CachingConnectionFactory connectionFactory) {
    return new RabbitAdmin(connectionFactory);
}

请注意使用 matchIfMissing 的。在这种情况下,如果 spring.rabbitmq.dynamic 属性存在并且比其它的值 AmqpAdmin bean将被创建的属性不存在一样。这使得豆选择退出,而不是问题的例子是选择在创建。

Note the use of matchIfMissing. In this case the AmqpAdmin bean will be created if the spring.rabbitmq.dynamic property exists and has a value other than false or the 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.

这篇关于什么是@ConditionalOnProperty注释的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆