Java中@Autowired注解的好处
也许,由于我的英语错误,我无法理解使用@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@Autowired
是 spring 特定的。@Inject
是标准的等效项。它是一个注释,告诉上下文(spring,或者在@Inject
的情况下 - 任何 DI 框架)尝试将对象设置到该字段中。编译器与此无关 - DI 框架 (spring) 在运行时实例化您的对象,然后在您指定的点设置它们的依赖项 - 通过 XML 或通过注释。
我同意 DI 框架尝试将依赖项注入所有字段是一种可能的情况,即使它们没有注释。 (如果您想排除某个特定字段,请对其进行注释)。但他们选择了另一种策略(配置优于约定)。顺便说一句:
@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:
当服务器自行引导时。它在应用程序上下文中查找
,然后遍历上下文中定义的类。如果有任何自动装配的 bean,它会通过引用上下文文件将其注入到类中。
基本上,它提倡约定优于配置。这就是当今大多数框架为减少开发时间所做的事情。
When the server bootstraps itself. It finds
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.
@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.
@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.