服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

struts源码的学习之配置模块初始化的实现


  author    :    zhyiwww
e-mail    :    zhyiwww@163.com
date    :    2007-1-10
转载请注明出处 www.javaresearch.org
(copyright by @ zhangyi)

struts中的messageresource、plugin、数据源等,都是通过moduleconfig来实现的。
那么在actionservlet初始化上面的那些模块之前,就先要初始化moduleconfig,然后由moduleconfig来负责其初始化。
在actionservlet初始化moduleconfig的时候,先要初始化配置工厂,然后由配置工厂:
     initmoduleconfigfactory();
之后再实例化一个moduleconfig的对象。
moduleconfig moduleconfig = initmoduleconfig("", config);

那么这个工厂到底初始化了什么?
现看源代码:
    protected void initmoduleconfigfactory(){
        string configfactory = getservletconfig().getinitparameter("configfactory");
        if (configfactory != null) {
            moduleconfigfactory.setfactoryclass(configfactory);
        }
}
很明显,现从配置参数取得其配置,如果用户没有作配置,那么就使用默认配置,如果用户作了配置,那么就使用用户的配置。
如果用户作了配置的话,那么就执行设置成用户的工厂。如何设置的呢?
    public static void setfactoryclass(string factoryclass) {
        moduleconfigfactory.factoryclass = factoryclass;
        moduleconfigfactory.clazz = null;
    }
在此我们可以看到,直接给moduleconfigfactory.factoryclass赋值,为什么可以这样做呢?因为此变量是:
    protected static string factoryclass =
        "org.apache.struts.config.impl.defaultmoduleconfigfactory";
由此定义决定了可以使用此赋值方法。因为此变量是一个静态的变量。

正是因为此变量是一个静态的变量,所以在下面的得工厂生成对象的时候就可以创建一个用户自己的对象。
看一下是如何初始化moduleconfig,看下面的源代码:
   protected moduleconfig initmoduleconfig(string prefix, string paths)
        throws servletexception {
    /*************************************************************
这个地方,我们可以看到,此时就由moduleconfigfactory直接创建了一个工厂对象,而此时我们用的配置就是上面我们初始化后的配置。
如果用户自己做了配置,那么此时初始化的工厂就是用户指定后的工厂。如果没有的话,那么就初始化的时默认的工厂。
也就是
    protected static string factoryclass =
        "org.apache.struts.config.impl.defaultmoduleconfigfactory";
的一个实例。
*************************************************************/
           moduleconfigfactory factoryobject = moduleconfigfactory.createfactory();
        moduleconfig config = factoryobject.createmoduleconfig(prefix);

        // configure the digester instance we will use
        digester digester = initconfigdigester();

        // process each specified resource path
        while (paths.length() > 0) {
            digester.push(config);
            string path = null;
            int comma = paths.indexof(',');
            if (comma >= 0) {
                path = paths.substring(0, comma).trim();
                paths = paths.substring(comma + 1);
            } else {
                path = paths.trim();
                paths = "";
            }

            if (path.length() < 1) {
                break;
            }

            this.parsemoduleconfigfile(digester, path);
        }

        getservletcontext().setattribute(
            globals.module_key + config.getprefix(),
            config);

        // force creation and registration of dynaactionformclass instances
        // for all dynamic form beans we wil be using
        formbeanconfig fbs[] = config.findformbeanconfigs();
        for (int i = 0; i < fbs.length; i++) {
            if (fbs[i].getdynamic()) {
                fbs[i].getdynaactionformclass();
            }
        }

        return config;
    }

那么初始化配置模块的部分到底做了什么呢?
其实是生成了一个moduleconfig的对象。这个对象是由其工厂产生的,由什么样的工厂就会生成什么样的产品。所以如果是用户配置过的工厂,那么就会生成其对应的配置模块的实现。
先看默认的情况:
public class defaultmoduleconfigfactory extends moduleconfigfactory implements serializable{
    // --------------------------------------------------------- public methods

    /**
     * create and return a newly instansiated {@link moduleconfig}.
     * this method must be implemented by concrete subclasses.
     *
     * @param prefix module prefix for configuration
     */
    public moduleconfig createmoduleconfig(string prefix) {

        return new moduleconfigimpl(prefix);

    }
}
通过其默认工厂的实现,我们可以看到,其实例化了一个moduleconfigimpl的对象,这是moduleconfig的一种实现,也是当前struts的默认的实现。

    如果是用户配置了实现工厂的话,可能的实现就是:
public class usermoduleconfigfactory extends moduleconfigfactory implements serializable{
       public moduleconfig createmoduleconfig(string prefix) {

        return new moduleconfiguserimpl(prefix);

    }
}
    当然,如果要启用你的工厂的话,那么还要在你的配置文件中添加如下部分,在web.xml中修改如下部分:
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.actionservlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/web-inf/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
</init-param>

    <init-param>
      <param-name>configfactory</param-name>
      <param-value>org.aa.struts. usermoduleconfigfactory </param-value>
</init-param>

    <load-on-startup>0</load-on-startup>
  </servlet>

这样的话,你的工厂就可以生效了,也可以生成你自己的配置模块的实例了。

到此,配置模块的初始化也已经完成,下面就是要实现如何对各个不同的模块进行初始化了。

扫描关注微信公众号