I have an Interface and multiple implementation. I'm auto wiring the interface in classes for usage. I need to choose different implementation at runtime.
public class Util {
public void getClient();
}
Implementations
public class UtilOne implements Util {
public void getClient() {...}
}
public class UtilTwo implements Util {
public void getClient() {...}
}
@Configuration
public class AppConfig {
@Autowired
@Bean
@Primary
public Util utilOne() {
return new UtilOne();
}
@Autowired
@Bean
public Util utilTwo() {
return new UtilTwo();
}
}
@Component
public class DemoService {
@Autowired
private Util util;
}
For some reason if we are unable to get client in UtilOne, I want to switch to UtilTwo without restarting the app. I want to change the Util object in DemoService to UtilTwo object.
Property active.util will come from DB and can we updated from UI.