前端之家收集整理的这篇文章主要介绍了
xml解析applicationContext.xml,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<pre class="java" name="code"> package com.zkjw.core.util.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ReadXml {
private String driverClass;
private String jdbcUrl;
public String getDriverClass() {
return driverClass;
}
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
public String getJdbcUrl() {
return jdbcUrl;
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
public ReadXml(){
String fileName = this.getClass().getClassLoader().getResource("applicationContext.xml").getPath();
fileName = fileName.substring(1,fileName.length());
//file:/E:/apache-tomcat-6.0.14/webapps/jnetcms/WEB-INF/classes/applicationContext.xml
System.out.println(fileName);
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
InputStream is=new FileInputStream(fileName);
Document doc=dombuilder.parse(is);
Element root=doc.getDocumentElement();
NodeList dbinfo=root.getChildNodes();
if(dbinfo!=null){
Node db=dbinfo.item(5);
for(Node node=db.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("property")){
NamedNodeMap nodeAttr = node.getAttributes();
Node nodec = nodeAttr.getNamedItem("name");
Node nodeV = nodeAttr.getNamedItem("value");
String nodecValue = nodec.getFirstChild().getNodeValue();
String nodevValue = nodeV.getFirstChild().getNodeValue();
if("driverClass".equals(nodecValue)){
//oracle.jdbc.driver.OracleDriver
System.out.println(nodevValue.lastIndexOf("."));
String s = nodevValue.substring(nodevValue.lastIndexOf(".")+1,nodevValue.length());
if(s.contains("Driver")){
s = s.replace("Driver","");
}
setDriverClass(s);
}
if("jdbcUrl".equals(nodecValue)){
setJdbcUrl(nodevValue);
}
}
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="classpath:/com/zkjw/**/*.xml"/>
<!-- c3p0 -->
<bean id="c3p0DataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="user" value="jnetcms"></property>
<property name="password" value="tiger"></property>
<property name="maxPoolSize" value="50"></property>
<property name="minPoolSize" value="10"></property>
<property name="initialPoolSize" value="10"></property>
<property name="acquireIncrement" value="5"></property>
<property name="maxIdleTime" value="30"></property>
<property name="checkoutTimeout" value="30000"></property>
<property name="idleConnectionTestPeriod" value="30"></property>
<property name="maxStatements" value="3000"></property>
</bean>
<bean id = "transactionManager"
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name = "dataSource" ref="c3p0DataSource"/>
</bean>
<!-- 配置Jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0DataSource" />
</bean>
<!-- baseDao基类 -->
<bean name="baseDao" class="com.zkjw.core.dao.impl.BaseDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--利用了拦截器的原理。-->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="delete*">PROPAGATION_required</prop>
<prop key="update*">PROPAGATION_required</prop>
<prop key="save*">PROPAGATION_required</prop>
<prop key="find*">PROPAGATION_required,readOnly</prop>
<prop key="page*">PROPAGATION_required,readOnly</prop>
</props>
</property>
</bean>
</beans>
原文链接:https://www.f2er.com/xml/298276.html