---
title: "实现xwork配置文件的自动加载-摘自http://blog.csdn.net/snow_fox_yaya/article/details/2218141"
author: "乐溪925"
date: "2012-07-09T15:54:01Z"
tags: []
categories: ["行业应用"]
slug: "xwork-http-blog-csdn-net-snow-fox-yaya-article-details-2218141"
draft: false
lastmod: "2012-07-09T15:54:01Z"
id: 1583017
url: "https://jcsuper.iteye.com/blog/1583017"
---

一、
如果配置文件struts.xml的内容太多，或者是团队开发时希望各自struts2 的Action配置独立，而不互相干扰，可以将配置文件拆分为多个文件，这样可以减少工作量，便于维护。

常见的做法在默认的struts.xml中使用include来包含各个模块的配置文件。如果是多个配置文件时，就使用多个include元素。新加模块的action配置时，还是要修改struts.xml。
于是，有人提出来，是不是可以在不修改文件时，自动引入各个模块的配置文件呢？ 当然，我们可以重写Filter类，配合使用Filter初始化参数来实现多配置文件的自动引入。 事实上，还有更简单的方法，那就是在include中使用通配符：
Xml代码  
<struts>     
       ... ...     
    <include file="config/struts-*.xml"></include>     
</struts>    
 
然后，将各个模块的配置文件放在config包下就可以了。




二、本公司的业务开发平台中，在展现层用到了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*);
        }
        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配置文件自动加载功能。