在Java中@Autowired注解的好处 [英] benefit of @Autowired annotation in Java

查看:475
本文介绍了在Java中@Autowired注解的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许,因为我的英语错了,我不明白使用@Autowired注解的利益。

据我们可以简化第(一)案件第二种情况教程(二)通过@Autowired的手段。

我的问题是,什么是@Autowired意思?因为它没有告诉任何更多,因为没有使用@Autowired编译器可以弄清楚,EmpDao emDao和EmpManager密切根据声明有关。

code从<被引href=\"http://www.developer.com/article_comments.php/3756831/2/Java-Tip-Simplify-Spring-Apps-with-Autowired.htm\">here

我。

 &LT;豆的id =empDao级=EmpDao/&GT;
    &LT;豆的id =empManager级=EmpManager&GT;
       &LT;属性名=empDaoREF =empDao/&GT;
    &LT; /豆&GT;公共类EmpManager {   私人EmpDao empDao;   公共EmpDao getEmpDao(){
      返回empDao;
   }   公共无效setEmpDao(EmpDao empDao){
      this.empDao = empDao;
   }   ...
}

二,

 &LT;背景:注解配置/&GT;&LT;豆的id =empManager级=autowiredexample.EmpManager/&GT;
&LT;豆的id =empDao级=autowiredexample.EmpDao/&GT;进口org.springframework.beans.factory.annotation.Autowired;公共类EmpManager {   @Autowired
   私人EmpDao empDao;}


解决方案

@Autowired 是特定的春天。 @Inject 是标准equivallent。这是一个注解,告诉背景(春,或 @Inject 的情况 - 所有的DI框架)来尝试设置对象到该字段

编译器有没有关系这一点 - 它是DI框架(春季)的实例化对象的在运行时的,然后设置它们的依赖在您指定的点 - 无论是通过XML或通过注释。

我同意它是一个的DI框架,试图注入依赖到各个领域可能的情况下,即使他们没有注释。 (如果要排除某一特定领域,进行注解)。但他们选择了其他战略(配置环比约定)。顺便说一句:


  • 如果使用XML配置,选择某种形式的自动装配,bean的依赖将被自动无需自动连接到指定任何

  • 您可以指定每个上下文自动装配的设置。

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;

}

解决方案

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

这篇关于在Java中@Autowired注解的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆