struts2框架执行流程及源码解析

struts2总体执行流程

一. 核心过滤器初始化
web.xml中配置核心过滤器StrutsPrepareAndExecuteFilter,servlet容器启动时会加载web.xml中配置的过滤器,执行过滤器的init方法,并实例化对象(有关过滤器知识请自行补习)。
init方法的核心是dispatcher = init.initDispatcher(config);
继续进入:

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

进入dispatcher.init();
看到如下代码块:

	init_DefaultProperties(); // [1]
    init_TraditionalXmlConfigurations(); // [2]
    init_LegacyStrutsProperties(); // [3]
    init_CustomConfigurationProviders(); // [5]
    init_FilterInitParameters() ; // [6]
    init_AliasStandardObjects() ; // [7]

此处为struts2框架初始化时会加载的配置文件,也标志了struts2加载配置文件的顺序。
1、init_DefaultProperties(); // [1]
加载org/apache/struts2/default.properties文件
点击源码查看:

private void init_DefaultProperties() {
    configurationManager.addContainerProvider(new DefaultPropertiesProvider());
}

继续点击进入:

public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
    try {
        PropertiesSettings defaultSettings = new PropertiesSettings("org/apache/struts2/default");
        loadSettings(props, defaultSettings);
    } catch (Exception e) {
        throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);
    }
}

继续点击 new PropertiesSettings(“org/apache/struts2/default”)进入:

public PropertiesSettings(String name) {
    URL settingsUrl = ClassLoaderUtil.getResource(name + ".properties", getClass());
    略......
}

此处已非常明显寻找路径为org/apache/struts2/default.properties文件。
这个文件其实是配置了一下常量。可自行查看。

2.init_TraditionalXmlConfigurations(); // [2]
加载
struts-default.xml
struts-plugin.xml
struts.xml

查看源码:

private void init_TraditionalXmlConfigurations() {
    String configPaths = initParams.get("config");
    if (configPaths == null) {
        configPaths = DEFAULT_CONFIGURATION_PATHS;
    }
    String[] files = configPaths.split("\\s*[,]\\s*");
    for (String file : files) {
        if (file.endsWith(".xml")) {
            if ("xwork.xml".equals(file)) {
                configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
            } else {
                configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
            }
        } else {
            throw new IllegalArgumentException("Invalid configuration file name");
        }
    }
}

其中:DEFAULT_CONFIGURATION_PATHS定义为:

private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";

此处已明显看出,这句话加载的配置文件为:
struts-default.xml
struts-plugin.xml
struts.xml

  • struts-default.xml

为struts2核心包中的文件。

  • struts-plugin.xml

在struts2的插件包中,struts2允许你定制多个插件,每个插件jar包中都有一个struts-plugin.xml配置文件。

  • struts.xml
    为用户自定义的配置文件,存放在资源目录下。

3.init_LegacyStrutsProperties(); // [3]
加载自定义的struts2.propertoes文件,可用于自定义常量覆盖struts2核心jar里面struts2.properties中定义的值。

点击查看源码

   private void init_LegacyStrutsProperties() {
        configurationManager.addContainerProvider(new PropertiesConfigurationProvider());
    }

继续进入:

public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
    final DefaultSettings settings = new DefaultSettings();
    loadSettings(props, settings);
}

继续:

    public DefaultSettings() {

        ArrayList<Settings> list = new ArrayList<Settings>();

        // stuts.properties, default.properties
        try {
            list.add(new PropertiesSettings("struts"));
        } catch (Exception e) {
            LOG.warn("DefaultSettings: Could not find or error in struts.properties", e);
        }
略.....
    }

进入new PropertiesSettings(“struts”),可以看到:

URL settingsUrl = ClassLoaderUtil.getResource(name + ".properties", getClass());

在资源目录下寻找struts2.properties配置文件。
4、init_CustomConfigurationProviders(); // [5]
用户自定义的配置,基本用不上,不详细展开了。
5、init_FilterInitParameters() ; // [6]
加载web.xml文件。
此处需要说明:
web.xml文件在servlet容器启动时候就会加载,如初始化过滤器等操作,此处加载是指struts2框架对web.xml文件中定义的struts2自己的标签进行解析。
具体原理不详。
6、init_AliasStandardObjects() ; // [7]
加载bean相关配置.
源码如下(只摘取一部分,可自行查阅):

    private void init_AliasStandardObjects() {
            configurationManager.addContainerProvider(new DefaultBeanSelectionProvider());
        }
点击进入:

    public void register(ContainerBuilder builder, LocatableProperties props) {
        alias(ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY, builder, props);
略...
        /** Checker is used mostly in interceptors, so there be one instance of checker per interceptor with Scope.DEFAULT **/
        alias(ExcludedPatternsChecker.class, StrutsConstants.STRUTS_EXCLUDED_PATTERNS_CHECKER, builder, props, Scope.DEFAULT);
        alias(AcceptedPatternsChecker.class, StrutsConstants.STRUTS_ACCEPTED_PATTERNS_CHECKER, builder, props, Scope.DEFAULT);
    
        switchDevMode(props);
    
        // Convert Struts properties into XWork properties
        convertIfExist(props, StrutsConstants.STRUTS_LOG_MISSING_PROPERTIES, XWorkConstants.LOG_MISSING_PROPERTIES);
        略...   
        LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
        loadCustomResourceBundles(props);
    }

总结一下,struts2框架对配置文件加载顺序为:

  • 核心包下的default.properties
  • 核心包下的struts-default.xml
  • 插件包下的struts-plugin.xml
  • 资源目录下自定义的struts.xml
  • 资源目录下自定义的struts.properties
  • web.xml

二、拦截器
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现。
源码解析:

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    try {
        if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
            chain.doFilter(request, response);
        } else {
            prepare.setEncodingAndLocale(request, response);
            prepare.createActionContext(request, response);
            prepare.assignDispatcherToThread();
            request = prepare.wrapRequest(request);
            ActionMapping mapping = prepare.findActionMapping(request, response, true);
            if (mapping == null) {
                boolean handled = execute.executeStaticResourceRequest(request, response);
                if (!handled) {
                    chain.doFilter(request, response);
                }
            } else {
                execute.executeAction(request, response, mapping);
            }
        }
    } finally {
        prepare.cleanupRequest(request);
    }
}

进入execute.executeAction方法:

public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
        throws ServletException {

    Map<String, Object> extraContext = createContextMap(request, response, mapping);

    // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
    ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
    boolean nullStack = stack == null;
    if (nullStack) {
        ActionContext ctx = ActionContext.getContext();
        if (ctx != null) {
            stack = ctx.getValueStack();
        }
    }
    if (stack != null) {
        extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
    }

    String timerKey = "Handling request from Dispatcher";
    try {
        UtilTimerStack.push(timerKey);
        String namespace = mapping.getNamespace();
        String name = mapping.getName();
        String method = mapping.getMethod();

        ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                namespace, name, method, extraContext, true, false);

        request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());

        // if the ActionMapping says to go straight to a result, do it!
        if (mapping.getResult() != null) {
            Result result = mapping.getResult();
            result.execute(proxy.getInvocation());
        } else {
            proxy.execute();
        }

        // If there was a previous value stack then set it back onto the request
        if (!nullStack) {
            request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
        }
    } catch (ConfigurationException e) {
        logConfigurationException(request, e);
        sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);
    } catch (Exception e) {
        if (handleException || devMode) {
            sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        } else {
            throw new ServletException(e);
        }
    } finally {
        UtilTimerStack.pop(timerKey);
    }
}

可以看到此处创建了Action的代理对象。进入创建代理对象内部查看:

public ActionProxy createActionProxy(ActionInvocation inv, String namespace, String actionName, String methodName, boolean executeResult, boolean cleanupContext) {

    DefaultActionProxy proxy = new DefaultActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
    container.inject(proxy);
    proxy.prepare();
    return proxy;
}

继续进入prepare方法,然后进入init方法:

public void init(ActionProxy proxy) {
    this.proxy = proxy;
    Map<String, Object> contextMap = createContextMap();

    // Setting this so that other classes, like object factories, can use the ActionProxy and other
    // contextual information to operate
    ActionContext actionContext = ActionContext.getContext();

    if (actionContext != null) {
        actionContext.setActionInvocation(this);
    }

    createAction(contextMap);

    if (pushAction) {
        stack.push(action);
        contextMap.put("action", action);
    }

    invocationContext = new ActionContext(contextMap);
    invocationContext.setName(proxy.getActionName());

    // get a new List so we don't get problems with the iterator if someone changes the list
    List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
    interceptors = interceptorList.iterator();
}

此处拿到了拦截器集合并以迭代器的形式返回,这里的迭代器集合就是struts-default.xml文件中定义的默认拦截器,共18个。

生成代理对象后继续执行,execute方法,如下:

public String execute() throws Exception {
    ActionContext nestedContext = ActionContext.getContext();
    ActionContext.setContext(invocation.getInvocationContext());

    String retCode = null;

    String profileKey = "execute: ";
    try {
        UtilTimerStack.push(profileKey);

        retCode = invocation.invoke();
    } finally {
        if (cleanupContext) {
            ActionContext.setContext(nestedContext);
        }
        UtilTimerStack.pop(profileKey);
    }

    return retCode;
}

下面就是拦截器的核心执行过程,进入invoke方法,如下:

    public String invoke() throws Exception {
        String profileKey = "invoke: ";
        try {
            UtilTimerStack.push(profileKey);

            if (executed) {
                throw new IllegalStateException("Action has already executed");
            }

            if (interceptors.hasNext()) {
                final InterceptorMapping interceptor = interceptors.next();
                String interceptorMsg = "interceptor: " + interceptor.getName();
                UtilTimerStack.push(interceptorMsg);
                try {
                                resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
                            }
                finally {
                    UtilTimerStack.pop(interceptorMsg);
                }
            } else {
                resultCode = invokeActionOnly();
           }
略.....
    }

intercept方法就是真正执行拦截的方法,可以随便找一个拦截器进入查看,如ExceptionMappingInterceptor(默认的18个拦截器之一),如下:

public String intercept(ActionInvocation invocation) throws Exception {
    String result;

    try {
        result = invocation.invoke();
    } catch (Exception e) {
        if (isLogEnabled()) {
            handleLogging(e);
        }
        List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();
        ExceptionMappingConfig mappingConfig = this.findMappingFromExceptions(exceptionMappings, e);
        if (mappingConfig != null && mappingConfig.getResult()!=null) {
            Map parameterMap = mappingConfig.getParams();
            // create a mutable HashMap since some interceptors will remove parameters, and parameterMap is immutable
            invocation.getInvocationContext().setParameters(new HashMap<String, Object>(parameterMap));
            result = mappingConfig.getResult();
            publishException(invocation, new ExceptionHolder(e));
        } else {
            throw e;
        }
    }

    return result;
}

发现在拦截方法内部又调用了拦截方法,又会回到前面的if (interceptors.hasNext())进行判断,迭代器中如果还有下一个元素,说明18个默认拦截器还没全部执行完,如此递归反复执行。

拦截器的套路和spring的AOP非常相似,都是利用动态代理的技术,对请求进行特定的处理。struts2还可以自定义拦截器,针对某些特定方法的执行进行拦截,利用反射的手段使目标方法执行,并在执行前后做特定的事情,以动态的改变或者增强他们的职能。

发布了6 篇原创文章 · 获赞 0 · 访问量 125

猜你喜欢

转载自blog.csdn.net/li210530/article/details/99167943