加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml解析applicationContext.xml

发布时间:2020-12-15 22:33:41 所属栏目:百科 来源:网络整理
导读: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
<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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读