動(dòng)態(tài)創(chuàng)建和修改Spring的bean配置文件
今天本來打算寫Spring溫故知新系列的第二篇,不過突然想起一直都忘了學(xué)怎么用java來操作XML,這么重要的事情居然拖了這么久才想起來實(shí)在是太不應(yīng)該了,于是今天就先練習(xí)一下用dom4j來操作XML。
其實(shí)dom4j這個(gè)庫實(shí)在是太方便了,使用起來跟C#操作XML幾乎沒太大差別,也沒什么難度,所以就先貼兩段代碼吧。
其中有幾個(gè)要點(diǎn):
1、如果只是創(chuàng)建一個(gè)XML文件,那么只需要導(dǎo)入dom4j-1.6.1.jar就可以了,路徑如下:
spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar
如果是需要讀取或者修改,那么就需要導(dǎo)入這個(gè)庫內(nèi)的另外一個(gè)文件:
spring-framework-2.5.6\lib\dom4j\jaxen-1.1-beta-7.jar
否則就會(huì)報(bào)錯(cuò),報(bào)錯(cuò)內(nèi)容如下:
java.lang.NoClassDefFoundError: org/jaxen/JaxenException
...
...
...
2、dom4j是支持鏈?zhǔn)讲僮鞯?,這跟jQuery非常像。這樣一來創(chuàng)建一個(gè)XML文件就非常方便而且代碼結(jié)構(gòu)看起來也更加清晰明了。
3、要學(xué)會(huì)XPath.... 要不然你會(huì)很痛苦,不過XPath其實(shí)很簡單,應(yīng)該花不了多少時(shí)間,難不住各位的,哈哈~
Action部分:
Java代碼
- package com.iteye.bolide74.action;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.List;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.Node;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- public class MyDom4j {
- /**
- * 動(dòng)態(tài)創(chuàng)建一個(gè)bean配置文件,包含HelloWorld這個(gè)bean,并添加初始值
- * */
- public void createXML(String xmlPath, String msg) throws IOException {
- Document XmlDoc = DocumentHelper.createDocument();
- XmlDoc.addDocType("beans", "-//SPRING//DTD BEAN//EN",
- "http://www.springframework.org/dtd/spring-beans.dtd");
- //首先創(chuàng)建beans根節(jié)點(diǎn)
- Element beansEle = XmlDoc.addElement("beans");
- //注意:dom4j是支持類似于jQuery一樣的鏈?zhǔn)讲僮鞯?nbsp;
- Element beanHelloWorld = beansEle.addElement("bean")
- .addAttribute("id", "HelloWorld")
- .addAttribute("class", "com.iteye.bolide74.action.HelloWorld");
- Element propertyHelloWorld = beanHelloWorld.addElement("property")
- .addAttribute("name", "msg");
- Element valueHelloWorld = propertyHelloWorld.addElement("value")
- .addText(msg);
- XMLWriter outXml = new XMLWriter(new FileWriter(new File(xmlPath)));
- outXml.write(XmlDoc);
- outXml.close();
- }
- /**
- * 首先遍歷一個(gè)bean配置文件里的所有bean,獲取id和class的值, 然后修改HelloWorld這個(gè)bean的msg的值
- * @throws IOException
- * */
- public void editXML(String xmlPath, String msg) throws DocumentException, IOException {
- Document XmlDoc = new SAXReader().read(new File(xmlPath));
- List
xmlList = XmlDoc.selectNodes( "/beans/bean");- System.out.println("\r\n遍歷所有的bean獲得id和class:");
- for (Element element : xmlList) {
- System.out.println("id:" + element.attributeValue("id")
- + " / class:" + element.attributeValue("class"));
- }
- System.out.println("\r\n動(dòng)態(tài)修改HelloWorld這個(gè)bean的msg值:");
- //用XPath來獲取單一節(jié)點(diǎn)
- Node valueHelloWorld = XmlDoc
- .selectSingleNode("/beans/bean[@id='HelloWorld']/property[@name='msg']/value");
- System.out.println("原始值為:" + valueHelloWorld.getText());
- valueHelloWorld.setText(msg);
- System.out.println("修改后的值為:" + valueHelloWorld.getText());
- //修改完了以后記得保存,要不然你會(huì)納悶為什么XML文件沒變的,哈哈
- XMLWriter outXml = new XMLWriter(new FileWriter(new File(xmlPath)));
- outXml.write(XmlDoc);
- outXml.close();
- }
- }
Java代碼
- package com.iteye.bolide74.action;
- public class HelloWorld {
- public String msg;
- public String getMsg() {
- return msg;
- }
- public void setMsg(String msg) {
- this.msg = msg;
- }
- }
Tester實(shí)現(xiàn)類部分:
Java代碼
- package com.iteye.bolide74.tester;
- import java.io.IOException;
- import org.dom4j.DocumentException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import com.iteye.bolide74.action.HelloWorld;
- import com.iteye.bolide74.action.MyDom4j;
- public class HelloWorldTester {
- public static void main(String[] args) {
- String xmlPath = "/WebContent/WEB-INF/conf/config_dom4j.xml";
- MyDom4j myBeans = new MyDom4j();
- try {
- myBeans.createXML(System.getProperty("user.dir") + xmlPath,
- "Hello,world!this is created by dom4j!");
- } catch (IOException e) {
- e.printStackTrace();
- }
- ApplicationContext ac = new FileSystemXmlApplicationContext(xmlPath);
- HelloWorld helloWorld = (HelloWorld) ac.getBean("HelloWorld");
- System.out.println(helloWorld.getMsg());
- try {
- myBeans.editXML(System.getProperty("user.dir") + xmlPath,
- "Hello,world!this is edited by dom4j!");
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 重新獲取bean配置文件
- ac = new FileSystemXmlApplicationContext(xmlPath);
- helloWorld = (HelloWorld) ac.getBean("HelloWorld");
- System.out.println("\r\n" + helloWorld.getMsg());
- }
- }
輸出結(jié)果為:
Html代碼
- Hello,world!this is created by dom4j!
- 遍歷所有的bean獲得id和class:
- id:HelloWorld / class:com.iteye.bolide74.action.HelloWorld
- 動(dòng)態(tài)修改HelloWorld這個(gè)bean的msg值:
- 原始值為:Hello,world!this is created by dom4j!
- 修改后的值為:Hello,world!this is edited by dom4j!
- Hello,world!this is edited by dom4j!


2009-06-17 14:10:30
2011-01-19 14:00:21




