我对Sprint很新,我使用Spring 3.x和roo1.1.1作为我的应用程序.
我有一个接口的多个实现,它将@Autowired到其他不同的类中.我只能决定在运行时使用哪个实现.这应该像工厂模式一样实现.
public interface SomeInterface {
public void doSomething();
}
Run Code Online (Sandbox Code Playgroud)
实施1.
public class SomeOb implements SomeInterface {
public void doSomething() {
//Do something for first implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
实施2.
public class SomeOtherOb implements SomeInterface {
public void doSomething() {
//Do something for first implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
现在在我的服务中,我需要这样的Autowired
@Service
public class MyService {
@Autowired
SomeInterface ob;
//Rest of the code here
}
Run Code Online (Sandbox Code Playgroud)
1)选择哪个实现为Autowired的逻辑只知道运行时,所以我不能使用@Qualifier注释来限定它.2)我尝试创建一个像FactoryBean
public class SomeFactoryBean implements FactoryBean<SomeInterface> {
@Override
public SomeInterface getObject() throws Exception {
if(/*Somecondition*/) { …Run Code Online (Sandbox Code Playgroud)