I just modified spring boot configuration, and encountered
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
from org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration
@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").
Best Answer
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 thanfalse
. This means that, for thisView
bean to be created, you need to set thespring.social.auto-connection-views
property and it has to have a value other than false.You can find numerous other uses of this annotation throughout the Spring Boot code base. Another example is:
Note the use of
matchIfMissing
. In this case theAmqpAdmin
bean will be created if thespring.rabbitmq.dynamic
property exists and has a value other thanfalse
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.