`
hain
  • 浏览: 448971 次
  • 来自: ...
社区版块
存档分类
最新评论

struts2配置文件加载流程分析

阅读更多
首先org.apache.struts2.dispatcher. FilterDispatcher.java 中的init()方法,
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        String param = filterConfig.getInitParameter("packages");//由此可以看出可以增加参数packages
        String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging";
        if (param != null) {
            packages = param + " " + packages;
        }
        this.pathPrefixes = parse(packages);
        dispatcher = new Dispatcher(filterConfig.getServletContext());
}

org.apache.struts2.dispatcher. Dispatcher.java
首先执行静态代码块:
    static {
        ObjectFactory.setObjectFactory(new StrutsObjectFactory());
        ActionProxyFactory.setFactory(new StrutsActionProxyFactory());
}
执行构造方法
    public Dispatcher(ServletContext servletContext) {
        init(servletContext);
}
然后执行init()方法:

boolean reloadi18n = Boolean.valueOf(
(String) Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();
上面的语句读取了properties文件。


读取properties文件的流程:
首先执行Org.apache.struts2.dispatcher.Dispatcher.java 类的方法init()中的语句
        boolean reloadi18n = Boolean.valueOf((String)
Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();

然后执行了org.apacher.struts2.config.Setting.java类中的方法
    public static String get(String name) throws IllegalArgumentException {
        String val = getInstance().getImpl(name);
        return val;
    }
然后执行了
    public static Settings getInstance() {
        return (settingsImpl == null) ? getDefaultInstance() : settingsImpl;
}
然后执行了
    private static Settings getDefaultInstance() {
        if (defaultImpl == null) {
            // Create bootstrap implementation
            defaultImpl = new DefaultSettings(); //产生了一个DefaultSettings对象

            // Create default implementation
            try {
                String className = get(StrutsConstants.STRUTS_CONFIGURATION);

                if (!className.equals(defaultImpl.getClass().getName())) {
                    try {
                        // singleton instances shouldn't be built accessing request or session-specific context data
                        defaultImpl = (Settings) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className), null);
                    } catch (Exception e) {
                        LOG.error("Could not instantiate settings", e);
                    }
                }
            } catch (IllegalArgumentException ex) {
                // ignore
            }
        }

        return defaultImpl;
}

然后首先执行了语句defaultImpl = new DefaultSettings();
    public DefaultSettings() {
        // Create default implementations
        // Use default properties and struts.properties
        ArrayList list = new ArrayList();

        try {
           list.add(new PropertiesSettings("struts"));//从这里加载struts.properties文件
        } catch (Exception e) {
            log.warn("Could not find or error in struts.properties", e);
        }

        try {
        list.add(new PropertiesSettings("org/apache/struts2/default")); //加载默认的defualt.properties文件
        } catch (Exception e) {
            log.error("Could not find org/apache/struts2/default.properties", e);
        }

        Settings[] configList = new Settings[list.size()];//定义了Settings对象数组
        config = new DelegatingSettings((Settings[]) list.toArray(configList));

        // Add list of additional properties settingss
        try {
            StringTokenizer configFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES), ",");
加载了用户自己定义的properties文件。
            while (configFiles.hasMoreTokens()) {
                String name = configFiles.nextToken();

                try {
                    list.add(new PropertiesSettings(name));
                } catch (Exception e) {
                    log.error("Could not find " + name + ".properties. Skipping");
                }
            }

            configList = new Settings[list.size()];
            config = new DelegatingSettings((Settings[]) list.toArray(configList));
        } catch (IllegalArgumentException e) {
            // thrown when Settings is unable to find a certain property
            // eg. struts.custom.properties in default.properties which is commented
            // out
        }

        // Add additional list of i18n global resource bundles
        try {

            LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
            StringTokenizer bundleFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");

            while (bundleFiles.hasMoreTokens()) {
                String name = bundleFiles.nextToken();
                try {
                    log.info("Loading global messages from " + name);
                    LocalizedTextUtil.addDefaultResourceBundle(name);
                } catch (Exception e) {
                    log.error("Could not find " + name + ".properties. Skipping");
                }
            }
        } catch (IllegalArgumentException e) {
            // struts.custom.i18n.resources wasn't provided
        }
}
看看org.apache.struts2.config.DelegatingSettings.java下面的方法。
/**
     * Gets the specified property - calls getImpl(String) method on config objects in config list
     * until successful.
     *
     * @see #get(String)
     */
    public String getImpl(String name) throws IllegalArgumentException {
        // Delegate to the other settings
        IllegalArgumentException e = null;

        for (int i = 0; i < configList.length; i++) {
            try {
                return configList[i].getImpl(name);
            } catch (IllegalArgumentException ex) {
                e = ex;

                // Try next config
            }
        }

        throw e;
    }
分享到:
评论
1 楼 allenBen 2007-09-29  
怎么你写的一些代码我没看到啊难道源码还会不同?2.0.9

相关推荐

    struts2流程与流程图

     ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到需要调用的Action类。例如,用户注册示例将找到UserReg类。  ActionProxy创建一个ActionInvocation实例,同时ActionInvocation通过...

    Struts2快速学习步骤

    本章学习目标  struts2 的概念和作用  struts2 的HelloWorld ... struts2 配置文件加载  struts.xml 配置文件详解  struts2 常量文件修改  Action 动作类的三种写法  Action 动作类的三种访问方式

    Struts1配置

    Struts1配置文件各标签属性的详细解释与用法,包括struts1的工作流程

    SSH的jar包.rar

    SSH(struts+spring+hibernate)的jar包 SSH 通常指的是 Struts2 做前端控制器,Spring ...配置文件加载顺序为:default.properties -&gt; struts-default.xml -&gt; struts-plugins.xml -&gt; struts.xml -&gt; struts.locale。

    Java求职面试宝典各大公司常考知识点

    1.6. hibernate中一对多配置文件返回的是什么? 4 1.7. update()和saveOrUpdate()的区别? 4 1.8. hibernate拒绝连接、服务器崩溃的原因?最少写5个 4 1.9. hibernate如何管理缓存 4 1.10. 使用Hibernate的优点...

    JFinalDemo流程

    自动加载修改后的java文件,开发过程中无需重启web server AOP支持,拦截器配置灵活,功能强大 Plugin体系结构,扩展性强 多视图支持,支持FreeMarker、JSP、Velocity 强大的Validator后端校验功能 功能...

    从J2SE到J2EE知识点介绍

    2. 通过web.xml配置文件访问servlet的流程 129 (七) servlet里面的跳转和传参数的方法 130 1. 跳转 130 2. 传参数 131 (八) jsp中文乱码问题 131 1. JSP页面乱码 132 2. 表单提交中文时出现乱码 134 3. 关于jsp在...

    健身房管理信息系统设计.doc

    ,控制器时整个程序执行流程的调度者,在Struts中基本的控制器 组建是ActionServlet类,但是Action- Servlet类不包含任何控制信息,程序的所有调度信息都需要在Struts-con- fig.xml配置文件中设置。 Structs工作原理...

    J2EE应用开发详解

    118 8.3.5 Struts2配置文件 119 8.4 Action的配置方式 121 8.4.1 动态方法调用 121 8.4.2 设置action元素的method属性 122 8.4.3 使用通配符配置action 122 8.4.4 默认action 123 8.5 拦截器Interceptor 123 8.5.1 ...

    外文翻译 stus MVC

    Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design. This definition implies that Struts is a framework, rather than a library, but Struts also contains...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    Spring面试题

    在struts配置文件中配置具体的错误提示,再在FormBean中的validate()方法具体调用。 9. 说下Struts的设计模式 MVC模式: web应用程序启动时就会加载并初始化ActionServler。用户提交表单时,一个配置好的ActionForm...

    ssh(structs,spring,hibernate)框架中的上传下载

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

    基于SSH模拟当当网项目(电子商务平台)

    2) 引入Spring开发包和配置文件 applicationContext-base.xml 定义共同的datasource,sessionFactory applicationContext-dao.xml 定义DAO组件 applicationContext-service.xml 定义Service组件 ...

    Java语言基础下载

    第三十二章:配置Struts组件 654 学习目标 654 三个 XML文件和一个属性文件 655 Web应用部署描述符 web.xml 655 ActionServlet的参数的配置 656 应用资源文件 658 Ant构建文件 659 配置Tiles框架 660 内容总结 661 ...

    《MyEclipse 6 Java 开发中文教程》前10章

    9.3.1 Struts配置文件编辑器 171 9.3.2 Struts组件向导 173 9.4编写登录应用 175 9.4.1 应用的流程和目标 175 9.4.2 创建登录成功页面 175 9.4.3 使用新建Form,Action和JSP的向导创建关键组件 176 9.4.4 调整生成的...

    学习ssh框架日记

    4 struts 机制工作流程:在web应用启动时就会加载初始ActionServlet ,ActionServlet从struts-config.xml文件中读取配置信息,把它们存放到各种配置对象 5 spring 机制 1.spring mvc请所有的请求都提交给...

Global site tag (gtag.js) - Google Analytics