@Autowired注释在Java中的好处

时间:2022-03-01 04:57:23

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将第一种情况简化为第二种情况。

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;

}

4 个解决方案

#1


11  

@Autowired is spring-specific. @Inject is 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 spring特定。@Inject is the standard equivallent。它是一个注释,告诉上下文(spring,或@Inject - any 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框架尝试将依赖项插入到所有字段中是一种可能的情况,即使它们没有注释。(如果你想排除一个特定的字段,可以注释它)。但他们选择了另一种策略(配置优先于约定)。顺便说一下:

  • 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
  • 如果使用xml config并选择某种形式的自动连接,那么bean的依赖项将自动自动连接,不需要指定任何内容
  • you can specify per-context autowiring settings.
  • 您可以指定每个上下文自动连接设置。

#2


1  

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.

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

#3


1  

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类中,而不需要在Spring配置文件中添加empDao bean作为属性。

#4


0  

@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上下文中只有一个类型的实现,则可以使配置应用程序更容易。

#1


11  

@Autowired is spring-specific. @Inject is 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 spring特定。@Inject is the standard equivallent。它是一个注释,告诉上下文(spring,或@Inject - any 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框架尝试将依赖项插入到所有字段中是一种可能的情况,即使它们没有注释。(如果你想排除一个特定的字段,可以注释它)。但他们选择了另一种策略(配置优先于约定)。顺便说一下:

  • 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
  • 如果使用xml config并选择某种形式的自动连接,那么bean的依赖项将自动自动连接,不需要指定任何内容
  • you can specify per-context autowiring settings.
  • 您可以指定每个上下文自动连接设置。

#2


1  

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.

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

#3


1  

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类中,而不需要在Spring配置文件中添加empDao bean作为属性。

#4


0  

@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上下文中只有一个类型的实现,则可以使配置应用程序更容易。