@Autowired is actually perfect for this scenario. You can either autwire a specific class (implementation) or use the interface.
Consider the following example:
public interface Item { } @Component("itemA") public class ItemImplA implements Item { } @Component("itemB") public class ItemImplB implements Item { }
Now you can choose which of these implementations will be used simply by choosing a name for the object according to the @Component annotation value
Like this:
@Autowired private Item itemA;
To create the same instance several times, you can use the @Qualifier annotation to indicate which implementation will be used:
@Autowired @Qualifier("itemA") private Item item1;
If you need to instantiate elements with specific constructor parameters, you will need to specify its XML configuration file. A good tutorial on using qulifiers and autwiring can be found HERE .
Smile
source share