http://stackoverflow.com/questions/26394778/what-is-purpose-of-conditionalonproperty-annotation

****************************************************

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");
}

answers

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.

如果property spring.social.auto-connection-views存在,并且值不为false,创建bean.

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);
}

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.

如果property spring.rabbitmq.dynamic存在,并且值不为false,创建bean

matchIfMissing = true, 如果改属性条目不存在,创建bean.

What is purpose of @ConditionalOnProperty annotation?的更多相关文章

  1. Creating your own auto-configuration

    44. Creating your own auto-configuration If you work in a company that develops shared libraries, or ...

  2. Spring Boot Reference Guide

    Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch,  ...

  3. SpringBoot - Starter

    If you work in a company that develops shared libraries, or if you work on an open-source or commerc ...

  4. Jersey MVC

    Jersey是JAX-RS(JavaAPI for RESTful Service)标准的一个实现,用于开发RESTful Web Application.可以参考JAX-RS的介绍(http://w ...

  5. interface类型

    接口可使用的修饰符如下: InterfaceModifier: one of Annotation public protected private abstract static strictfp  ...

  6. Spring Boot文档

    本文来自于springboot官方文档 地址:https://docs.spring.io/spring-boot/docs/current/reference/html/ Spring Boot参考 ...

  7. 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure

    相信用过Selenium WebDriver 的朋友都应该知道如何使用WebDriver API实现Take Screenshot的功能. 在这篇文章里,我主要来介绍对failed tests实现 t ...

  8. Spring Annotation Processing: How It Works--转

    找的好辛苦呀 原文地址:https://dzone.com/articles/spring-annotation-processing-how-it-works If you see an annot ...

  9. Spring ConditionalOnProperty

    Spring Annotation @ConditionalOnProperty spring doc解释 @Conditional: Indicates that a component is on ...

随机推荐

  1. 在Spark中尽量少使用GroupByKey函数(转)

    原文链接:在Spark中尽量少使用GroupByKey函数 为什么建议尽量在Spark中少用GroupByKey,让我们看一下使用两种不同的方式去计算单词的个数,第一种方式使用reduceByKey  ...

  2. 数学图形(2.5)Loxodrome曲线

    这也是一种贴在球上的曲线 #http://www.mathcurve.com/courbes3d/loxodromie/sphereloxodromie.shtml vertices = 1000 t ...

  3. Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space

    在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" ...

  4. 数值格式化 NumberFormat DecimalFormat RoundingMode

    NumberFormat [简介] java.text.NumberFormat extends java.text.Format extends java.lang.Object 实现的接口:Ser ...

  5. IE系列不支持圆角等CSS3属性的解决方案

    IE系列浏览器不支持CSS的圆角(border-radius)等CSS3属性是一个众所周知的问题,而FF.Chrome原生的浏览器支持是的优雅的圆角感觉较好,如何解决这个问题呢? 一种比较传统的方案是 ...

  6. 基于restful注解(spring4.0.2整合flex+blazeds+spring-mvc)<一>

    摘自: http://www.blogjava.net/liuguly/archive/2014/03/10/410824.html 参考官网:1.http://livedocs.adobe.com/ ...

  7. PHP curl 抓取AJAX异步内容

    其实抓ajax异步内容的页面和抓普通的页面区别不大.ajax只不过是做了一次异步的http请求,只要使用firebug类似的工具,找到请求的后端服务url和传值的参数,然后对该url传递参数进行抓取即 ...

  8. 关于C#程序优化的五十种方法

    关于C#程序优化的五十种方法    这篇文章主要介绍了C#程序优化的五十个需要注意的地方,使用c#开发的朋友可以看下   一.用属性代替可访问的字段 1..NET数据绑定只支持数据绑定,使用属性可以获 ...

  9. (剑指Offer)面试题58:二叉树的下一个结点

    题目: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 思路: 考虑中序遍历的过程, 如果当前结点存在右子节点, ...

  10. C#基础视频教程7.3 如何编写简单游戏

    前面我们大致实现了鸟的一圈轨迹(其实如果你不做这个,就用两个矩形块的碰撞检测代替也可以),跟所有前面的教程一样,草稿打完了就要设计封装成一个类.至少到目前为止我们已经知道了鸟的属性和方法,先不要管方法 ...