亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Springboot Autowried及Resouce使用對比解析

瀏覽:3日期:2023-05-13 08:26:27

在做項目時,發現項目中 加載類時,有的地方使用@Autowired,有的地方使用@Resource

在網上搜集了資料

共同點

@Resource和@Autowired都可以作為注入屬性的修飾,在接口僅有單一實現類時,兩個注解的修飾效果相同,可以互相替換,不影響使用。

不同點

@Resource是Java自己的注解,@Resource有兩個屬性是比較重要的,分是name和type;Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動注入策略。

@Autowired是spring的注解,是spring2.5版本引入的,Autowired只根據type進行注入,不會去匹配name。如果涉及到type無法辨別注入對象時,那需要依賴@Qualifier或@Primary注解一起來修飾。

寫列子

新建 HumanService.java類

package com.komiles.study.service;/** * @author [email protected] * @date 2020-03-23 11:46 */public interface HumanService { /** * 跑馬拉松 * @return */ String runMarathon();}

實現類 ManServiceImpl.java

package com.komiles.study.service.impl;import com.komiles.study.service.HumanService;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;/** * @author [email protected] * @date 2020-03-23 11:48 */@Servicepublic class ManServiceImpl implements HumanService { /** * 跑馬拉松 */ @Override public String runMarathon() { return ' A man run marathon'; }}

新建HumanController.java

package com.komiles.study.controller;import com.komiles.study.service.HumanService;import com.komiles.study.service.impl.ManServiceImpl;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author [email protected] * @date 2020-03-23 11:49 */@RestController@RequestMapping('/human')public class HumanController { @Autowired private HumanService humanService; @GetMapping('/run') public String runMarathon() { return humanService.runMarathon(); }}

運行程序

輸出內容為: man run marathon

把controller里的 @Autowired 改成@Resource 也能正常訪問。

假如我寫多個實現類會怎么樣呢?

新建一個 WomanServiceImpl.java

package com.komiles.study.service.impl;import com.komiles.study.service.HumanService;import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;/** * @author [email protected] * @date 2020-03-23 12:01 */@Servicepublic class WomanServiceImpl implements HumanService { /** * 跑馬拉松 */ @Override public String runMarathon() { return 'A Woman run marathon'; }}

運行程序,發現報錯了,因為有兩個實現類,程序不知道找那個了

怎么辦呢?

有兩種辦法

第一種,在實現類中給類起名字,在引入的時候直接引入名字。

例如:在ManServiceImpl.java類,@Service上加值。@Service(value = 'manService') 或者 @Component(value = 'manService')

package com.komiles.study.service.impl;import com.komiles.study.service.HumanService;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;/** * @author [email protected] * @date 2020-03-23 11:48 */@Service(value = 'manService')//@Component(value = 'manService')public class ManServiceImpl implements HumanService { /** * 跑馬拉松 */ @Override public String runMarathon() { return ' A man run marathon'; }}

在Controller類中使用時,也需要制定一下名字。

如果使用@Resource 需要加上 @Resource(name='manService')

如果使用@Autowired 需要使用@Qualifier(value='manService')

package com.komiles.study.controller;import com.komiles.study.service.HumanService;import com.komiles.study.service.impl.ManServiceImpl;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author [email protected] * @date 2020-03-23 11:49 */@RestController@RequestMapping('/human')public class HumanController { @Autowired @Qualifier(value = 'manService')// @Resource(name='manService') private HumanService humanService; @GetMapping('/run') public String runMarathon() { return humanService.runMarathon(); }}

如果想優先引用某一個類,可以在實現類上使用 @Primary。

項目代碼:

https://github.com/KoMiles/springboot/blob/master/src/main/java/com/komiles/study/controller/HumanController.java

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 91精品国产乱码久久久久久 | 欧美一区二区激情三区 | 黄色小视频在线免费观看 | 国产激情视频在线观看首页 | 亚洲精品日韩一区二区日本 | 国产挤奶水在线视频播放 | 再猛点深使劲爽日本免费视频 | 国产高清亚洲精品26u | 草草在线观看 | 在线免费你懂的 | 一区在线免费 | 网红思瑞一区二区三区 | 黄色aaa级片| 日本中文字幕乱码免费 | 国产色产综合色产在线观看视频 | 日本一级特黄aa毛片免费观看 | 91短视频版在线观看免费 | 亚洲视频不卡 | 51成人免费影院 | 国产成人一区二区三区在线播放 | 有没有毛片网站 | 玉米视频污 | 91视频久久 | 特级淫片日本高清视频 | 一级毛片免费视频日本 | 免费在线看黄网址 | 亚洲精品国产专区一区 | 999yy成年在线视频免费看 | 在线成h人视频网站免费观看 | 欧美亚洲综合另类在线观看 | 奇米影视久久777中文字幕 | 视频播放在线观看精品视频 | 优女视频免费观看 | 国产一级视频播放 | 美女白丝超短裙被输出动态图 | 99热成人精品国产免男男 | 免费jizz在线播放视频高清版 | 欧美日韩三 | 精品无人区一区二区三区 | 色综合久久88色综合天天 | 国产激情一区二区三区成人91 |