xml解析applicationContext.xml

前端之家收集整理的这篇文章主要介绍了xml解析applicationContext.xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <pre class="java" name="code"> package com.zkjw.core.util.xml;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7.  
  8. import javax.xml.parsers.DocumentBuilder;
  9. import javax.xml.parsers.DocumentBuilderFactory;
  10. import javax.xml.parsers.ParserConfigurationException;
  11.  
  12. import org.w3c.dom.Document;
  13. import org.w3c.dom.Element;
  14. import org.w3c.dom.NamedNodeMap;
  15. import org.w3c.dom.Node;
  16. import org.w3c.dom.NodeList;
  17. import org.xml.sax.SAXException;
  18.  
  19.  
  20. public class ReadXml {
  21. private String driverClass;
  22. private String jdbcUrl;
  23. public String getDriverClass() {
  24. return driverClass;
  25. }
  26. public void setDriverClass(String driverClass) {
  27. this.driverClass = driverClass;
  28. }
  29. public String getJdbcUrl() {
  30. return jdbcUrl;
  31. }
  32. public void setJdbcUrl(String jdbcUrl) {
  33. this.jdbcUrl = jdbcUrl;
  34. }
  35.  
  36.  
  37.  
  38. public ReadXml(){
  39. String fileName = this.getClass().getClassLoader().getResource("applicationContext.xml").getPath();
  40. fileName = fileName.substring(1,fileName.length());
  41. //file:/E:/apache-tomcat-6.0.14/webapps/jnetcms/WEB-INF/classes/applicationContext.xml
  42. System.out.println(fileName);
  43. DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
  44. try {
  45. DocumentBuilder dombuilder=domfac.newDocumentBuilder();
  46. InputStream is=new FileInputStream(fileName);
  47. Document doc=dombuilder.parse(is);
  48. Element root=doc.getDocumentElement();
  49. NodeList dbinfo=root.getChildNodes();
  50. if(dbinfo!=null){
  51. Node db=dbinfo.item(5);
  52. for(Node node=db.getFirstChild();node!=null;node=node.getNextSibling()){
  53. if(node.getNodeType()==Node.ELEMENT_NODE){
  54. if(node.getNodeName().equals("property")){
  55. NamedNodeMap nodeAttr = node.getAttributes();
  56. Node nodec = nodeAttr.getNamedItem("name");
  57. Node nodeV = nodeAttr.getNamedItem("value");
  58. String nodecValue = nodec.getFirstChild().getNodeValue();
  59. String nodevValue = nodeV.getFirstChild().getNodeValue();
  60. if("driverClass".equals(nodecValue)){
  61. //oracle.jdbc.driver.OracleDriver
  62. System.out.println(nodevValue.lastIndexOf("."));
  63. String s = nodevValue.substring(nodevValue.lastIndexOf(".")+1,nodevValue.length());
  64. if(s.contains("Driver")){
  65. s = s.replace("Driver","");
  66. }
  67. setDriverClass(s);
  68. }
  69. if("jdbcUrl".equals(nodecValue)){
  70. setJdbcUrl(nodevValue);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. } catch (ParserConfigurationException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. } catch (FileNotFoundException e) {
  80. // TODO Auto-generated catch block
  81. e.printStackTrace();
  82. } catch (SAXException e) {
  83. // TODO Auto-generated catch block
  84. e.printStackTrace();
  85. } catch (IOException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. }
  90. }

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  12.  
  13. <import resource="classpath:/com/zkjw/**/*.xml"/>
  14. <!-- c3p0 -->
  15. <bean id="c3p0DataSource"
  16. class="com.mchange.v2.c3p0.ComboPooledDataSource"
  17. destroy-method="close">
  18. <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
  19. <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
  20. <property name="user" value="jnetcms"></property>
  21. <property name="password" value="tiger"></property>
  22. <property name="maxPoolSize" value="50"></property>
  23. <property name="minPoolSize" value="10"></property>
  24. <property name="initialPoolSize" value="10"></property>
  25. <property name="acquireIncrement" value="5"></property>
  26. <property name="maxIdleTime" value="30"></property>
  27. <property name="checkoutTimeout" value="30000"></property>
  28. <property name="idleConnectionTestPeriod" value="30"></property>
  29. <property name="maxStatements" value="3000"></property>
  30. </bean>
  31. <bean id = "transactionManager"
  32. class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
  33. <property name = "dataSource" ref="c3p0DataSource"/>
  34. </bean>
  35. <!-- 配置Jdbc模板 -->
  36. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  37. <property name="dataSource" ref="c3p0DataSource" />
  38. </bean>
  39. <!-- baseDao基类 -->
  40. <bean name="baseDao" class="com.zkjw.core.dao.impl.BaseDao">
  41. <property name="jdbcTemplate" ref="jdbcTemplate"></property>
  42. </bean>
  43.  
  44. <!--利用了拦截器的原理。-->
  45. <bean id="transactionInterceptor"
  46. class="org.springframework.transaction.interceptor.TransactionInterceptor">
  47. <property name="transactionManager">
  48. <ref bean="transactionManager" />
  49. </property>
  50. <!-- 配置事务属性 -->
  51. <property name="transactionAttributes">
  52. <props>
  53. <prop key="delete*">PROPAGATION_required</prop>
  54. <prop key="update*">PROPAGATION_required</prop>
  55. <prop key="save*">PROPAGATION_required</prop>
  56. <prop key="find*">PROPAGATION_required,readOnly</prop>
  57. <prop key="page*">PROPAGATION_required,readOnly</prop>
  58. </props>
  59. </property>
  60. </bean>
  61. </beans>

猜你在找的XML相关文章