Spring实战之ResourceLoader接口资源加载用法示例
作者:cakincqm 时间:2023-11-20 12:16:49
本文实例讲述了Spring实战之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
{
// 创建ApplicationContext实例
// 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());
// 创建基于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() ; )
{
// 每个节点都是<书>节点
Element book = (Element)it.next();
List ll = book.elements();
// 遍历<书>节点的全部子节点
for (Iterator it2 = ll.iterator();it2.hasNext() ; )
{
Element eee = (Element)it2.next();
System.out.println(eee.getText());
}
}
}
}
二 资源文件
<?xml version="1.0" encoding="GBK"?>
<计算机书籍列表>
<书>
<书名>疯狂Java讲义</书名>
<作者>李刚</作者>
</书>
<书>
<书名>疯狂iOS讲义</书名>
<作者>李刚</作者>
</书>
</计算机书籍列表>
三 配置文件
<?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>
四 测试结果
book.xml
file [F:\Mybatis\spring\book.xml]
疯狂Java讲义
李刚
轻量级Java EE企业应用实战
李刚
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101223355
标签:Spring,ResourceLoader,接口资源加载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot整合MyBatis操作过程
2022-08-02 13:50:22
关于Android中WebView远程代码执行漏洞浅析
2023-02-21 13:36:21
![](https://img.aspxhome.com/file/2023/7/126667_0s.jpg)
Android 详解Studio引用Library与导入jar
2022-05-23 17:53:14
![](https://img.aspxhome.com/file/2023/3/137233_0s.png)
C# this关键字的四种用法
2022-10-06 07:09:22
![](https://img.aspxhome.com/file/2023/5/75595_0s.jpg)
java Apache poi 对word doc文件进行读写操作
2023-09-23 02:13:15
![](https://img.aspxhome.com/file/2023/7/62987_0s.png)
Java中七种排序算法总结分析
2023-03-27 19:41:44
![](https://img.aspxhome.com/file/2023/6/67616_0s.png)
C#使用GZipStream解压缩数据文件的方法
2022-09-22 15:40:34
浅谈Android面向切面编程(AOP)
2022-04-03 15:14:41
![](https://img.aspxhome.com/file/2023/3/137513_0s.png)
Java实现根据模板自动生成新的PPT
2022-10-22 18:31:31
![](https://img.aspxhome.com/file/2023/7/65797_0s.jpg)
Android使用Xutils3进行断点下载的实例
2021-08-13 21:59:27
利用Jetpack Compose实现绘制五角星效果
2023-04-10 06:20:48
![](https://img.aspxhome.com/file/2023/7/137847_0s.png)
SpringCloud使用Zookeeper作为配置中心的示例
2023-08-03 12:30:51
![](https://img.aspxhome.com/file/2023/6/132176_0s.jpg)
Spring集成Quartz的简单配置的方法
2023-08-24 02:52:27
spring boot配置多个请求服务代理的完整步骤
2023-03-30 10:30:49
![](https://img.aspxhome.com/file/2023/0/110890_0s.png)
unity使用射线实现贴花系统
2023-04-23 10:22:00
![](https://img.aspxhome.com/file/2023/4/108694_0s.jpg)
基于servlet实现统计网页访问次数
2021-11-05 08:35:13
![](https://img.aspxhome.com/file/2023/7/82047_0s.jpg)
SpringBoot在Controller层接收参数的n种姿势(超详细)
2023-01-28 00:54:39
![](https://img.aspxhome.com/file/2023/0/60600_0s.jpg)
C语言中各种操作符的详细介绍(纯干货!)
2022-07-22 08:34:40
![](https://img.aspxhome.com/file/2023/1/129541_0s.png)
Java算法设计与分析分治算法
2022-04-02 08:07:15
![](https://img.aspxhome.com/file/2023/9/92549_0s.png)
java web项目里ehcache.xml介绍
2022-02-25 20:46:25