[英]how do i get @Service bean by its default qualifier bean name in spring
I have many ServiceImpl beans but I want to access specific bean by its default name like 我有很多ServiceImpl bean但我想通过它的默认名称来访问特定的bean
class SuperServiceImpl {}
class UserServiceImpl extends SuperServiceImpl {}
class PlaceServiceImpl extends SuperServiceImpl {}
public SuperServiceImpl getServiceImpl(String qualifierName) {
// if qualifierName="userServiceImpl"
// return UserServiceImpl
}
I tried to implement following solution 我试图实施以下解决方案
Auto-cast Spring Beans 自动浇铸弹簧豆
but I'm not getting getBean()
method on ApplicationContext
reference. 但是我没有在
ApplicationContext
引用上获得getBean()
方法。
return applicationContext.getBean(qualifierName, SuperServiceImpl.class)
you may want to define an interface for SuperServiceImpl
and use return applicationContext.getBean(qualifierName, SuperService.class)
instead, especially if you have multiple implementations. return applicationContext.getBean(qualifierName, SuperServiceImpl.class)
您可能希望为SuperServiceImpl
定义一个接口并使用return applicationContext.getBean(qualifierName, SuperService.class)
,尤其是如果您有多个实现。
Of course, to make that work you need to define your beans properly: 当然,为了完成这项工作,您需要正确定义bean:
id
attribute of the bean
element bean
元素的id
属性 name
attribute of the @Bean
annotation @Bean
批注的name
属性覆盖它 Found easiest solution.. 找到最简单的解决方案
1)I autowired ApplicationContext 1)我自动连接了ApplicationContext
2)Passed default qualifier name to method getBean(defaultQualifierName) 2)将默认限定符名称传递给方法getBean(defaultQualifierName)
thats it.. 而已..
thanks guys for helping me. 谢谢你们帮助我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.