溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何動(dòng)態(tài)創(chuàng)建和修改Spring的bean配置文件

發(fā)布時(shí)間:2021-10-27 09:51:12 來(lái)源:億速云 閱讀:232 作者:柒染 欄目:編程語(yǔ)言

這篇文章給大家介紹如何動(dòng)態(tài)創(chuàng)建和修改Spring的bean配置文件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

今天本來(lái)打算寫Spring溫故知新系列的第二篇,不過(guò)突然想起一直都忘了學(xué)怎么用java來(lái)操作XML,這么重要的事情居然拖了這么久才想起來(lái)實(shí)在是太不應(yīng)該了,于是今天就先練習(xí)一下用dom4j來(lái)操作XML。

其實(shí)dom4j這個(gè)庫(kù)實(shí)在是太方便了,使用起來(lái)跟C#操作XML幾乎沒(méi)太大差別,也沒(méi)什么難度,所以就先貼兩段代碼吧。

其中有幾個(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è)庫(kù)內(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非常像。這樣一來(lái)創(chuàng)建一個(gè)XML文件就非常方便而且代碼結(jié)構(gòu)看起來(lái)也更加清晰明了。

3、要學(xué)會(huì)XPath.... 要不然你會(huì)很痛苦,不過(guò)XPath其實(shí)很簡(jiǎn)單,應(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來(lái)獲取單一節(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文件沒(méi)變的,哈哈             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!

關(guān)于如何動(dòng)態(tài)創(chuàng)建和修改Spring的bean配置文件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI