关于JFig的一点使用心得

在《深入浅出hibernate》一书中看到在DAO模式中使用开源组件JFig来管理xml配置文件,于是借用了其中的代码用在自己的Struts程序中,主要涉及到五个文件DAOFactory.java,DAOConfig.java,ClassToolKit.java,DAOException.java以及dao.xml。工作原理可简单描述为:JFig根据配置文件(这里是dao.xml)中 接口类名和对应实现类的类名的对应关系动态生成实现类的实例,该实例保存在静态的HashMap的value中(key中保存的是对应的接口类), DAOFactory以指定的接口类名为key值从Map中返回实现类实例的引用。第一次只用在dao模式中,没出什么问题。第二次我将JFig 用在项目中另一个工厂模式中,蛮以为不会出什么差错,结果却报错
java.lang.NullPointerException
  at org.igfay.jfig.JFig.getSectionAsProperties(JFig.java:462)
  at org.igfay.jfig.JFig.getSectionAsProperties(JFig.java:449)
............
/
DAOConfig.java
public static synchronized HashMap load() {
        HashMap<Class, Class> daoMap = new HashMap<Class, Class>();
        JFigLocator daoLocator = new JFigLocator(DAO_CONFIG_FILE);
        JFigIF daoConfig = JFig.getInstance(daoLocator);
        Properties prop = daoConfig.getSectionAsProperties(DAO_CONFIG_SECTION);

        Enumeration enumSection = prop.keys();
        while (enumSection.hasMoreElements()) {
            String daoIface = (String) enumSection.nextElement();
            String daoImpl = prop.getProperty(daoIface);

            try {
                Class iface = ClassToolKit.loadClass(daoIface);
                Class impl = ClassToolKit.loadClass(daoImpl);
                daoMap.put(iface, impl);
            } catch (ClassNotFoundException e) {
                log.error("No Class Found=>" + e);
            }
        }
        return daoMap;
    }
出错是在load方法中的Properties prop = daoConfig.getSectionAsProperties(DAO_CONFIG_SECTION)一行。个人感觉一般是配置文件出错才会报该异常,但我的配置文件没发现什么问题啊,另外奇怪的是该工厂模式中JFig使用正常(该工厂模式先被使用),
出现异常的是 接下来的DAO模式 使用的 JFig,这说明配置文件没有问题。接下来两天里我反复调试的N次,结果还是发现不了什么有明显错误,但可以确认的是头一使用JFig不会出现问题,在第二次使用时就报该错误。后来用MyEclipse中的调试功能才发现其中的问题,两次使用JFig对应的配置文件是service.xml和dao.xml。问题出在    JFigIF daoConfig = JFig.getInstance(daoLocator);
分析源代码
public class JFig implements JFigIF {
..............
    private static JFigIF configSingleton;//
configSingleton为静态
................
  private static JFigIF getConfigSingleton(JFigLocatorIF jfigLocator) throws JFigException {
// 由于configSingleton为静态, 第一次使用时 configSingleton被赋值 ,第二次在DAO模式中使用时由于 //configSingleton不为空所以 JFig.getInstance(daoLocator) 返回的是第一次的结果,也就是说JFig在第二次中使用的 // 其实是第一次的配置文件dao.xml
        if (configSingleton == null) {
            log.debug("Create singleton object.");
            configSingleton = new JFig(jfigLocator);

            log.debug("Created singleton object");
        }
        return configSingleton;
    }
........................
public synchronized static JFigIF getInstance(JFigLocatorIF jfigLocator) {
        // default to a "null" jfig if there are problems
        JFigIF myConfigSingleton = new JFig();
        try {
            myConfigSingleton = getConfigSingleton(jfigLocator);
        } catch (JFigException e) {
            log.error("*** Unable to create configuration dictionary. "        + e.getMessage()+ " ***");
        } catch (NoClassDefFoundError e) {
            log.error("*** Unable to create configuration dictionary. NoClassDefFoundError "+ e.getMessage() +"/nThis may be the result of incompatible XML jars. See JFIG documentation.");
        } catch (Throwable e) {
            log.error("*** Unhandled exception initializing JFig "+e.getMessage(),e);
        }
        setInstance(myConfigSingleton);
        return myConfigSingleton;
    }
..............
}
解决方法:
1.将两个配置文件的内容写到同一个配置文件中,例如dao.xml 和service.xml合并到base.cofnig.xml中,两次使用时将配置文件指定为该文件即可
2.在第三个配置文件中包含进前面两个配置文件,
<?xml version="1.0" encoding="GBK"?>
<CONFIGURATION>
<INCLUDE name="dao.xml" />
<INCLUDE name="service.xml" />
</CONFIGURATION>
这种用法还没有试过,仅提供思路,大家自己可以试下。
http://someok.blogbus.com/logs/146438.html
总的来说还是因为第一次用JFig,对它的用法还不熟练造成的。
本人是初学者,如有描述不对的地方,还请各位指正
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值