Java USES dom4j to parse the XML configuration file to implement the abstract factory reflection example

  • 2020-04-01 02:42:21
  • OfStack

Logical description:

Now we want to add the interface layer to layer B and layer D and use the factory. Instead, we can think of creating B and creating D as two series, and then we can create them using an abstract factory.

Configuration file: beans-config.xml. Service -class and dao-class correspond to two series of products respectively. The id in the submenu corresponds to the namespace of the interface, and the class corresponds to the namespace of the implementation 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>   

The abstract factory : the BeanFactory. The configuration file is read, the associated objects are retrieved, and the associated objects are saved in a 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;   

       
    public class BeanFactory {   

        private static BeanFactory instance=new BeanFactory();   

        //System default configuration file name & NBSP;  
        private final String beansConfigFile="beans-config.xml";   

        //Save the Dao related object & cake;  
        private Document doc;   

           
        private Map serviceMap = new HashMap();//Save the Service related object & cake;  
        private Map daoMap = new HashMap();//Save the Dao related object & cake;  

           
        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;   
        }   

           
        public synchronized Object getServiceObject(Class c){   
            //Returns & NBSP if an associated object instance exists;  
            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();   

                //Place the created object into a Map & NBSP;  
                serviceMap.put(c.getName(), service);   
            } catch (Exception e) {   
                throw new RuntimeException();   
            }   
            return service;   
        }   

           
        public synchronized Object getDaoObject(Class c){   
            //Returns & NBSP if an associated object instance exists;  
            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();   

                //Place the created object into a Map & NBSP;  
                daoMap.put(c.getName(), dao);   
            } catch (Exception e) {   
                throw new RuntimeException();   
            }   
            return dao;   
        }   

           
           
        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);   
        }   

    }   
    


Related articles: