Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

@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").

like image 617
user3409583 Avatar asked Oct 16 '14 01:10

user3409583


People also ask

What is @conditional annotation in spring?

Annotation Type Conditional The @Conditional annotation may be used in any of the following ways: as a type-level annotation on any class directly or indirectly annotated with @Component , including @Configuration classes. as a meta-annotation, for the purpose of composing custom stereotype annotations.

What is the use of Spring Boot annotations?

Spring Boot Annotations is a form of metadata that provides data about a program. In other words, annotations are used to provide supplemental information about a program. It is not a part of the application that we develop. It does not have a direct effect on the operation of the code they annotate.

What is the use of @configuration annotation?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

What do we use conditional beans for?

Why do we need Conditional Beans? A Spring application context contains an object graph that makes up all the beans that our application needs at runtime. Spring's @Conditional annotation allows us to define conditions under which a certain bean is included into that object graph.


2 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.

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.

like image 153
Andy Wilkinson Avatar answered Sep 17 '22 17:09

Andy Wilkinson


In case you are using this property on TYPE-level, i.e. on one of your @Configuration classes... Keep in mind that in such case the annotation is evaluated/checked against the default properties file, i.e. application.properties

@ConditionalOnProperty on TYPE level w/ @Configuration

like image 44
Jaroslav Záruba Avatar answered Sep 18 '22 17:09

Jaroslav Záruba