Java Spring MVC - @Autowired 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21276136/
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-08-13 07:49:47  来源:igfitidea点击:

Spring MVC - How does @Autowired work?

javaspringspring-mvcdependency-injectionautowired

提问by COBOL

I am working on a web application using Java and Spring. I'm new at Spring, so as an opportunity to learn, I was basically given an existing application and was told to extend it. I am having trouble understanding how the @Autowired work. I understand the high-level concept of dependency injection, but I want to know how the @Autowired annotation knows which concrete implementation of an interface to inject?

我正在使用 Java 和 Spring 开发 Web 应用程序。我是 Spring 的新手,所以作为学习的机会,我基本上得到了一个现有的应用程序,并被告知要扩展它。我无法理解@Autowired 的工作原理。我理解依赖注入的高级概念,但我想知道@Autowired 注释如何知道要注入的接口的具体实现?

To put my question into context, I will explain the problem I am having:

为了将我的问题放在上下文中,我将解释我遇到的问题:

I have an interfacecalled PostDao, and a classcalled PostDaoImplwhich implements PostDao. I then have another classcalled PostDaoPublicImplwhich extends PostDaoImpl. These classes exist in my persistence layer.

I then have an interfacecalled PostService, and a classcalled called PostServiceImplwhich implement PostService. I then have another classcalled PostServicePublicImplwhich extends PostServiceImpl. These classes exist in my services layer.

我有一个名为PostDao接口和一个名为PostDaoImpl,它实现了PostDao。然后我有另一个名为PostDaoPublicImpl 的,它扩展了PostDaoImpl。这些类存在于我的持久层中。

然后我有一个名为PostService接口和一个名为PostServiceImpl,它实现了PostService。然后我有另一个名为PostServicePublicImpl 的,它扩展了PostServiceImpl。这些类存在于我的服务层中。

In PostServiceImplthe following line injects the following object:

PostServiceImpl 中,以下行注入以下对象:

@Autowired private PostDao postDao; 
//This injects an object of the class PostDaoImpl

My problem is, in PostServicePublicImplhow do I have the same declaration as above, but have it inject an object of the class PostDaoPublicImpl:

我的问题是,在PostServicePublicImpl 中,我如何具有与上述相同的声明,但让它注入类PostDaoPublicImpl的对象:

@Autowired private PostDao postDao; 
//This injects an object of the class PostDaoPublicImpl

I feel as though if I understand how the @Autowired annotation works, then I will be able to solve this problem. Any help and suggestions to solve the problem would be greatly appreciated.

我觉得如果我了解 @Autowired 注释的工作原理,那么我就能够解决这个问题。任何解决问题的帮助和建议将不胜感激。

回答by sanbhat

First of all you need to understand how Autowiring works

首先你需要了解自动装配的工作原理

then in the bean definition, define autowire strategy- let it be byNamefor our case

然后在 bean 定义中,定义自动装配策略-对于我们的情况,让它成为byName

<bean id="postDaoPublicImpl" class="com.yourpackage.PostDaoPublicImpl" autowire="byName" />

that means, if any class has a property with name "postDaoPublicImpl", which is the bean id of above bean, will be automatically injected with the bean instance

这意味着,如果任何类具有名为“postDaoPublicImpl”的属性,即上述 bean 的 bean id,将自动注入 bean 实例

after that in your class PostServicePublicImpldefine property like this

之后在你的班级中PostServicePublicImpl定义这样的属性

class PostServicePublicImpl  {

@Autowired private PostDaoPublicImpl postDaoPublicImpl; 
//here Spring will auto inject PostDaoPublicImpl implementation, since this property name matches the bean id and (auto wiring strategy is "byName")

}

回答by COBOL

I found an easier solution to my problem. Using the @Qualifier tag I can give a unique name to each implementation of the interface. Then when auto-wiring the object, I just provide a qualifier and the object of the implementation matching the qualifier is injected.

我找到了一个更简单的方法来解决我的问题。使用@Qualifier 标签,我可以为接口的每个实现指定一个唯一的名称。然后在自动连接对象时,我只提供一个限定符,并注入与该限定符匹配的实现对象。

回答by David Ruan

Add this to your applicationContext

将此添加到您的 applicationContext

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>