首页 技术 正文
技术 2022年11月18日
0 收藏 388 点赞 2,422 浏览 1647 个字

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.
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838