java Java中@Autowired注解的好处

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

benefit of @Autowired annotation in Java

javaspringannotationsautowired

提问by cscsaba

Maybe, because of my wrong English, I couldn't understand the benefit of using @Autowired annotation.

也许,因为我的英语错误,我无法理解使用@Autowired 注释的好处。

According to the tutorial we can simplify the first(I.) case to second case(II.) by means of @Autowired.

根据教程,我们可以通过@Autowired 将第一种(I.)情况简化为第二种情况(II.)。

My question is, what is the meaning of the @Autowired ? Because it doesnt tell any more, since without using @Autowired the compiler can figure out that "EmpDao emDao" and "EmpManager" are closely related according the declaration.

我的问题是,@Autowired 的含义是什么?因为它不再说明,因为不使用@Autowired 编译器可以根据声明找出“EmpDao emDao”和“EmpManager”密切相关。

code cited from here

这里引用的代码

I.

一世。

    <bean id="empDao" class="EmpDao" />
    <bean id="empManager" class="EmpManager">
       <property name="empDao" ref="empDao" />
    </bean>

public class EmpManager {

   private EmpDao empDao;

   public EmpDao getEmpDao() {
      return empDao;
   }

   public void setEmpDao(EmpDao empDao) {
      this.empDao = empDao;
   }

   ...
}

II.

二、

<context:annotation-config />

<bean id="empManager" class="autowiredexample.EmpManager" />
<bean id="empDao"     class="autowiredexample.EmpDao" />

import org.springframework.beans.factory.annotation.Autowired;

public class EmpManager {

   @Autowired
   private EmpDao empDao;

}

回答by Bozho

@Autowiredis spring-specific. @Injectis the standard equivallent. It is an annotation that tells the context (spring, or in the case of @Inject- any DI framework) to try to set an object into that field.

@Autowired是弹簧专用的。@Inject是标准等价物。它是一个注解,告诉上下文(spring,或者在@Inject任何 DI 框架的情况下)尝试将对象设置到该字段中。

The compiler has nothing to do with this - it is the DI framework (spring) that instantiates your objects at runtime, and then sets their dependencies at the points you have specified - either via XML or via an annotation.

编译器与此无关 - 它是 DI 框架(spring)在运行时实例化您的对象,然后在您指定的点设置它们的依赖项 - 通过 XML 或通过注释。

I agree it is a possible scenario for a DI framework to try to inject dependencies into all fields, even if they are not annotated. (And if you want to exclude a particular field, to annotate it). But they chose the other strategy (configuration-over-convention). By the way:

我同意 DI 框架可能会尝试将依赖项注入所有字段,即使它们没有注释。(如果要排除特定字段,请对其进行注释)。但他们选择了另一种策略(configuration-over-convention)。顺便一提:

  • if using xml config and choose some form of autowiring, the dependencies of the bean will be automatically autowired without the need to specify anything
  • you can specify per-context autowiring settings.
  • 如果使用 xml config 并选择某种形式的自动装配,则 bean 的依赖项将自动装配,无需指定任何内容
  • 您可以指定每个上下文的自动装配设置。

回答by Vanchinathan Chandrasekaran

When the server bootstraps itself. It finds

当服务器引导自身时。它发现

 <context:annotation-config />

in the application context and then goes through the classes defined in the contexts. If there are any beans that are autowired, it injects that into the class by referring the context file.

在应用程序上下文中,然后通过上下文中定义的类。如果有任何自动装配的 bean,它会通过引用上下文文件将其注入到类中。

Basically, it promotes convention over configuration. That's what most frameworks do these days to reduce the development time.

基本上,它促进约定优于配置。这就是当今大多数框架为减少开发时间所做的事情。

回答by elduff

the @Autowired Spring annotation tells Spring to for a bean named 'empDao' and inject it into the EmpManager class, without you having to add the empDao bean as a property in your spring config file.

@Autowired Spring 注释告诉 Spring 使用名为“empDao”的 bean 并将其注入 EmpManager 类,而无需将 empDao bean 作为属性添加到 spring 配置文件中。

回答by James Kingsbery

@Autowired tells Spring to find a bean of the declared type and wire in that bean, rather than requiring an explicit lookup by bean name. It can, under certain circumstances, make configuring applications easier if you only have one implementation of your types in a given Spring context.

@Autowired 告诉 Spring 查找声明类型的 bean 并在该 bean 中进行连接,而不是要求按 bean 名称进行显式查找。在某些情况下,如果在给定的 Spring 上下文中只有一种类型的实现,它可以使配置应用程序更容易。