Spring實(shí)戰(zhàn)之Qualifier注解用法示例
本文實(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)文章
簡(jiǎn)單談?wù)凷pringMVC轉(zhuǎn)發(fā)和重定向的區(qū)別
下面小編就為大家?guī)硪黄?jiǎn)單談?wù)凷pringMVC轉(zhuǎn)發(fā)和重定向的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
RabbitMQ死信機(jī)制實(shí)現(xiàn)延遲隊(duì)列的實(shí)戰(zhàn)
本文主要介紹了RabbitMQ死信機(jī)制實(shí)現(xiàn)延遲隊(duì)列的實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Java 中的CharArrayReader 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
CharArrayReader 是字符數(shù)組輸入流。它和ByteArrayInputStream類似,只不過ByteArrayInputStream是字節(jié)數(shù)組輸入流,而CharArray是字符數(shù)組輸入流。CharArrayReader 是用于讀取字符數(shù)組,它繼承于Reader2017-05-05
java實(shí)現(xiàn)投票程序設(shè)計(jì)
這篇文章主要介紹了java實(shí)現(xiàn)投票程序設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12
配置java環(huán)境變量(linux mac windows7)
本文給大家詳細(xì)總結(jié)介紹了Linux、MAC以及Windows下配置java環(huán)境變量的方法,非常的細(xì)致全面,有需要的小伙伴可以參考下2015-11-11
Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實(shí)現(xiàn)
在二叉樹的結(jié)點(diǎn)上加上線索的二叉樹稱為線索二叉樹,對(duì)二叉樹以某種遍歷方式進(jìn)行遍歷,使其變?yōu)榫€索二叉樹的過程稱為對(duì)二叉樹進(jìn)行線索化。本文將詳解如何實(shí)現(xiàn)線索化二叉樹,需要的可以參考一下2022-05-05

