Spring Multiple implementations of the same interface

I have an interface and several implementation classes, about 10, of this interface.

I have a naming convention, like prefix + name + suffix , so at runtime I can add @Autowired private Map<String, MyInterface> myImplementations; and then access the implementation class using the myImplementations.get() method.

Is there a better way to access these implementations? I only know which imp. I need it at runtime, changes depend on the message I received.

+5
source share
1 answer

You can implement the BeanFactoryAware interface in your class, and then use the introduced bean factory to get the necessary implementation:

 Interface impl = beanFactory.getBean("interfaceimpl"); 

or

 Interface impl = beanFactory.getBean(InterfaceImpl.class); 
+3
source

All Articles