亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • Spring Security+JWT實(shí)現(xiàn)認(rèn)證與授權(quán)的實(shí)現(xiàn)

    Spring Security+JWT實(shí)現(xiàn)認(rèn)證與授權(quán)的實(shí)現(xiàn)

    本文主要介紹了Spring Security+JWT實(shí)現(xiàn)認(rèn)證與授權(quán)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • SpringBoot全局異常處理方式

    SpringBoot全局異常處理方式

    這篇文章主要介紹了SpringBoot全局異常處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中抽象類(lèi)的作用及說(shuō)明

    Java中抽象類(lèi)的作用及說(shuō)明

    這篇文章主要介紹了Java中抽象類(lèi)的作用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java獲取e.printStackTrace()打印的信息方式

    Java獲取e.printStackTrace()打印的信息方式

    這篇文章主要介紹了Java獲取e.printStackTrace()打印的信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Arthas在線java進(jìn)程診斷工具在線調(diào)試神器詳解

    Arthas在線java進(jìn)程診斷工具在線調(diào)試神器詳解

    Arthas是 Alibaba 開(kāi)源的Java診斷工具,深受開(kāi)發(fā)者喜愛(ài)。這篇文章主要介紹了Arthas在線java進(jìn)程診斷工具 在線調(diào)試神器,需要的朋友可以參考下
    2021-11-11
  • Spring實(shí)戰(zhàn)之讓Bean獲取Spring容器操作示例

    Spring實(shí)戰(zhàn)之讓Bean獲取Spring容器操作示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之讓Bean獲取Spring容器操作,結(jié)合實(shí)例形式分析了Bean獲取Spring容器的相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-11-11
  • 解決springcloud Zuul丟失Cookie的問(wèn)題

    解決springcloud Zuul丟失Cookie的問(wèn)題

    這篇文章主要介紹了解決springcloud Zuul丟失Cookie的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 用Set類(lèi)判斷Map里key是否存在的示例代碼

    用Set類(lèi)判斷Map里key是否存在的示例代碼

    本篇文章主要是對(duì)用Set類(lèi)判斷Map里key是否存在的示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-12-12
  • 解決spring jpa中update的坑

    解決spring jpa中update的坑

    這篇文章主要介紹了spring jpa中update遇到的坑及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 淺談Java并發(fā)編程基礎(chǔ)知識(shí)

    淺談Java并發(fā)編程基礎(chǔ)知識(shí)

    這篇文章主要介紹了淺談Java并發(fā)編程基礎(chǔ)知識(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評(píng)論