Spring實戰(zhàn)之使用Resource作為屬性操作示例
本文實例講述了Spring實戰(zhàn)之使用Resource作為屬性操作。分享給大家供大家參考,具體如下:
一 配置文件
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"
p:res="classpath:book.xml"/>
</beans>
二 屬性文件
<?xml version="1.0" encoding="GBK"?>
<計算機書籍列表>
<書>
<書名>瘋狂Java講義</書名>
<作者>李剛</作者>
</書>
<書>
<書名>輕量級Java EE企業(yè)應(yīng)用實戰(zhàn)</書名>
<作者>李剛</作者>
</書>
</計算機書籍列表>
三 Bean
package org.crazyit.app.service;
import org.springframework.core.io.Resource;
import org.dom4j.io.*;
import org.dom4j.*;
import java.util.*;
public class TestBean
{
private Resource res;
// res的setter方法
public void setRes(Resource res)
{
this.res = res;
}
public void parse()throws Exception
{
// 獲取該資源的簡單信息
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() ; )
{
// 每個節(jié)點都是<書>節(jié)點
Element book = (Element)it.next();
List ll = book.elements();
// 遍歷<書>節(jié)點的全部子節(jié)點
for (Iterator it2 = ll.iterator();it2.hasNext() ; )
{
Element eee = (Element)it2.next();
System.out.println(eee.getText());
}
}
}
}
四 測試類
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.springframework.core.io.*;
import org.crazyit.app.service.*;
public class SpringTest
{
public static void main(String[] args) throws Exception
{
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
TestBean tb = ctx.getBean("test" , TestBean.class);
tb.parse();
}
}
五 測試結(jié)果
book.xml
class path resource [book.xml]
瘋狂Java講義
李剛
輕量級Java EE企業(yè)應(yīng)用實戰(zhàn)
李剛
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- Spring實戰(zhàn)之ResourceLoaderAware加載資源用法示例
- Spring實戰(zhàn)之ResourceLoader接口資源加載用法示例
- Spring實戰(zhàn)之ServletContextResource訪問資源文件示例
- Spring實戰(zhàn)之FileSystemResource加載資源文件示例
- Spring實戰(zhàn)之使用ClassPathResource加載xml資源示例
- Spring實戰(zhàn)之使用@Resource配置依賴操作示例
- Spring 中 @Service 和 @Resource 注解的區(qū)別
- Spring框架中 @Autowired 和 @Resource 注解的區(qū)別
- 詳解Spring注解--@Autowired、@Resource和@Service
- Springboot項目打war包docker包找不到resource下靜態(tài)資源的解決方案
- 詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器
相關(guān)文章
java中InputStream獲取字節(jié)大小相關(guān)方法詳解
這篇文章主要給大家介紹了關(guān)于java中InputStream獲取字節(jié)大小相關(guān)方法的相關(guān)資料,在Java中要實現(xiàn)讀取文件大小,可以使用InputStream來讀取文件的內(nèi)容,并通過獲取讀取的字節(jié)數(shù)來得到文件的大小,需要的朋友可以參考下2023-11-11
Android中比較常見的Java super關(guān)鍵字
這篇文章主要為大家介紹了Android中比較常見的Java super關(guān)鍵字,具有一定的學(xué)習(xí)參考價值,感興趣的小伙伴們可以參考一下2016-01-01
利用Spring Boot創(chuàng)建docker image的完整步驟
這篇文章主要給大家介紹了關(guān)于如何利用Spring Boot創(chuàng)建docker image的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Idea 同一窗口導(dǎo)入多個項目的實現(xiàn)步驟
本文主要介紹了Idea 同一窗口導(dǎo)入多個項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

