Java中dom4j怎么读取配置文件实现抽象工厂+反射

本篇内容主要讲解“Java中dom4j怎么读取配置文件实现抽象工厂+反射”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java中dom4j怎么读取配置文件实现抽象工厂+反射”吧!

在Java中也可以同Donet一样,将差异配置在配置文件里面。另外,我们采用下面的方式实现,将会更加便捷。

逻辑描述:

现在我们想在B层和D层加上接口层,并使用工厂。而我们可以将创建B和创建D看作是两个系列,然后就可以使用抽象工厂进行创建了。

配置文件:beans-config.xml。service-class与dao-class分别对应两个系列的产品。子菜单中id对应接口的命名空间,class对应实现类的命名空间。

[html] view plaincopyprint?      <?xml version="1.0" encoding="UTF-8"?>       <beans>            <service-class>               <service id="com.xxjstgb.drp.basedata.manager.ItemManager" class="com.xxjstgb.drp.basedata.manager.ItemManagerImpl" />               <service id="com.xxjstgb.drp.flowcard.manager.FlowCardManager" class="com.xxjstgb.drp.flowcard.manager.impl.FlowCardManagerImpl" />            </service-class>            <dao-class>               <dao id="com.xxjstgb.drp.basedata.dao.ItemDao" class="com.xxjstgb.drp.basedata.dao.ItemDao4OracleImpl" />               <dao id="com.xxjstgb.drp.flowcard.dao.FlowCardDao" class="com.xxjstgb.drp.flowcard.dao.impl.FlowCardDaoImpl" />            </dao-class>       </beans>

抽象工厂:BeanFactory。通过读取配置文件,取得相关对象,并将相关创建好的对象保存在Map中。

[java] view plaincopyprint?      package com.xxjstgb.drp.util;              import java.util.HashMap;       import java.util.Map;              //dom4j       import org.dom4j.Document;       import org.dom4j.DocumentException;       import org.dom4j.Element;       import org.dom4j.io.SAXReader;              import com.xxjstgb.drp.basedata.dao.ItemDao;       import com.xxjstgb.drp.basedata.manager.ItemManager;       import com.xxjstgb.drp.flowcard.dao.FlowCardDao;       import com.xxjstgb.drp.flowcard.manager.FlowCardManager;              /**       * 抽象工厂,主要创建两个系列的产品       * 1、Manager系列       * 2、Dao系列产品       * @author liuzhengquan       *       */       public class BeanFactory {                      private static BeanFactory instance=new BeanFactory();                      //系统缺省配置文件名称           private final String beansConfigFile="beans-config.xml";                      //保存Dao相关对象           private Document doc;                      /*           * key=配置文件中的id值           * value=对应了该Id的对象           */           private Map serviceMap = new HashMap();//保存Service相关对象           private Map daoMap = new HashMap();//保存Dao相关对象                                 private BeanFactory(){               try {                   doc=new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream(beansConfigFile));               } catch (DocumentException e) {                   e.printStackTrace();                   throw new RuntimeException();               }           }                      public static BeanFactory getInstance(){               return instance;           }                      /**           * 根据产品编号取得Service系列产品           * @param serviceId           * @return           */           public synchronized Object getServiceObject(Class c){               //如果存在相关对象实例,返回               if(serviceMap.containsKey(c.getName())){                   return serviceMap.get(c.getName());               }               Element beanElt=(Element)doc.selectSingleNode("//service[@id=\""+ c.getName() + "\"]");               String className=beanElt.attributeValue("class");               Object service=null;                              try {                   service=Class.forName(className).newInstance();                                      //将创建好的对象放到Map中                   serviceMap.put(c.getName(), service);               } catch (Exception e) {                   throw new RuntimeException();               }               return service;           }                      /**           * 根据产品编号取得Dao系列产品           * @param daoId           * @return           */           public synchronized Object getDaoObject(Class c){               //如果存在相关对象实例,返回               if(daoMap.containsKey(c.getName())){                   return daoMap.get(c.getName());               }               Element beanElt=(Element)doc.selectSingleNode("//dao[@id=\""+c.getName()+"\"]");               String className=beanElt.attributeValue("class");               Object dao=null;                              try {                   dao=Class.forName(className).newInstance();                                      //将创建好的对象放到Map中                   daoMap.put(c.getName(), dao);               } catch (Exception e) {                   throw new RuntimeException();               }               return dao;           }                                 /**           * 测试           * @param args           */           public static void main(String[] args){               ItemManager itemManager=(ItemManager)BeanFactory.getInstance().getServiceObject(ItemManager.class);               System.out.println("itemManager"+itemManager);                              ItemDao itemDao=(ItemDao)BeanFactory.getInstance().getDaoObject(ItemDao.class);               System.out.println("itemDao:"+itemDao);                              FlowCardManager flowCardManager=(FlowCardManager)BeanFactory.getInstance().getServiceObject(FlowCardManager.class);               //FlowCardManager flowCardManager=new FlowCardManagerImpl();               System.out.println(flowCardManager);                              FlowCardDao flowCardDao=(FlowCardDao)BeanFactory.getInstance().getDaoObject(FlowCardDao.class);               //FlowCardDao flowCardDao=new FlowCardDaoImpl();               System.out.println("flowCardDao:"+flowCardDao);           }                  }

运行结果:

Java中dom4j怎么读取配置文件实现抽象工厂+反射

总结:

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。