What is purpose of @ConditionalOnProperty annotation?
来源:互联网 发布:c 知乎 编辑:程序博客网 时间:2024/05/14 23:24
http://stackoverflow.com/questions/26394778/what-is-purpose-of-conditionalonproperty-annotation
****************************************************
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"); }
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").
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 thespring.social.auto-connection-views
property exists and it has a value other thanfalse
. This means that, for this View
bean to be created, you need to set thespring.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 thespring.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?
- What Is the Purpose of This Book?
- what is the purpose of __ASSEMBLY__?
- What is the purpose of the USB?
- What is the purpose of /etc/default?
- 27What is purpose of delegates?
- what is the purpose of using translatable in Android strings?
- What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?
- What is the purpose of using do {...} while (0) in macros?
- ccah-500 第47题 What is the purpose of ZooKeeper in such a configuration
- What is the use of "?
- what is Freedom of expression ?
- purpose of package size
- The Purpose of Education
- the purpose of education
- what is AttributeOverride annotation used for in Hibernate
- What is the key of Data Assimilation?
- What is this "def" I heard of?
- HEXO+Github,搭建属于自己的博客
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)
- UVA 156 Ananagrams(stl,map,set)
- 字幕文件批量重命名脚本(二) —— Linux
- 项目及人员管理系统
- What is purpose of @ConditionalOnProperty annotation?
- 解决viewpager自动轮播速度变快的问题
- ZOJ 3601 Unrequited Love【STL】
- 十六、Hive 表的创建及测试
- python机器学习库入门之pandas
- 原型模式——prototype
- 高性能业务架构解决方案(LVS+Keepalived)
- Day 1
- hoj 2662