【问题标题】:@RefreshScope with @ConditionalOnProperty does not work@RefreshScope 和 @ConditionalOnProperty 不起作用
【发布时间】:2018-02-26 12:06:13
【问题描述】:

问题

探索在不重新启动 JVM 的情况下按需启用/禁用 @RequestMapping 端点的选项。在运行时在端点上实现终止开关。

尝试

我尝试了以下方法,但 @RefreshScope 似乎不适用于 @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

此时控制器返回更新后的值。

问题

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

【问题讨论】:

标签: spring-boot environment-variables runtime spring-cloud spring-boot-actuator


【解决方案1】:

Boot 中的条件仅适用于 Configuration 类级别或 bean 定义级别。

这可能会对你有所帮助。

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

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 2017-12-21
    • 2017-03-06
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2019-04-01
    相关资源
    最近更新 更多