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
. 我正在SpringSource上浏览一些博客,在其中一个博客中,作者使用的是@Inject
,我想他也可以使用@Autowired
。
Here is the piece of code: 这是一段代码:
@Inject private CustomerOrderService customerOrderService;
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? 我不确定@Inject
和@Autowired
之间的区别,如果有人解释了它们的区别,以及在什么情况下使用哪个,我将不胜感激。
#1楼
参考:https://stackoom.com/question/Ty7a/在Spring-Framework中-Inject和-Autowired有什么区别-在什么条件下使用哪一个
#2楼
As of Spring 3.0, Spring offers support for JSR-330 dependency injection annotations ( @Inject
, @Named
, @Singleton
). 从Spring 3.0开始,Spring提供了对JSR-330依赖项注入注释(@ @Inject
,@ @Named
,@ @Singleton
)的支持。
There is a separate section in the Spring documentation about them, including comparisons to their Spring equivalents. Spring文档中有一个关于它们的单独部分 ,包括与它们的Spring等效项的比较。
#3楼
@Inject
没有“必需”属性
#4楼
In addition to the above: 除了上述内容:
- The default scope for
@Autowired
beans is Singleton whereas using JSR 330@Inject
annotation it is like Spring's prototype .@Autowired
bean的默认作用域是Singleton,而使用JSR 330@Inject
注释则类似于Spring的prototype 。 - There is no equivalent of @Lazy in JSR 330 using
@Inject
. 在JSR 330中,没有使用@Inject
的@Lazy等效项。 - There is no equivalent of @Value in JSR 330 using
@Inject
. 使用@Inject
在JSR 330中没有等效的@Value。
#5楼
@Autowired
annotation is defined in the Spring framework. @Autowired
注释在Spring框架中定义。
@Inject
annotation is a standard annotation, which is defined in the standard "Dependency Injection for Java" (JSR-330) . @Inject
批注是一个标准批注,在标准“ Java依赖项注入”(JSR-330)中定义 。 Spring (since the version 3.0) supports the generalized model of dependency injection which is defined in the standard JSR-330. Spring(自版本3.0起)支持标准JSR-330中定义的依赖项注入的通用模型。 ( Google Guice frameworks and Picocontainer framework also support this model). ( Google Guice框架和Picocontainer框架也支持此模型)。
With @Inject
can be injected the reference to the implementation of the Provider
interface, which allows injecting the deferred references. 使用@Inject
可以注入对Provider
接口实现的引用,该接口允许注入延迟的引用。
Annotations @Inject
and @Autowired
- is almost complete analogies. 注释@Inject
和@ @Autowired
几乎是完整的类比。 As well as @Autowired
annotation, @Inject
annotation can be used for automatic binding properties, methods, and constructors. 与@Autowired
注释一样,@ @Inject
注释可用于自动绑定属性,方法和构造函数。
In contrast to @Autowired
annotation, @Inject
annotation has no required
attribute. 与@Autowired
注释相反,@ @Inject
注释没有required
属性。 Therefore, if the dependencies will not be found - will be thrown an exception. 因此,如果找不到依赖项,则将引发异常。
There are also differences in the clarifications of the binding properties. 结合特性的澄清也有所不同。 If there is ambiguity in the choice of components for the injection the @Named
qualifier should be added. 如果在注入的成分选择上存在歧义,则应添加@Named
限定词。 In a similar situation for @Autowired
annotation will be added @Qualifier
qualifier (JSR-330 defines it's own @Qualifier
annotation and via this qualifier annotation @Named
is defined). 在类似的情况下,将为@Autowired
注释添加@Qualifier
限定符(JSR-330定义了它自己的@Qualifier
注释,并通过此限定符注释定义了@Named
)。
#6楼
@Autowired
和@Inject
之间的主要区别(在阅读Spring Docs时注意到)是,@ @Autowired
具有'required'属性,而@Inject没有'required'属性。