用到的是DOM4J里面的SAXReader类,需要下载dom4j的jar包。
本文为了方便起见,特地在末尾上传了它的jar包。
此util类的功能是读取xml中的关于数据库配置的URL、用户名、密码。
util类如下:
package com.vastis.util; import java.io.File; import java.util.HashMap; import java.util.Iterator; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class ReadSpringXmlUtil { public void iterateWholeXML(String filename, HashMap<String, String> hm) throws Exception{ SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(new File(filename)); Element root = document.getRootElement(); //遍历根结点,即第一层 for (Iterator iter = root.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); // 获取id为dataSource的节点的bean if(element!=null){ Attribute ageAttr = element.attribute("id"); if (ageAttr != null && !ageAttr.equals("")) { String id = ageAttr.getValue(); if (id != null && id.equals("dataSource")) { // 遍历第二层(即driverClassName,url,username,password),并进行处理 for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) { Element elementInner = (Element) iterInner.next(); if(elementInner!=null && !elementInner.equals("")){ //获取name属性 Attribute dsAttr = elementInner.attribute("name"); if(dsAttr!=null && !dsAttr.equals("")) //下面开启遍历第3层,即4个property for (Iterator iterThird = elementInner.elementIterator(); iterThird .hasNext();) { Element elementThird = (Element) iterThird.next(); if(elementThird!=null && !elementThird.equals("")){ if(elementThird.getName().equals("value")){ hm.put(dsAttr.getValue(), elementThird.getText()); // System.out.println("name="+elementThird.getName()); // System.out.println("value="+elementThird.getText()); } } } } } } } } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new Exception(e); } } }
调用util类的函数如下:将此函数放在main函数中即可跑起来。
/** * 读取数据库配置,从spring.conf.xml中读取 * @throws Exception */ private void configDBInfo() throws Exception{ HashMap<String, String> hashMap= new HashMap<String, String>(); ReadSpringXmlUtil rsxu=new ReadSpringXmlUtil(); rsxu.iterateWholeXML(FILE_PATH,hashMap); try{ if(hashMap!=null && hashMap.size()>0){ for(java.util.Iterator iter=hashMap.keySet().iterator();iter.hasNext();){ Object key=iter.next(); String value=hashMap.get(key); if(((String)key).equals(Constants.URL)){ ReadOnlyConfiger.setUrl(value); }else if(((String)key).equals(Constants.USERNAME)){ ReadOnlyConfiger.setUserName(value); }else if(((String)key).equals(Constants.PASSWORD)){ ReadOnlyConfiger.setPassword(value); } } }else{ System.err.println("数据库配置信息未找到,请查看spring.conf.xml文件中的数据配置信息是否存在!"); } }catch(Exception ex){ System.out.println(ex.toString()); throw new Exception(ex); } }
xml源文件如下:
<?xml version="1.0" encoding="GB2312"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--JDBC数据源配置--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@11.5.56.199:1521/caqa</value> </property> <property name="username"> <value>system</value> </property> <property name="password"> <value>admin</value> </property> </bean> <!-- hibernate 配置文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="configLocation"> <value>/WEB-INF/classes/conf/hibernate.cfg.xml</value> </property> </bean> <!-- hibernate 事务工厂 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <!-- hibernate 事务代理 --> <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="Create*">PROPAGATION_REQUIRED</prop> <prop key="create*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="copy*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!--JDBC事务工厂--> <bean id="JDBCtransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <!--JDBC 事务代理--> <bean id="JDBCDAOProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="JDBCtransactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- base --> <!-- base:project --> <bean id="projectDAO" class="com.vastis.base.project.dao.impl.ProjectDAOHibernateImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="projectFacade" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.base.project.service.impl.ProjectFacadeImpl"> <property name="projectDAO"> <ref bean="projectDAO"/> </property> </bean> </property> </bean> <bean id="projectHelper" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.base.project.service.impl.ProjectHelperImpl"> <property name="projectDAO"> <ref bean="projectDAO"/> </property> </bean> </property> </bean> <!-- end base:project --> <!--base:param--> <bean id="paramDAO" class="com.vastis.base.param.dao.impl.ParamDAOHibernateImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="paramHelper" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.base.param.service.impl.ParamHelperImpl"> <property name="paramDAO"> <ref bean="paramDAO"/> </property> </bean> </property> </bean> <bean id="paramFacade" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.base.param.service.impl.ParamFacadeImpl"> <property name="paramDAO"> <ref bean="paramDAO"/> </property> </bean> </property> </bean> <!-- end base:param --> <!--ccms:qamonitor--> <bean id="qaMonitorDAOHibernate" class="com.vastis.ccms.qamonitor.dao.impl.QAMonitorDAOHibernateImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="QamonitorFacade" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.ccms.qamonitor.service.impl.QAMonitorFacadeImpl"> <property name="qaMonitorDAOHibernate"> <ref bean="qaMonitorDAOHibernate"/> </property> </bean> </property> </bean> <bean id="QamonitorHelper" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.ccms.qamonitor.service.impl.QAMonitorHelperImpl"> <property name="qamonitorDAOHibernate"> <ref bean="qaMonitorDAOHibernate"/> </property> </bean> </property> </bean> <!-- end ccms:qamonitor--> <!-- start ccms:staff --> <bean id="staffDAO" class="com.vastis.ccms.staff.dao.impl.StaffDAOHibernateImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="staffJDBCDAO" class="com.vastis.ccms.staff.dao.impl.StaffDAOJDBCImpl"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean> <bean id="staffFacade" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.ccms.staff.service.impl.StaffFacadeImpl"> <property name="staffDAOHibernate"> <ref bean="staffDAO"/> </property> <property name="staffDAOJDBC"> <ref bean="staffJDBCDAO"/> </property> </bean> </property> </bean> <bean id="staffHelper" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.ccms.staff.service.impl.StaffHelperImpl"> <property name="staffDAOHibernate"> <ref bean="staffDAO"/> </property> <property name="staffDAOJDBC"> <ref bean="staffJDBCDAO"/> </property> </bean> </property> </bean> <!-- end ccms:staff --> <bean id="MenuManagerFacade" class="com.vastis.menu.service.FacadeImpl"/> <bean id="MenuManagerHelper" class="com.vastis.menu.service.MenuManagerHelperImpl"/> <bean id="securityFacade" parent="txProxyTemplate"> <property name="target"> <bean class="com.vastis.security.service.impl.SecurityFacadeImpl"> </bean> </property> </bean> </beans>
refurl:http://hi.baidu.com/yuletianxia750/blog/item/920dae456c21fb8fb2b7dc90.html