Spring Framework 中@Inject 和@Autowired 的区别是什么?在什么条件下使用哪一个? [英] What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?
问题描述
我正在浏览一些关于 SpringSource 的博客,在其中一个博客中,作者使用的是 @Inject
,我想他也可以使用 @Autowired
.
I am going through some blogs on SpringSource and in one of the blogs, author is using @Inject
and I suppose he can also use @Autowired
.
这是一段代码:
@Inject private CustomerOrderService customerOrderService;
我不确定 @Inject
和 @Autowired
之间的区别,如果有人解释它们的区别以及在什么情况下使用哪一个,我将不胜感激?>
I am not sure about the difference between @Inject
and @Autowired
and would appreciate it if someone explained their difference and which one to use under what situation?
推荐答案
假设这里指的是 javax.inject.Inject
注释.@Inject
是 Java CDI (Contexts和依赖注入)标准在 Java EE 6 (JSR-299) 中引入,阅读更多.Spring 选择支持使用 @Inject
注释作为他们自己的 @Autowired
注释的同义词.
Assuming here you're referring to the javax.inject.Inject
annotation. @Inject
is part of the Java CDI (Contexts and Dependency Injection) standard introduced in Java EE 6 (JSR-299), read more. Spring has chosen to support using the @Inject
annotation synonymously with their own @Autowired
annotation.
所以,回答你的问题,@Autowired
是 Spring 自己的注解.@Inject
是称为 CDI 的 Java 技术的一部分,该技术定义了类似于 Spring 的依赖注入标准.在 Spring 应用程序中,这两个注解的工作方式与 Spring 决定在它们自己的之外支持一些 JSR-299 注解的方式相同.
So, to answer your question, @Autowired
is Spring's own annotation. @Inject
is part of a Java technology called CDI that defines a standard for dependency injection similar to Spring. In a Spring application, the two annotations works the same way as Spring has decided to support some JSR-299 annotations in addition to their own.
这篇关于Spring Framework 中@Inject 和@Autowired 的区别是什么?在什么条件下使用哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!