通過Spring工具類獲取classpath下的文件資源,解析xml


File cfgFile = ResourceUtils.getFile("classpath:test.txt");

或者

org.springframework.core.io.Resource fileRource = new ClassPathResource("test.xml");

獲取文件:fileRource.getFile();

獲取文件流:fileRource.getInputStream();

 

獲取到xml:進行解析:例:<myXml> <template id="index" type="page" name="首頁" templatePath="test.ftl" staticPath="ii.html"/></myXml>

Document document = new SAXReader().read(fileRource.getFile());

//獲取到myxml下面的template節點為id為test的節點
Element element = (Element) document.selectSingleNode("/myXml/template[@id='test']");

//解析xml,並獲取到節點里的值。

String id = element.attributeValue("id");

 

//獲取到java的臨時文件:

System.getProperty("java.io.tmpdir"),

//windows是\,unix是/

File.separator

 

一、簡單的用ApplicationContext做測試的話,獲得Spring中定義的Bean實例(對象).可以用:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO",RegisterDao.class);

 

在主文件中引入副文件:

<import resource="classpath*:env/*/applicationContext.xml" />

在主文件中可以引入一些properties文件,作為applicationContext中的變量

<context:property-placeholder location="classpath*:env/default/ecp.properties,
classpath*:env/default/common.properties,
classpath*:ecp.properties"
ignore-resource-not-found="true" ignore-unresolvable="true" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM