Spring實(shí)戰(zhàn)之使用@Resource配置依賴(lài)操作示例
本文實(shí)例講述了Spring使用@Resource配置依賴(lài)操作。分享給大家供大家參考,具體如下:
一 配置
<?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"> <!-- 自動(dòng)掃描指定包及其子包下的所有Bean類(lèi) --> <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 javax.annotation.*; import org.crazyit.app.service.*; @Component public class Chinese implements Person { private Axe axe; // axe的setter方法 @Resource(name="stoneAxe") public void setAxe(Axe axe) { this.axe = axe; } // 實(shí)現(xiàn)Person接口的useAxe()方法 public void useAxe() { // 調(diào)用axe的chop()方法, // 表明Person對(duì)象依賴(lài)于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è)試類(lèi)
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容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person person = ctx.getBean("chinese" , Person.class); person.useAxe(); } }
五 測(cè)試結(jié)果
石斧砍柴好慢
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法示例
- Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法示例
- Spring實(shí)戰(zhàn)之ServletContextResource訪(fǎng)問(wèn)資源文件示例
- Spring實(shí)戰(zhàn)之FileSystemResource加載資源文件示例
- Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源示例
- Spring 中 @Service 和 @Resource 注解的區(qū)別
- Spring框架中 @Autowired 和 @Resource 注解的區(qū)別
- 詳解Spring注解--@Autowired、@Resource和@Service
- Springboot項(xiàng)目打war包docker包找不到resource下靜態(tài)資源的解決方案
- 詳解SpringBoot開(kāi)發(fā)使用@ImportResource注解影響攔截器
- Spring實(shí)戰(zhàn)之使用Resource作為屬性操作示例
相關(guān)文章
IDEA整合SSM框架實(shí)現(xiàn)網(wǎng)頁(yè)上顯示數(shù)據(jù)
最近做了個(gè)小項(xiàng)目,該項(xiàng)目包在intellij idea中實(shí)現(xiàn)了ssm框架的整合以及實(shí)現(xiàn)訪(fǎng)問(wèn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05springboot基于過(guò)濾器實(shí)現(xiàn)接口請(qǐng)求耗時(shí)統(tǒng)計(jì)操作
這篇文章主要介紹了springboot基于過(guò)濾器實(shí)現(xiàn)接口請(qǐng)求耗時(shí)統(tǒng)計(jì)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09如何利用Stream改變list中特定對(duì)象的某一屬性
這篇文章主要介紹了如何利用Stream改變list中特定對(duì)象的某一屬性問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12java用兩個(gè)例子充分闡述多態(tài)的可拓展性介紹
下面小編就為大家?guī)?lái)一篇java用兩個(gè)例子充分闡述多態(tài)的可拓展性介紹。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06詳解SpringCloud使用Consul做注冊(cè)中心
這篇文章主要介紹了SpringCloud使用Consul做注冊(cè)中心,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼
在實(shí)際開(kāi)發(fā)中,肯定會(huì)經(jīng)常遇到對(duì)參數(shù)字段進(jìn)行校驗(yàn)的場(chǎng)景,通常我們只能寫(xiě)大量的if else來(lái)完成校驗(yàn)工作,而如果使用SpringBoot Validation則可以輕松的通過(guò)注解來(lái)完成,接下來(lái)小編給大家介紹下利用SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼,需要的朋友參考下吧2022-06-06Java 迪杰斯特拉算法實(shí)現(xiàn)查找最短距離的實(shí)現(xiàn)
這篇文章主要介紹了Java 迪杰斯特拉算法實(shí)現(xiàn)查找最短距離的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09SpringBoot3集成SLF4J+logback進(jìn)行日志記錄的實(shí)現(xiàn)
本文主要介紹了SpringBoot3集成SLF4J+logback進(jìn)行日志記錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Java用freemarker導(dǎo)出word實(shí)用示例
本篇文章主要介紹了Java用freemarker導(dǎo)出word實(shí)用示例,使用freemarker的模板來(lái)實(shí)現(xiàn)功能,有需要的可以了解一下。2016-11-11Java Iterator迭代器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
迭代器是一種模式,它可以使得對(duì)于序列類(lèi)型的數(shù)據(jù)結(jié)構(gòu)的遍歷行為與被遍歷的對(duì)象分離,接下來(lái)通過(guò)本文給大家分享Java Iterator迭代器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友參考下吧2017-05-05