I am new to Sprint and am using Spring 3.x and roo1.1.1 for my application.
I have several implementations of an interface that will be @Autowired in other classes. I could only decide which implementation to work with at runtime. This should be achieved using the factory pattern.
public interface SomeInterface { public void doSomething(); }
Implementation 1.
public class SomeOb implements SomeInterface { public void doSomething() {
Implementation 2.
public class SomeOtherOb implements SomeInterface { public void doSomething() {
Now in my service I need this Autwired, as
@Service public class MyService { @Autowired SomeInterface ob;
1) The logic of choosing which implementation should be Autowired is to only know the runtime, so I cannot use the @Qualifier annotation to qualify it. 2) I tried to create a FactoryBean, for example
public class SomeFactoryBean implements FactoryBean<SomeInterface> { @Override public SomeInterface getObject() throws Exception { if() { return new SomeOb(); } else return new SomeOtherOb(); } @Override public Class<? extends SomeInterface> getObjectType() { if() { return SomeOb.class; } else return SomeOtherOb.class; } @Override public boolean isSingleton() { return false; } }
A tag is specified in applicationContext.xml.
When I start the web server, I run an error like
No unique bean of type [com.xxxx.xxxx.SomeInterface] is defined: expected single matching bean but found 3: [xxxx, xxxxxxx, xxxxFactory]
Can anyone help me solve this problem. If I donβt do it right, ask me to do it right.
Thanks and appreciate any help, JJK