[英]Spring Multiple Implementation of same interface
我有一個接口和多個實現類,大約10個,這個接口。
我有一個命名約定,如prefix + name + suffix
所以在運行時,我可以添加@Autowired private Map<String, MyInterface> myImplementations;
然后使用myImplementations.get()
方法訪問實現類。
有沒有更好的方法來訪問這些實現? 我只知道哪個impl。 我需要在運行時,更改取決於我收到的消息。
您可以在類中實現BeanFactoryAware接口,然后使用注入的bean工廠來獲得所需的實現:
Interface impl = beanFactory.getBean("interfaceimpl");
要么
Interface impl = beanFactory.getBean(InterfaceImpl.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.