@RefreshScope with @ConditionalOnProperty does not work

房东的猫 提交于 2020-01-03 09:09:16

问题


Problem

Exploring an option of enabling/disabling a @RequestMapping endpoint on demand without restarting JVM. Implement a kill switch on a endpoint at runtime.

Attempt

I've tried the following but @RefreshScope does not seem to work with @ConditionOnProperty

@RestController
@RefreshScope
@ConditionalOnProperty(name = "stackoverflow.endpoints", havingValue = "enabled")
public class MyController {

    @Value("${stackoverflow.endpoints}")
    String value;

    @RequestMapping(path = "/sample", method = RequestMethod.GET)
    public ResponseEntity<String> process() {
        return ResponseEntity.ok(value);
    }

}

Updated property using

POST /env?stackoverflow.endpoints=anyvalue

And reloaded context using

POST /refresh

At this point the controller returns the updated value.

Question

Are there any other ways to achieve the desired functionality?


回答1:


Conditions in Boot are only applied at the Configuration class level or at the bean definition level.

This might help you.

public class MyWebConfiguration extends WebMvcConfigurationSupport {
 @Bean
 @ConditionalOnProperty(prefix="stackoverflow.endpoints",name = "enabled")
 public MyController myController() {
    return new MyController ();
 }
}


来源:https://stackoverflow.com/questions/46271052/refreshscope-with-conditionalonproperty-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!