我有一个接口(interface)和这个接口(interface)的多个实现类,大约有 10 个。
我有一个像 prefix + name + suffix
这样的命名约定所以在运行时,我可以添加
@Autowired
private Map<String, MyInterface> myImplementations;
然后使用 myImplementations.get()
访问实现类方法。
有没有更好的方法来访问这些实现?我只知道哪个暗示。我在运行时需要,更改取决于我收到的消息。
最佳答案
你可以实现BeanFactoryAware类中的接口(interface),然后使用注入(inject)的 bean 工厂来获得所需的实现:
Interface impl = beanFactory.getBean("interfaceimpl");
或
Interface impl = beanFactory.getBean(InterfaceImpl.class);
关于java - 同一接口(interface)的Spring多重实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31401034/