pokemzok
pokemzok

Reputation: 1709

@ConditionalOnExpression enabling/disabling @RestController

I am trying to enable/disable controller depending on value in properties file. My Controller looks like this:

@RestController
@ConditionalOnExpression("${properties.enabled}")
public class Controller{
  public String getSomething() {
    return "Something";
  }
}

My properties file looks like this:

properties.enabled= false

And controller is always enabled (I can access method getSomething). I also tried combinations like this:

@ConditionalOnExpression("${properties.enabled:true}")
@ConditionalOnExpression("${properties.enabled}==true")
@ConditionalOnExpression("${properties.enabled}=='true'")
@ConditionalOnExpression("'${properties.enabled}'=='true'")

Edit: Also tried different annotation:

@ConditionalOnProperty(prefix = "properties", name="enabled")

Upvotes: 2

Views: 2571

Answers (1)

pokemzok
pokemzok

Reputation: 1709

I finally found the problem. This Bean wasn't created by Spring but in WebConfiguration class so i had to also add annotation there

public class CommonWebConfiguration extends WebMvcConfigurationSupport {
 @Bean
 @ConditionalOnProperty(prefix="properties",name = "enabled")
 public Controller controller() {
    return new Controller ();
 }
}

My Controller now looks like this:

@RestController
@ConditionalOnProperty(prefix="properties",name = "enabled")
public class Controller{
  public String getSomething() {
   return "Something";
  }
}

Upvotes: 2

Related Questions