欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring實(shí)戰(zhàn)之Qualifier注解用法示例

 更新時(shí)間:2019年12月27日 09:37:47   作者:cakincqm  
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Qualifier注解用法,結(jié)合實(shí)例形式詳細(xì)分析了spring Qualifier注解相關(guān)配置、定義與使用方法,需要的朋友可以參考下

本文實(shí)例講述了Spring實(shí)戰(zhàn)之Qualifier注解用法。分享給大家供大家參考,具體如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <context:component-scan base-package="org.crazyit.app.service"/>  
</beans>

二 接口

Axe

package org.crazyit.app.service;
public interface Axe
{
   public String chop();
}

Person

package org.crazyit.app.service;
public interface Person
{
   public void useAxe();
}

三 Bean

Chinese

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
@Component
public class Chinese implements Person
{
  @Autowired
  @Qualifier("steelAxe")
  private Axe axe;
//  // axe的setter方法
//  @Autowired
//  public void setAxe(@Qualifier("stoneAxe") Axe axe)
//  {
//    this.axe = axe;
//  }
  // 實(shí)現(xiàn)Person接口的useAxe()方法
  public void useAxe()
  {
    // 調(diào)用axe的chop()方法,
    // 表明Person對(duì)象依賴于axe對(duì)象
    System.out.println(axe.chop());
  }
}

SteelAxe

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class SteelAxe implements Axe
{
  public String chop()
  {
    return "鋼斧砍柴真快";
  }
}

StoneAxe

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class StoneAxe implements Axe
{
  public String chop()
  {
    return "石斧砍柴好慢";
  }
}

四 測(cè)試類

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest
{
  public static void main(String[] args)
  {
    // 創(chuàng)建Spring容器
    AbstractApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    // 注冊(cè)關(guān)閉鉤子
    ctx.registerShutdownHook();
    Person person = ctx.getBean("chinese" , Person.class);
    person.useAxe();
  }
}

五 測(cè)試結(jié)果

鋼斧砍柴真快

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • springboot項(xiàng)目獲取resources相對(duì)路徑的方法

    springboot項(xiàng)目獲取resources相對(duì)路徑的方法

    這篇文章主要介紹了springboot項(xiàng)目獲取resources相對(duì)路徑的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Spring基于注解讀取外部配置文件

    Spring基于注解讀取外部配置文件

    這篇文章主要介紹了Spring基于注解讀取外部配置文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java中equals和==的區(qū)別詳解

    Java中equals和==的區(qū)別詳解

    這篇文章主要介紹了詳解 Java 中 equals 和 == 的區(qū)別的相關(guān)資料,equals 和 == 都是用來(lái)檢測(cè)兩個(gè)字符串是否相等,返回值也都是布爾型,但是兩者在內(nèi)部比較的處理中卻不盡相同需要的朋友可以參考下
    2021-09-09
  • 10道springboot常見面試題

    10道springboot常見面試題

    這篇文章主要為大家詳細(xì)介紹了10道springboot常見面試題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Spring Bean基本管理實(shí)例詳解

    Spring Bean基本管理實(shí)例詳解

    這篇文章主要介紹了Spring Bean基本管理,以實(shí)例形式較為詳細(xì)的分析了Spring Bean的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn)

    Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn)

    這篇文章主要介紹了Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn),文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 深入淺出RocketMQ的事務(wù)消息

    深入淺出RocketMQ的事務(wù)消息

    RocketMQ事務(wù)消息(Transactional?Message)是指應(yīng)用本地事務(wù)和發(fā)送消息操作可以被定義到全局事務(wù)中,要么同時(shí)成功,要么同時(shí)失敗。本文主要介紹了RocketMQ事務(wù)消息的相關(guān)知識(shí),需要的可以參考一下
    2023-04-04
  • Java遞歸基礎(chǔ)與遞歸的宏觀語(yǔ)意實(shí)例分析

    Java遞歸基礎(chǔ)與遞歸的宏觀語(yǔ)意實(shí)例分析

    這篇文章主要介紹了Java遞歸基礎(chǔ)與遞歸的宏觀語(yǔ)意,結(jié)合實(shí)例形式分析了java遞歸的相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2020-03-03
  • mall整合SpringSecurity及JWT認(rèn)證授權(quán)實(shí)戰(zhàn)下

    mall整合SpringSecurity及JWT認(rèn)證授權(quán)實(shí)戰(zhàn)下

    這篇文章主要為大家介紹了mall整合SpringSecurity及JWT認(rèn)證授權(quán)實(shí)戰(zhàn)第二篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 使用jpa之動(dòng)態(tài)插入與修改(重寫save)

    使用jpa之動(dòng)態(tài)插入與修改(重寫save)

    這篇文章主要介紹了使用jpa之動(dòng)態(tài)插入與修改(重寫save),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評(píng)論