使用注释按名称自动装配 spring bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11831261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:17:33  来源:igfitidea点击:

Autowiring spring bean by name using annotation

springautowired

提问by Anand

In Springs latest version, we can autowire a bean using annotation as @Autowired. This will autowire the bean using its type(or constructor, if applied on it). Is there any way I can use the @Autowiredannotation based on the bean name which we were doing without annotation in Spring's XML file as autowire="byName"?

在 Springs 的最新版本中,我们可以使用注释自动装配 bean @Autowired。这将使用 bean 的类型(或构造函数,如果应用于它)自动装配 bean。有什么方法可以使用@Autowired基于 bean 名称的注释,我们在 Spring 的 XML 文件中没有注释作为autowire="byName"

采纳答案by soulcheck

You can use JSR-250 @Resourcefor by-name bean autowiring, unless you need constructor injection or multi-parameter method injection.

您可以使用 JSR-250@Resource进行按名称 bean 自动装配,除非您需要构造函数注入或多参数方法注入。

From the docs:

从文档:

If you intend to express annotation-driven injection by name, do not primarily use @Autowired, even if is technically capable of referring to a bean name through @Qualifier values. Instead, use the JSR-250 @Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

如果您打算按名称表达注解驱动的注入,请不要主要使用 @Autowired,即使技术上能够通过 @Qualifier 值引用 bean 名称。相反,请使用 JSR-250 @Resource 注释,该注释在语义上定义为通过其唯一名称标识特定目标组件,声明的类型与匹配过程无关。

回答by Biju Kunjummen

You can use:

您可以使用:

@Autowired
@Qualifier("beanname")

According to the @Qualifier javadoc

根据@Qualifier javadoc

This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring

自动装配时,此注释可用于字段或参数作为候选 bean 的限定符

回答by jeet

I was using bean name proxy which was messing up autowiring by name. @Resource didn't have that issue since it doesn't care about type. So now I know one reason for this recommendation by Spring developers :-) Just FYI

我正在使用 bean 名称代理,它通过名称搞乱了自动装配。@Resource 没有这个问题,因为它不关心类型。所以现在我知道 Spring 开发人员推荐的一个原因:-) 仅供参考

回答by tinku

If you want to define name of the bean with which they will be registered in DI container, you can pass the name in annotation itself e.g. @Service (“employeeManager”).

如果你想定义它们将在 DI 容器中注册的 bean 的名称,你可以在注释本身中传递名称,例如@Service (“employeeManager”)。

Then using below code you can enable autowire by Name

然后使用下面的代码,您可以按名称启用自动装配

@Autowired
@Qualifier("employeeManager")
private EmployeeManagerService employeeManagerService;

回答by Nandan

Use @Component("beanname") in the java class definition of your bean

在 bean 的 java 类定义中使用 @Component("beanname")

Then while autowiring use JSR 330

然后在自动装配时使用 JSR 330

@Inject @Named(Value="beanname")

@Inject @Named(Value="beanname")