1

How can I write consul configuration class on the conditional property, I have done and tested with properties,

But want to write configuration class and that configuration should be load on condition.

Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44

1 Answers1

2

If I got your question right, you want to create a configuration class but load this configuration only if some condition is met:

@Configuration
public class MyConfiguration {
      @Bean 
      public ??? myBean() {
         ....
      }
}

In this case, you should use @Conditional annotation that has been firstly shipped with Spring 4.

There are many @Conditional-like annotations, you should just peek the one you need.

  • @ConditionalOnClass
  • @ConditionalOnProperty
  • @ConditionalOnMissingProperty
  • @ConditionalOnWebApplication
  • @ConditionalOnBean
  • @ConditionalOnMissingBean
  • @ConditionalOnClass
  • .... (there are others probably)

If it's not enough you can even roll your own Conditional logic. Since it wasn't asked directly in the question, I'll just provide a link (one among many) articles that shows how to do it.

So you can go with, for example:

@Configuration
@ConditionalOnProperty(value="my.consul.integration.enabled", havingValue = "true")
public class MyConfiguration {
      @Bean 
      public ??? myBean() {
         ....
      }
} 

You can also put the conditional logic at the granularity of a single bean.

Here the requirement is running the consul configuration only if a certain property is enabled (tools.consul.enabled=true from your comments)

The idea is to define a set of consul related properties not in a default application.yml file but somewhere else, for example consul-integration.properties file, and read this file with configuration that is turned on only if a @ConditionOnProperty is met.

Example:

@Configuration
@ConditionalOnProperty(value="tools.consul.enabled", havingValue=true) 
@PropertySources({
 @PropertySource("classpath:consul-integration.properties) 
})
public class ToolConsulSampleConfiguration {

}
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • It is helpful, I know about conditional property of spring boot, I want specifically for spring-cloud-consul, how can I create configuration class for that – Ravat Tailor Jul 02 '18 at 08:58
  • Oh, I see now. Why don't you just use a starter of consul which is available anyway: https://cloud.spring.io/spring-cloud-consul/ If you need a condition on it just take a look at its source code, its configurations have already a bunch of conditionals on their own. So you might want to adjust your project to use them. And if you want to "override" any of its beans, you can do that with your own configuration class – Mark Bramnik Jul 02 '18 at 09:23
  • my concerns, in bootstrap.yml I have, provides all the necessary properties, but they will load when an application will be up but I want to load them on when I will have my own property like tools.consul.enabled=true, then all the properties should load otherwise not, is there any way to do that – Ravat Tailor Jul 02 '18 at 09:41
  • Hi, I've updated the answer to address your use case – Mark Bramnik Jul 03 '18 at 04:53