实现xwork配置文件的自动加载

本公司的业务开发平台中,在展现层用到了webwork作为控制框架,由于最近在公司的一个比较大的商业计划中,打算实现“应用集市”的功能,在技术上要实现业务模块的导入导出功能,因此要求系统必须能够实现自动加载一个业务模块的所有相关文件,而在持久层和业务层不存在任何问题,因为hibernate的mapping文件,和spring的配置文件xxx.bean.xml只要在classpath中,都是可以自动加载的,但是对于webwork的配置文件,用过的人都知道,一个单独的模块配置文件:xxxmodule_xwork.xml必须要在系统的xwork.xml文件中显式地include进来,这个模块的配置文件才能够自动地被加载到,实在是烦人。因此笔者想到了,能不能也像spring的配置文件一样,能够放到classpath下被自动地加载到,于是偶赶紧去看xwork的源码,终于被偶发现:xwork的配置文件加载都是依赖于com.opensymphony.xwork.config.providers.XmlConfigurationProvider这个类, 其中init方法的源码如下:

 public void init(Configuration configuration) {
        this.configuration = configuration;
        includedFileNames.clear();


        try {
            loadConfigurationFile(configFileName, null);
        } catch (ConfigurationException e) {
            throw e;
        } catch (Exception e) {
            LOG.fatal("Could not load XWork configuration file, failing", e);
            throw new ConfigurationException("Error loading configuration file " + configFileName, e);
        }
    }

可以看到loadConfigurationFile()这个方法真正地实现xwork.xml文件的加载,如果我在系统启动的时候先取到classpath中所有的xxxmodule_xwork.xml文件,然后传给这个init方法,循环调用loadConfigurationFile()这个方法,不就可以把classpath中所有的xwork相关的配置文件自动加载了么,因此代码改写如下:

    //add by xinpeng 20080316
    private List configFileNameList;

    //加入一个构造方法,将classpath下所有的xwork文件名称的集合传入
    public XmlConfigurationProvider(List configFileNameList) {
        this.configFileNameList = configFileNameList;
    }
    //end by xinpeng

public void init(Configuration configuration) {
        this.configuration = configuration;
        includedFileNames.clear();
        try {
            //modify by xinpeng 20080314,循环加载所有的xwork配置文件
            for (int i = 0, len = configFileNameList.size(); i < len; i++) {
                String configFileName = (String) configFileNameList.get(i);
                this.configFileName = configFileName;
                LOG.debug("the current config file name is : "+this.configFileName);
                loadConfigurationFile(configFileName, null);
            }
            //end by xinpeng
        } catch (Exception e) {
            LOG.fatal("Could not load XWork configuration file, failing:" + e);
            throw new ConfigurationException("Error loading configuration file " + configFileNameList, e);
        }
    }

然后再写一个监听器,在系统启动的时候,取得classpath下所有的xwork的配置文件,并通过XmlConfigurationProvider类的构造子传入,监听器的监听方法如下:

public void contextInitialized(ServletContextEvent event) {
        log.info("beginning to load xwork config file......");

        List<String> xworkConfigFileList = new ArrayList<String>();
        String[] fileList = ResourceFinder.getXwork();

        for (int i = 0, len = fileList.length; i < len; i++) {
            xworkConfigFileList.add(fileList[i]);
        }
        ConfigurationProvider configurationProvider = new XmlConfigurationProvider(xworkConfigFileList);
        ConfigurationManager.addConfigurationProvider(configurationProvider);
        log.info("all the xwork config file has been loaded successfully!");
    }

最后在web.xml文件中,配置上这个监听器,把xwork.xml文件中n多的include全部干掉,重启tomcat,OK全部成功了!,以后再也不用关心xwork配置文件的include问题,我们可以直接把它打包到xxxmodule.jar中,从而真正地实现了xwork配置文件自动加载功能。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值