86

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

vegemite4me
  • 6,621
  • 5
  • 53
  • 79
user3409583
  • 887
  • 1
  • 7
  • 9

3 Answers3

145

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.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
6

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

Jaroslav Záruba
  • 4,694
  • 5
  • 39
  • 58
0

Rather, it is the opposite. A precondition for implementing the method, if the property is set in the environment (development, approval, production) and is true value with the method can be executed.

If the property is not set in the environment annotation not prevented the execution of the method.

  • I think you have expressed this in a confusing way. I guess English is not your natural language (Pt?). I use Google Translate, and feed the first result back through the translation. If rubbish, I simplify the original and try again, until something OK re-emerges. Also could you provide an example of the use for running the different environments that you mention, please? – Bill Woodger Oct 17 '14 at 08:15
  • You are correct. I'm brazilian and I'm not good in writing skills. – Anderson Marques Oct 18 '14 at 01:11
  • This may interest you if you don't know about it. http://pt.stackoverflow.com/. There's only been a couple of Mainframe questions there, but you have other experience as well. My Portuguese is pretty rotten, so I use Google Translate and get a lot of help from the good people there. – Bill Woodger Oct 18 '14 at 06:27
  • With this question, if you can provide a short code-example to show what you mean, I can edit the text. – Bill Woodger Oct 18 '14 at 09:06