简体   繁体   English

如何在spring中通过默认的限定符bean名称获取@Service bean

[英]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:

  • With XML the qualifier is the id attribute of the bean element 使用XML,限定符是bean元素的id属性
  • With Javaconfig, the name of the method is the bean name by default. 使用Javaconfig,方法的名称默认为bean名称。 You can override that of course with the 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.

相关问题 Spring @Bean 名称,不适用于 @Qualifier - Spring @Bean Name, is not working with @Qualifier 如何定义具有限定符的特定 bean,该限定符将为其依赖项指定特定 bean? - How do I define a specific bean with a qualifier that would designate a specific bean for its dependancies? 使用@Qualifier 指定的 bean 注入点时,如何扩展命名的 Spring bean? - How do I extend a named Spring bean when using a @Qualifier specified bean injection point? 如何在Spring中的运行时设置bean限定符名称 - How to set a bean qualifier name at run time in Spring 在 @Service 构造函数中找不到 Bean 的限定符 - Qualifier for Bean dont found name in @Service constructor 如何在春季从@Autowired List &lt;&gt;的每个bean中获取@Qualifier - How to get @Qualifier from each bean of @Autowired List<> in Spring Spring 中使用 @Bean 和 @Qualifier 注释的 Bean 名称解析 - Bean name resolution in Spring using @Bean and @Qualifier annotations 有没有一种方便的方法来获取 Spring Bean/服务的名称? - Is there a handy way to get the name of a Spring Bean/Service? 如何以编程方式获取类和限定符的bean? - How to get bean with class and qualifier programmatically? spring bean 方法:如何获取方法名称? - spring bean method : how to get method name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM