小編這次要給大家分享的是淺談Springboot中Autowried及Resouce,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
成都網(wǎng)站制作、網(wǎng)站設(shè)計的關(guān)注點(diǎn)不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)建站一個展示的機(jī)會來證明自己,這并不會花費(fèi)您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗,一切以用戶為中心。
在做項目時,發(fā)現(xiàn)項目中 加載類時,有的地方使用@Autowired,有的地方使用@Resource
在網(wǎng)上搜集了資料
共同點(diǎn)
@Resource和@Autowired都可以作為注入屬性的修飾,在接口僅有單一實現(xiàn)類時,兩個注解的修飾效果相同,可以互相替換,不影響使用。
不同點(diǎn)
@Resource是Java自己的注解,@Resource有兩個屬性是比較重要的,分是name和type;Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機(jī)制使用byName自動注入策略。
@Autowired是spring的注解,是spring2.5版本引入的,Autowired只根據(jù)type進(jìn)行注入,不會去匹配name。如果涉及到type無法辨別注入對象時,那需要依賴@Qualifier或@Primary注解一起來修飾。
寫列子
新建 HumanService.java類
package com.komiles.study.service; /** * @author komiles@163.com * @date 2020-03-23 11:46 */ public interface HumanService { /** * 跑馬拉松 * @return */ String runMarathon(); }
實現(xiàn)類 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 komiles@163.com * @date 2020-03-23 11:48 */ @Service public 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 komiles@163.com * @date 2020-03-23 11:49 */ @RestController @RequestMapping("/human") public class HumanController { @Autowired private HumanService humanService; @GetMapping("/run") public String runMarathon() { return humanService.runMarathon(); } }
運(yùn)行程序
輸出內(nèi)容為: man run marathon
把controller里的 @Autowired 改成@Resource 也能正常訪問。
假如我寫多個實現(xiàn)類會怎么樣呢?
新建一個 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 komiles@163.com * @date 2020-03-23 12:01 */ @Service public class WomanServiceImpl implements HumanService { /** * 跑馬拉松 */ @Override public String runMarathon() { return "A Woman run marathon"; } }
運(yùn)行程序,發(fā)現(xiàn)報錯了,因為有兩個實現(xiàn)類,程序不知道找那個了
怎么辦呢?
有兩種辦法
第一種,在實現(xiàn)類中給類起名字,在引入的時候直接引入名字。
例如:在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 komiles@163.com * @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 komiles@163.com * @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(); } }
如果想優(yōu)先引用某一個類,可以在實現(xiàn)類上使用 @Primary。
看完這篇關(guān)于淺談Springboot中Autowried及Resouce的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
文章名稱:淺談Springboot中Autowried及Resouce
當(dāng)前地址:http://chinadenli.net/article0/ppgjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作、ChatGPT、企業(yè)建站、網(wǎng)站制作、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)