Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法示例
本文實(shí)例講述了Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法。分享給大家供大家參考,具體如下:
一 代碼
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.springframework.core.io.Resource;
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
public class ResourceLoaderTest
{
public static void main(String[] args)
throws Exception
{
// 創(chuàng)建ApplicationContext實(shí)例
// ApplicationContext ctx = new
// ClassPathXmlApplicationContext("beans.xml");
ApplicationContext ctx = new
FileSystemXmlApplicationContext("beans.xml");
Resource res = ctx.getResource("book.xml");
// 獲取該資源的簡單信息
System.out.println(res.getFilename());
System.out.println(res.getDescription());
// 創(chuàng)建基于SAX的dom4j解析器
SAXReader reader = new SAXReader();
Document doc = reader.read(res.getFile());
// 獲取根元素
Element el = doc.getRootElement();
List l = el.elements();
// 遍歷根元素的全部子元素
for (Iterator it = l.iterator();it.hasNext() ; )
{
// 每個(gè)節(jié)點(diǎn)都是<書>節(jié)點(diǎn)
Element book = (Element)it.next();
List ll = book.elements();
// 遍歷<書>節(jié)點(diǎn)的全部子節(jié)點(diǎn)
for (Iterator it2 = ll.iterator();it2.hasNext() ; )
{
Element eee = (Element)it2.next();
System.out.println(eee.getText());
}
}
}
}
二 資源文件
<?xml version="1.0" encoding="GBK"?>
<計(jì)算機(jī)書籍列表>
<書>
<書名>瘋狂Java講義</書名>
<作者>李剛</作者>
</書>
<書>
<書名>瘋狂iOS講義</書名>
<作者>李剛</作者>
</書>
</計(jì)算機(jī)書籍列表>
三 配置文件
<?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"> </beans>
四 測(cè)試結(jié)果
book.xml
file [F:\Mybatis\spring\book.xml]
瘋狂Java講義
李剛
輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)
李剛
更多關(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)文章
基于CyclicBarrier和CountDownLatch的使用區(qū)別說明
這篇文章主要介紹了基于CyclicBarrier和CountDownLatch的使用區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java通過調(diào)用FFMPEG獲取視頻時(shí)長
這篇文章主要為大家詳細(xì)介紹了Java通過調(diào)用FFMPEG獲取視頻時(shí)長,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Spring Boot thymeleaf模板引擎的使用詳解
這篇文章主要介紹了Spring Boot thymeleaf模板引擎的使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java元素排序Comparable與Comparator的區(qū)別
這篇文章主要介紹了Java元素排序Comparable與Comparator的區(qū)別,二者都是頂級(jí)的接口,但擁有的方法和用法是不同的,下面我們分別來看看具體是怎樣的區(qū)別吧2022-05-05

