A class with @Bean or @Component will be loaded by spring when launched through the bean visitor mechanism. To exclude this bean from the spring context at startup, you can create a BeanPostProcessor ( here ) and check the highlighted annotation, BUT as far as I understand, you cannot return the bean to the context at runtime.
As a result, you must make this bean “smart” in order to perform the correct / mock operation (or send HTTP code 503) when requests are received.
FF4j can really help you implement this behavior, but not with one annotation on top of your REST controller. What could you do:
- Create an interface, annotate the interface using the special annotation FF4J
- Create 2 interface implementations, each time with a different name
- Use FF4J to select an implementation or at run time.
Here is the code snippet to get the idea:
public interface GreetingService { @Flip(name = "theFeatureIDToToggle", alterBean = "greeting.french") String sayHello(String name); } @Component("greeting.french") public class GreetingServiceFrenchImpl implements GreetingService { public String sayHello(String name) {return "Bonjour " + name; } @Component("greeting.english") public class GreetingServiceEnglishImpl implements GreetingService { public String sayHello(String name) {return "Hello " + name; }
You can switch one method or the whole class, since you want all the samples to be available here .
source share