在 Spring 中混合 web.xml 和 AbstractAnnotationConfigDispatcherServletInitializer [英] Mixing web.xml and AbstractAnnotationConfigDispatcherServletInitializer in Spring

查看:53
本文介绍了在 Spring 中混合 web.xml 和 AbstractAnnotationConfigDispatcherServletInitializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring 上有一个应用程序并使用 Java Configs 来配置和初始化我的应用程序,因此我没有 web.xml.这是我的 Web 初始值设定项的样子,

I have an application on Spring and using Java Configs to configure and initialize my application, so that I have no web.xml. Here is how my web initializer looks like,

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{PublicApiConfig.class, MobileConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/*"};
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        LoggingFilter loggingFilter = new LoggingFilter();
        return new Filter[]{characterEncodingFilter, loggingFilter};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[0];
    }
}

我需要实现 tomcat 会话复制,出于目的,我需要将应用程序设为 可分发.使用传统的 web.xml,我可以添加 <distributable/> 属性,仅此而已.然而,据我所知,没有办法通过 Java 配置来做到这一点.

I need to implement tomcat session replication, and for the sake of purpose I need to have application as distributable. With traditional web.xml I could add <distributable/> attribute and thats it. However as far as I understand there is no way to do this via Java Configs.

我的问题是是否可以混合使用 web.xml 和 java 配置,例如拥有

My question is if it is possible to have mixed web.xml and java configs, e.g. to have

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <distributable/>

</web-app>

并将其包含在 WebInitializer 中.

推荐答案

根据 Servlet 3.0 规范,只要 web-app 版本 >= 3.0 并且 metadata-complete 属性为,就可以将 web.xml 与 Programmatic servlet 注册混合使用假(默认).使用您当前的配置,它应该可以工作

According to the Servlet 3.0 specification its possible to mix web.xml with Programmatic servlet registration as long as web-app version >= 3.0 and metadata-complete attribute is false (default). With your current configuration it should work

这篇关于在 Spring 中混合 web.xml 和 AbstractAnnotationConfigDispatcherServletInitializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆