Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法示例
本文實(shí)例講述了Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法。分享給大家供大家參考,具體如下:
一 配置文件
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="test" class="org.crazyit.app.service.TestBean"/> </beans>
二 Bean
package org.crazyit.app.service; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.ResourceLoader; public class TestBean implements ResourceLoaderAware { private ResourceLoader rd; // 實(shí)現(xiàn)ResourceLoaderAware接口必須實(shí)現(xiàn)的方法 // 如果把該Bean部署在Spring容器中,該方法將會(huì)由Spring容器負(fù)責(zé)調(diào)用 // Spring容器調(diào)用該方法時(shí),Spring會(huì)將自身作為參數(shù)傳給該方法 public void setResourceLoader(ResourceLoader resourceLoader) { this.rd = resourceLoader; } // 返回ResourceLoader對(duì)象的引用 public ResourceLoader getResourceLoader() { return rd; } }
三 測(cè)試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.springframework.core.io.*; import org.dom4j.io.XPPReader; import org.dom4j.Document; import org.dom4j.Element; import java.util.*; import org.crazyit.app.service.*; public class SpringTest { public static void main(String[] args) { // 創(chuàng)建ApplicationContext容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 獲取容器中名為test的Bean實(shí)例 TestBean tb = ctx.getBean("test" , TestBean.class); // 通過(guò)tb實(shí)例來(lái)獲取ResourceLoader對(duì)象 ResourceLoader rl = tb.getResourceLoader(); // 判斷程序獲得ResourceLoader和容器是否相同 System.out.println(rl == ctx); } }
四 測(cè)試結(jié)果
true
更多關(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)文章
后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
x-www-form-urlencoded格式是一種常見(jiàn)的HTTP請(qǐng)求數(shù)據(jù)格式,它將請(qǐng)求參數(shù)編碼為鍵值對(duì)的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案
大數(shù)據(jù)量的List集合,需要把List集合中的數(shù)據(jù)批量插入數(shù)據(jù)庫(kù)中,本文給大家介紹Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案,感興趣的朋友跟隨小編一起看看吧2024-04-04IDEA如何修改項(xiàng)目名稱出現(xiàn)中括號(hào)
當(dāng)項(xiàng)目文件夾名稱與model名稱不一致時(shí),IDEA會(huì)在項(xiàng)目名旁顯示中括號(hào)以區(qū)分,修改項(xiàng)目名稱時(shí)出現(xiàn)中括號(hào)問(wèn)題,通常是因?yàn)镮DE中model名與文件夾名不同步,解決方法是統(tǒng)一model名稱和文件夾名稱,可通過(guò)重構(gòu)功能或項(xiàng)目結(jié)構(gòu)設(shè)置完成,重命名操作應(yīng)謹(jǐn)慎,以避免路徑等引用錯(cuò)誤2024-10-10Java API如何實(shí)現(xiàn)向Hive批量導(dǎo)入數(shù)據(jù)
這篇文章主要介紹了Java API如何實(shí)現(xiàn)向Hive批量導(dǎo)入數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Spring Boot Web應(yīng)用開發(fā) CORS 跨域請(qǐng)求支持
本篇文章主要介紹了Spring Boot Web應(yīng)用開發(fā) CORS 跨域請(qǐng)求支持,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Java List轉(zhuǎn)換成String數(shù)組幾種實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Java List轉(zhuǎn)換成String數(shù)組幾種實(shí)現(xiàn)方式詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12Java中Caffeine本地緩存項(xiàng)目實(shí)例
這篇文章主要介紹了Java中Caffeine本地緩存項(xiàng)目實(shí)例,Caffeine是一個(gè)高性能Java 緩存庫(kù),使用Java8對(duì)Guava緩存重寫版本,在Spring Boot 2.0中將取代Guava,使用spring.cache.cache-names屬性可以在啟動(dòng)時(shí)創(chuàng)建緩存,需要的朋友可以參考下2023-10-10Java案例使用比較排序器comparator實(shí)現(xiàn)成績(jī)排序
這篇文章主要介紹了Java案例使用比較排序器comparator實(shí)現(xiàn)成績(jī)排序,主要通過(guò)案例用TreeSet集合存儲(chǔ)多個(gè)學(xué)生信息,并遍歷該集合,要按照總分從高到低進(jìn)行排序,下文介紹需要的朋友可以參考一下2022-04-04