天天看點

在Spring Framework中@Inject和@Autowired有什麼差別? 在什麼條件下使用哪一個?

本文翻譯自:What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

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:

除了上述内容:
  1. 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 。
  2. There is no equivalent of @Lazy in JSR 330 using

    @Inject

    . 在JSR 330中,沒有使用

    @Inject

    的@Lazy等效項。
  3. 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'屬性。