在官方文檔中同時也看到這麼一段話:java
Letting qualifier values select against target bean names, within the type-matching candidates, does not require a @Qualifier annotation at the injection point. If there is no other resolution indicator (such as a qualifier or a primary marker), for a non-unique dependency situation, Spring matches the injection point name (that is, the field name or parameter name) against the target bean names and choose the same-named candidate, if any.
意思就是說,若是使用@Autowired進行注入,若是沒有使用@Qualifier以及@Primary等註解明顯標誌須要注入的依賴,那麼Spring就會根據名稱進行匹配。express
一開始看到這句話的時候有點懷疑,由於和個人認知是有點出入。印象中使用@Autowired的時候,假如存在多個實現類,若是不經過@Qualifier 指定,那麼會注入失敗。但剛剛通過試驗,官方文檔說的是對的。那麼目前這個時候@Autowired和@Resource的區別就比較少了。ide
但二者之間的出發點仍是不同。@Resource是經過名字注入,類型注入是fallback。而@Autowired是經過類型注入,名字注入是fallback。這個體現下面的聲明:ui
@Resource Object userService
能夠注入userService bean id/name爲userService的實現類,而@Autowired不行。code
官方的這段話:component
That said, if you intend to express annotation-driven injection by name, do not primarily use @Autowired, even if it is capable of selecting by bean name among type-matching candidates. Instead, use the JSR-250 @Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process. @Autowired has rather different semantics: After selecting candidate beans by type, the specified String qualifier value is considered within those type-selected candidates only (for example, matching an account qualifier against beans marked with the same qualifier label).
也說明若是但願經過惟一的beanid注入,建議使用@Resource進行注入。雖然@Autowired可以實現差很少的功能,可是@Autowired仍是機遇type-match的基礎上進行過濾的。ci
下面繼續羅列一下@Resource和@Autowired的區別:
一、@Autowired能夠做用在construct,field,setter方法(能夠有多個參數,而且參數上可使用@Qualifies進行標註),而@Resource只可使用在field,setter方法上(只能是單個單數的setter方法)文檔
二、對於self reference上的處理方式不同。(目前尚未搞明白)get