@RefreshScope与@ConditionalOnProperty不起作用 [英] @RefreshScope with @ConditionalOnProperty does not work

查看:2167
本文介绍了@RefreshScope与@ConditionalOnProperty不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

探讨一种根据需要启用/禁用 @RequestMapping 终结点而无需重新启动JVM的选项.在运行时在端点上实现kill开关.

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

尝试

我尝试了以下操作,但 @RefreshScope 似乎不适用于 @ConditionOnProperty

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);
    }

}

使用以下属性更新属性

POST /env?stackoverflow.endpoints=anyvalue

并使用

POST /refresh

这时,控制器返回更新的值.

At this point the controller returns the updated value.

问题

还有其他方法可以实现所需的功能吗?

Are there any other ways to achieve the desired functionality?

推荐答案

Boot中的条件仅在Configuration类级别或Bean定义级别应用.

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 ();
 }
}

这篇关于@RefreshScope与@ConditionalOnProperty不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆