Spring實(shí)戰(zhàn)之使用Resource作為屬性操作示例
本文實(shí)例講述了Spring實(shí)戰(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"?> <計(jì)算機(jī)書籍列表> <書> <書名>瘋狂Java講義</書名> <作者>李剛</作者> </書> <書> <書名>輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)</書名> <作者>李剛</作者> </書> </計(jì)算機(jī)書籍列表>
三 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 { // 獲取該資源的簡(jiǎn)單信息 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()); } } } }
四 測(cè)試類
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(); } }
五 測(cè)試結(jié)果
book.xml
class path resource [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ì)有所幫助。
- Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法示例
- Spring實(shí)戰(zhàn)之ResourceLoader接口資源加載用法示例
- Spring實(shí)戰(zhàn)之ServletContextResource訪問資源文件示例
- Spring實(shí)戰(zhàn)之FileSystemResource加載資源文件示例
- Spring實(shí)戰(zhàn)之使用ClassPathResource加載xml資源示例
- Spring實(shí)戰(zhàn)之使用@Resource配置依賴操作示例
- Spring 中 @Service 和 @Resource 注解的區(qū)別
- Spring框架中 @Autowired 和 @Resource 注解的區(qū)別
- 詳解Spring注解--@Autowired、@Resource和@Service
- Springboot項(xiàng)目打war包docker包找不到resource下靜態(tài)資源的解決方案
- 詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器
相關(guān)文章
淺談Java中幾種常見的比較器的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄獪\談Java中幾種常見的比較器的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10java中InputStream獲取字節(jié)大小相關(guān)方法詳解
這篇文章主要給大家介紹了關(guān)于java中InputStream獲取字節(jié)大小相關(guān)方法的相關(guān)資料,在Java中要實(shí)現(xiàn)讀取文件大小,可以使用InputStream來讀取文件的內(nèi)容,并通過獲取讀取的字節(jié)數(shù)來得到文件的大小,需要的朋友可以參考下2023-11-11@PathVariable 如何自動(dòng)填充入實(shí)例對(duì)象中
這篇文章主要介紹了@PathVariable 實(shí)現(xiàn)自動(dòng)填充入實(shí)例對(duì)象中的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Android中比較常見的Java super關(guān)鍵字
這篇文章主要為大家介紹了Android中比較常見的Java super關(guān)鍵字,具有一定的學(xué)習(xí)參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01利用Spring Boot創(chuàng)建docker image的完整步驟
這篇文章主要給大家介紹了關(guān)于如何利用Spring Boot創(chuàng)建docker image的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Idea 同一窗口導(dǎo)入多個(gè)項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了Idea 同一窗口導(dǎo)入多個(gè)項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07