Spring實戰(zhàn)之ResourceLoaderAware加載資源用法示例
本文實例講述了Spring實戰(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; // 實現(xiàn)ResourceLoaderAware接口必須實現(xiàn)的方法 // 如果把該Bean部署在Spring容器中,該方法將會由Spring容器負責調(diào)用 // Spring容器調(diào)用該方法時,Spring會將自身作為參數(shù)傳給該方法 public void setResourceLoader(ResourceLoader resourceLoader) { this.rd = resourceLoader; } // 返回ResourceLoader對象的引用 public ResourceLoader getResourceLoader() { return rd; } }
三 測試類
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實例 TestBean tb = ctx.getBean("test" , TestBean.class); // 通過tb實例來獲取ResourceLoader對象 ResourceLoader rl = tb.getResourceLoader(); // 判斷程序獲得ResourceLoader和容器是否相同 System.out.println(rl == ctx); } }
四 測試結(jié)果
true
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
x-www-form-urlencoded格式是一種常見的HTTP請求數(shù)據(jù)格式,它將請求參數(shù)編碼為鍵值對的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案
大數(shù)據(jù)量的List集合,需要把List集合中的數(shù)據(jù)批量插入數(shù)據(jù)庫中,本文給大家介紹Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案,感興趣的朋友跟隨小編一起看看吧2024-04-04Java API如何實現(xiàn)向Hive批量導入數(shù)據(jù)
這篇文章主要介紹了Java API如何實現(xiàn)向Hive批量導入數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Spring Boot Web應用開發(fā) CORS 跨域請求支持
本篇文章主要介紹了Spring Boot Web應用開發(fā) CORS 跨域請求支持,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Java List轉(zhuǎn)換成String數(shù)組幾種實現(xiàn)方式詳解
這篇文章主要介紹了Java List轉(zhuǎn)換成String數(shù)組幾種實現(xiàn)方式詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12Java案例使用比較排序器comparator實現(xiàn)成績排序
這篇文章主要介紹了Java案例使用比較排序器comparator實現(xiàn)成績排序,主要通過案例用TreeSet集合存儲多個學生信息,并遍歷該集合,要按照總分從高到低進行排序,下文介紹需要的朋友可以參考一下2022-04-04