我有以下课程:
public interface MyInterface{}
public class MyImpl1 implements MyInterface{}
public class MyImpl2 implements MyInterface{}
public class Runner {
@Autowired private MyInterface myInterface;
}
我想要做的是决定,当应用程序已经在运行(即不是在启动时)时,应该将哪个实现注入Runner
.
所以理想情况下是这样的:
ApplicationContext appContext = ...
Integer request = ...
Runner runner = null;
if (request == 1) {
//here the property 'myInterface' of 'Runner' would be injected with MyImpl1
runner = appContext.getBean(Runner.class)
}
else if (request == 2) {
//here the property 'myInterface' of 'Runner' would be injected with MyImpl2
runner = appContext.getBean(Runner.class)
}
runner.start();
实现这一目标的最佳方法是什么?