Spring實戰(zhàn)之ResourceLoaderAware加載資源用法示例
更新時間:2020年01月02日 09:07:49 作者:cakincqm
這篇文章主要介紹了Spring實戰(zhàn)之ResourceLoaderAware加載資源用法,結合實例形式分析了spring使用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;
// 實現ResourceLoaderAware接口必須實現的方法
// 如果把該Bean部署在Spring容器中,該方法將會由Spring容器負責調用
// Spring容器調用該方法時,Spring會將自身作為參數傳給該方法
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);
}
}
四 測試結果
true
更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數據結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
后端如何接收格式為x-www-form-urlencoded的數據
x-www-form-urlencoded格式是一種常見的HTTP請求數據格式,它將請求參數編碼為鍵值對的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關于后端如何接收格式為x-www-form-urlencoded的數據,需要的朋友可以參考下2023-05-05
Spring Boot分段處理List集合多線程批量插入數據的解決方案
大數據量的List集合,需要把List集合中的數據批量插入數據庫中,本文給大家介紹Spring Boot分段處理List集合多線程批量插入數據的解決方案,感興趣的朋友跟隨小編一起看看吧2024-04-04
Spring Boot Web應用開發(fā) CORS 跨域請求支持
本篇文章主要介紹了Spring Boot Web應用開發(fā) CORS 跨域請求支持,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

