Java Spring中@autowired注解的用途是什么。 术语autowired是什么意思

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

what is the use of @autowired annotation in Spring.What is meant by the term autowired

javaspring

提问by user1281029

I checked many tutorials but could not find a satisfactory answer.Does this annotation is performing Dependency injection of the properties.I have heard that this can be applied to a method,constructor or field but for what purpose.

我查了很多教程,但没有找到满意的答案。这个注解是否正在执行属性的依赖注入。我听说这可以应用于方法、构造函数或字段,但用于什么目的。

采纳答案by Ralph

The @Autowiredannotation is performing Dependency Injection.

@Autowired注解进行依赖注入。

If @Autowiredis applied to

如果@Autowired应用于

  • a field: then the dependency is stored in this field
  • a setter: then the setter is invoked, with the parameter that is determined by the same algorithm like for the field dependency injection
  • a constructor: then the constructor is invoked with the parameters determined by the same algorithm like for the field dependency injection  
  • 一个字段:然后依赖项存储在这个字段中
  • setter:然后调用 setter,参数由与字段依赖注入相同的算法确定
  • 构造函数:然后使用由与字段依赖注入相同的算法确定的参数调用构造函数  

回答by Shuhail Kadavath

We use @Autowired for :

我们将@Autowired 用于:

a. Less abstraction in the system .

一种。系统中的抽象较少。

b. When Spring finds an @Autowired annotation used with setter methods, it tries to perform byType autowiring on the method.

湾 当 Spring 发现与 setter 方法一起使用的 @Autowired 注解时,它会尝试对该方法执行 byType 自动装配。