What is purpose of @ConditionalOnProperty annotation?
2017-02-12 20:08
537 查看
http://stackoverflow.com/questions/26394778/what-is-purpose-of-conditionalonproperty-annotation
****************************************************
I just modified spring boot configuration, and encountered
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
如果property
You can find numerous other uses of this annotation throughout the Spring Boot code base. Another example is:
Note the use of
如果property spring.rabbitmq.dynamic存在,并且值不为false,创建bean
****************************************************
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-viewsproperty exists and it has a value other than
false. This means that, for this
Viewbean to be created, you need to set the
spring.social.auto-connection-viewsproperty 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
AmqpAdminbean will be created if the
spring.rabbitmq.dynamicproperty exists and has a value other than
falseor 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 channel coding?(信道编码的作用?)
- What is the purpose of the USB?
- The State of Visual Analytics Views on what visual analytics is and where it is going
- OpenMPI, LSF, InfiniBand, Mellanox OFED and Intel MPI Benchmark: what is going on ?
- What is blocker on the road of learning English?
- What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?
- What is the purpose of /etc/default?
- VMware安装Ubuntu17.04的VMware-tools提示:what is the location of the "ifconfig" program on your machin问题解决
- what is the purpose of __ASSEMBLY__?
- What is the use of the res/values/public.xml file on Android?
- what is the purpose of using translatable in Android strings?
- What is the purpose of using do {...} while (0) in macros?
- linux安装问题:what is the location of the gcc program on your machine
- What is the diference between putting a property on application.yml or bootstrap.yml in spring boot?
- what is the purpose of the rptproj.user file
- linux安装问题:what is the location of the gcc program on your machine
- 安装vmware-tools出现"what is the location of the "ifconfig"program on your machine?"
- 27What is purpose of delegates?
- What is impact of cloud on ITSM?