@RefreshScope with @ConditionalOnProperty does not work
Asked Answered
I

2

7

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?

Intertidal answered 18/9, 2017 at 3:8 Comment(1)
Basically what you want to do is not possible. The link below explains why. github.com/spring-cloud/spring-cloud-config/issues/1645Lipfert
T
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 ();
 }
}
Triglyph answered 16/10, 2019 at 14:50 Comment(0)
N
0

As Spenser Gibb said here, this behavior is not supported.

Northwester answered 17/3, 2022 at 4:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.