I have an Interface and multiple implementation classes, around 10, of this interface.
I have a naming convention like prefix + name + suffix
so during runtime, I can add
@Autowired
private Map<String, MyInterface> myImplementations;
and then access the implementation class with myImplementations.get()
method.
Is there a better way of accessing those implementations? I only know which impl. I needed during runtime, changes depends on the message I received.
You can implement BeanFactoryAware interface in your class and then use injected bean factory to get needed implementation:
Interface impl = beanFactory.getBean("interfaceimpl");
or
Interface impl = beanFactory.getBean(InterfaceImpl.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With