002--Struts2配置文件加载顺序

通过上一篇,对Struts2有了一个基本的认识,并成功的运行了基于Struts2的第一个web项目。在讲解配置文件之前,先来看一下这些配置文件的加载顺序。

客户端的每次请求到服务器都要先经过Struts2的核心过滤器StrutsPrepareAndExecuteFilter,主要负责请求预处理和执行。

在预处理中主要是来加载配置文件,对应着源码中的init方法。而执行主要通过拦截器完成部分功能,对应着过滤器的doFilter方法。

预处理

首先,从web.xml中配置的StrutsPrepareAndExecuterFilter的init方法看起,方法如下:

public void init(FilterConfig filterConfig) throws ServletException {
        InitOperations init = new InitOperations();
        Dispatcher dispatcher = null;

        try {
            FilterHostConfig config = new FilterHostConfig(filterConfig);
            init.initLogging(config);
            dispatcher = init.initDispatcher(config);
            init.initStaticContentLoader(config, dispatcher);
            this.prepare = new PrepareOperations(dispatcher);
            this.execute = new ExecuteOperations(dispatcher);
            this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
            this.postInit(dispatcher, filterConfig);
        } finally {
            if(dispatcher != null) {
                dispatcher.cleanUpAfterInit();
            }

            init.cleanup();
        }

    }

init方法中调用了initDispatcher方法来加载配置文件,方法如下:

public Dispatcher initDispatcher(HostConfig filterConfig) {
        Dispatcher dispatcher = this.createDispatcher(filterConfig);
        dispatcher.init();
        return dispatcher;
    }

initDispatcher中,又调用了dispatcher.init()方法,这里面真正做了Struts2配置文件的加载,如下:

public void init() {
        if(this.configurationManager == null) {
            this.configurationManager = this.createConfigurationManager("struts");
        }

        try {
            this.init_FileManager();
            // 加载`org.apache.struts/default.properties`配置中常量
            this.init_DefaultProperties();
            // 加载struts-default.xml、struts-plugin.xml、struts.xml
            this.init_TraditionalXmlConfigurations();
            // 加载自定义的struts.properties
            this.init_LegacyStrutsProperties();
            // 加载用户配置的provider(提供对象)
            this.init_CustomConfigurationProviders();
            // 加载web.xml文件
            this.init_FilterInitParameters();
            // 加载标准对象
            this.init_AliasStandardObjects();
            Container ex = this.init_PreloadConfiguration();
            ex.inject(this);
            this.init_CheckWebLogicWorkaround(ex);
            if(!dispatcherListeners.isEmpty()) {
                Iterator i$ = dispatcherListeners.iterator();

                while(i$.hasNext()) {
                    DispatcherListener l = (DispatcherListener)i$.next();
                    l.dispatcherInitialized(this);
                }
            }

            this.errorHandler.init(this.servletContext);
        } catch (Exception var4) {
            if(LOG.isErrorEnabled()) {
                LOG.error("Dispatcher initialization failed", var4, new String[0]);
            }

            throw new StrutsException(var4);
        }
    }

通过源码分析,可以看到配置文件的加在顺序如下:

default.properties
struts-default.xml
struts-plugin.xml
struts.xml // 配置Action以及常量
struts.properties // 配置常量
web.xml // 配置核心过滤器及常量

前三个为Struts2内部配置文件,无法对其修改,我们所需要关注的是struts.xmlstruts.propertiesweb.xml三个文件。

因为这几个文件加载是有顺序的,后三个配置文件都可以修改Struts2常量值,需要注意的是,后者会覆盖前者。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值