最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot Autowried及Resouce使用對(duì)比解析

 更新時(shí)間:2020年06月10日 11:13:16   作者:KoMiles  
這篇文章主要介紹了Springboot Autowried及Resouce使用對(duì)比解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在做項(xiàng)目時(shí),發(fā)現(xiàn)項(xiàng)目中 加載類(lèi)時(shí),有的地方使用@Autowired,有的地方使用@Resource

在網(wǎng)上搜集了資料

共同點(diǎn)

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

不同點(diǎn)

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

  @Autowired是spring的注解,是spring2.5版本引入的,Autowired只根據(jù)type進(jìn)行注入,不會(huì)去匹配name。如果涉及到type無(wú)法辨別注入對(duì)象時(shí),那需要依賴(lài)@Qualifier或@Primary注解一起來(lái)修飾。

寫(xiě)列子

新建 HumanService.java類(lèi)

package com.komiles.study.service;

/**
 * @author komiles@163.com
 * @date 2020-03-23 11:46
 */
public interface HumanService {

  /**
   * 跑馬拉松
   * @return
   */
  String runMarathon();
}

實(shí)現(xiàn)類(lèi) 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 也能正常訪問(wèn)。

假如我寫(xiě)多個(gè)實(shí)現(xiàn)類(lèi)會(huì)怎么樣呢?

新建一個(gè) 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)報(bào)錯(cuò)了,因?yàn)橛袃蓚€(gè)實(shí)現(xiàn)類(lèi),程序不知道找那個(gè)了

怎么辦呢?

有兩種辦法

第一種,在實(shí)現(xiàn)類(lèi)中給類(lèi)起名字,在引入的時(shí)候直接引入名字。

例如:在ManServiceImpl.java類(lèi),@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類(lèi)中使用時(shí),也需要制定一下名字。

如果使用@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)先引用某一個(gè)類(lèi),可以在實(shí)現(xiàn)類(lèi)上使用 @Primary。

項(xiàng)目代碼:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringCloud Gateway鑒權(quán)和跨域解決方案

    SpringCloud Gateway鑒權(quán)和跨域解決方案

    網(wǎng)關(guān)是介于客戶(hù)端和服務(wù)器端之間的中間層,所有的外部請(qǐng)求都會(huì)先經(jīng)過(guò) 網(wǎng)關(guān)這一層,也就是說(shuō),API 的實(shí)現(xiàn)方面更多的考慮業(yè)務(wù)邏輯,而安全、性能、監(jiān)控可以交由 網(wǎng)關(guān)來(lái)做,這樣既提高業(yè)務(wù)靈活性又不缺安全性,本文給大家介紹SpringCloud Gateway鑒權(quán)和跨域解決方案,一起看看吧
    2023-11-11
  • Java并發(fā)工具類(lèi)Future使用示例

    Java并發(fā)工具類(lèi)Future使用示例

    這篇文章主要介紹了Java并發(fā)工具類(lèi)Future使用示例,本文需要注意future.get()方法是阻塞式的,如果調(diào)用該方法的時(shí)候任務(wù)尚未執(zhí)行完成,則會(huì)一直等待下去,直到任務(wù)執(zhí)行結(jié)束,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-06-06
  • Java本地緩存實(shí)現(xiàn)代碼示例

    Java本地緩存實(shí)現(xiàn)代碼示例

    這篇文章主要給大家介紹了關(guān)于Java本地緩存實(shí)現(xiàn)的相關(guān)資料,對(duì)于緩存的作用不言而喻,可以提高查詢(xún)效率,比去DB查詢(xún)的速度要快,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-08-08
  • Java連接SqlServer錯(cuò)誤的完美解決方法

    Java連接SqlServer錯(cuò)誤的完美解決方法

    我們?cè)谧鯦ava或者C#連接數(shù)據(jù)庫(kù)的時(shí)候,常常遇到連接SqlServer失敗的問(wèn)題,明明檢查了好幾遍代碼沒(méi)問(wèn)題了,還是連接不上,下面這篇文章主要給大家介紹了關(guān)于Java連接SqlServer錯(cuò)誤的完美解決方法,需要的朋友可以參考下
    2023-04-04
  • idea maven項(xiàng)目無(wú)法識(shí)別jar包里的class解決方案

    idea maven項(xiàng)目無(wú)法識(shí)別jar包里的class解決方案

    這篇文章主要介紹了idea maven項(xiàng)目無(wú)法識(shí)別jar包里的class解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java中的內(nèi)存溢出方式

    java中的內(nèi)存溢出方式

    文章介紹了如何使用jmap和IBMHeapAnalyzer等工具分析OutOfMemoryError: Compressedclassspace錯(cuò)誤,發(fā)現(xiàn)問(wèn)題出在/org/pf4j/PluginClassLoader加載了大量類(lèi)
    2024-12-12
  • mybatis查詢(xún)語(yǔ)句揭秘之封裝數(shù)據(jù)

    mybatis查詢(xún)語(yǔ)句揭秘之封裝數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于mybatis查詢(xún)語(yǔ)句揭秘之封裝數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java面試高頻問(wèn)題之RabbitMQ系列全面解析

    Java面試高頻問(wèn)題之RabbitMQ系列全面解析

    在介紹RabbitMQ之前實(shí)現(xiàn)要介紹一下MQ,MQ是什么?MQ全稱(chēng)是Message Queue,可以理解為消息隊(duì)列的意思,簡(jiǎn)單來(lái)說(shuō)就是消息以管道的方式進(jìn)行傳遞。RabbitMQ是一個(gè)實(shí)現(xiàn)了AMQP(Advanced Message Queuing Protocol)高級(jí)消息隊(duì)列協(xié)議的消息隊(duì)列服務(wù),用Erlang語(yǔ)言的
    2021-11-11
  • java多線(xiàn)程之線(xiàn)程安全的單例模式

    java多線(xiàn)程之線(xiàn)程安全的單例模式

    這篇文章主要為大家詳細(xì)介紹了java多線(xiàn)程之線(xiàn)程安全的單例模式,文章內(nèi)容全面,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 解決Aop @AfterReturning因返回類(lèi)型不一致導(dǎo)致無(wú)法執(zhí)行切面代碼

    解決Aop @AfterReturning因返回類(lèi)型不一致導(dǎo)致無(wú)法執(zhí)行切面代碼

    這篇文章主要介紹了解決Aop @AfterReturning因返回類(lèi)型不一致導(dǎo)致無(wú)法執(zhí)行切面代碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07

最新評(píng)論

六盘水市| 工布江达县| 股票| 南溪县| 南和县| 无锡市| 东阿县| 来凤县| 织金县| 治多县| 临潭县| 泾源县| 塔城市| 富川| 手游| 澄城县| 龙胜| 贵州省| 贺州市| 苍南县| 平乡县| 岐山县| 昌图县| 江安县| 拜泉县| 百色市| 栾城县| 沙洋县| 刚察县| 嘉鱼县| 清原| 肇庆市| 西贡区| 博罗县| 格尔木市| 邓州市| 石渠县| 荃湾区| 绥棱县| 天台县| 濮阳市|