Java中@Autowired注解的好处

发布于 2024-10-17 22:07:42 字数 1106 浏览 10 评论 0原文

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

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

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

代码引用自 此处

.developer.com /

    <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;
   }

   ...
}

article_comments.php/3756831/2/Java-Tip- Simplify

<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;

}

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

According to the tutorial we can simplify the first(I.) case to second case(II.) by means of @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.

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放血 2024-10-24 22:07:42

@Autowired 是 spring 特定的。 @Inject 是标准的等效项。它是一个注释,告诉上下文(spring,或者在 @Inject 的情况下 - 任何 DI 框架)尝试将对象设置到该字段中。

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

我同意 DI 框架尝试将依赖项注入所有字段是一种可能的情况,即使它们没有注释。 (如果您想排除某个特定字段,请对其进行注释)。但他们选择了另一种策略(配置优于约定)。顺便说一句:

  • 如果使用 xml 配置并选择某种形式的自动装配,bean 的依赖项将自动自动装配,而无需指定任何内容,
  • 您可以指定每个上下文的自动装配设置。

@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.

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.

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:

  • 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.
很酷又爱笑 2024-10-24 22:07:42

当服务器自行引导时。它在应用程序上下文中查找

 <context:annotation-config />

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

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

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.

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

丢了幸福的猪 2024-10-24 22:07:42

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

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.

街角迷惘 2024-10-24 22:07:42

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

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文