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. PHP中多IP段权限控制方案

    在某些项目中我们可能会用到根据IP段进行权限校验,比如不在我们配置的IP段内的用户访问某些页面或功能模块时,将提示其权限不够并禁止访问该页面的内容.鉴于项目中需求各异,下面只说下大致思路以及我个人的实 ...

  2. scala for循环

    scala for循环功能强大啊,for条件可以写各种表达式 通过一个demo来看一下,这个是一个讲yield关键字的demo:<Scala中的yield> object YieldDem ...

  3. netty-socketio之BroadcastOperations

    最近用到了netty-socketio.之前对这个了解比较少,对netty了解比较多,看代码的时候,发现二者不是一个东西(废话). 主要让我比较惊讶的区别是BroadcastOperations这个东 ...

  4. Framework元数据向导错误之BMT-MD-6001与BMT-IMP-0002

    1:错误BMT-MD-6001的两种处理方法 在Framework中创建Project后,运行元数据向导从数据库连接中向物理层导入数据表,报错如下 环境如下:Framework版本:10.2.0,Co ...

  5. PHP封装成应用程序的三个方案

    大概有三个解决方案吧. 1 如果你精通PHP,你可以看看这个网站:http://gtk.php.net/ 很明显,这个网站介绍了啥是GTK,你可以构件跨平台的图形化应用程序,当然你要再学一门语言,去掌 ...

  6. uitabbar 标题设置 button text attributes only respected for UIControlStateNormal

    uitabbar 标题设置 button text attributes only respected for UIControlStateNormal [[UITabBarItem appearan ...

  7. function.length和arguments的区别

    function.length:接收到函数体外的参数计算长度 arguments:接收到函数体内的参数计算长度 /** * 函数参数长度和伪数组(arguments)长度不一样! -> 接收到函 ...

  8. Python3.2官方文档翻译--迭代器

    6.9 迭代器 到眼下为止.你可能已注意到很多容器对象都能够用for语句进行循环: 这样的訪问风格清楚简洁方便. 迭代器的应用是python遍历统一.在这样的场景背后.for语句调用容器对象iter( ...

  9. 算法笔记_028:字符串转换成整数(Java)

    1 问题描述 输入一个由数字组成的字符串,请把它转换成整数并输出.例如,输入字符串“123”,输出整数123. 请写出一个函数实现该功能,不能使用库函数. 2 解决方案 解答本问题的基本思路:从左至右 ...

  10. ionic build android 失败 及 解决方案

    原因:没有接受以下SDK组件的许可协议 解决方案: install Android Support Repository