【问题标题】:How to configure spring-boot servlet like in web.xml?如何在 web.xml 中配置 spring-boot servlet?
【发布时间】:2014-04-18 20:36:27
【问题描述】:

我在 web.xml 中有一个简单的 servlet 配置:

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
    <init-param>
        <param-name>org.atmosphere.servlet</param-name>
        <param-value>org.springframework.web.servlet.DispatcherServlet</param-value>
    </init-param>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>net.org.selector.animals.config.ComponentConfiguration</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

如何为 SpringBootServletInitializer 重写?

【问题讨论】:

  • 不清楚您要做什么。如果您想要使用 Servlet 3.0 的完全相同的 Spring 应用程序,为什么要定义不同的 Servlet 类型(MeteorServlet 与 DispatcherServlet)?您在 web.xml 中加载的配置类不会在任何地方显式使用。您还扩展了SpringBootServletInitializer,但似乎没有覆盖关键的configure 方法。
  • 对不起,也许是您误入歧途的代码示例。我需要配置 Meteor Servlet,如 web.xml 中所述
  • 抱歉,没有关注。 web.xml 有一个DispatcherServlet。如果您想要另一种 servlet 作为默认 servlet,请使用答案中的代码,但使用不同的 servlet 类(尽管 bean 名称仍然必须是“DispatcherServlet”)。
  • 啊,我明白了。我错过了流星 servlet 声明。有机会我会更新答案。
  • 谢谢戴夫!你解决了我的问题。

标签: java spring spring-mvc servlets spring-boot


【解决方案1】:

如果我从表面上看你的问题(你想要一个复制你现有应用程序的SpringBootServletInitializer),我猜它看起来像这样:

@Configuration
public class Restbucks extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Restbucks.class, ComponentConfiguration.class);
    }

    @Bean
    public MeteorServlet dispatcherServlet() {
        return new MeteorServlet();
    }

    @Bean
    public ServletRegistrationBean dispatcherServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
        Map<String,String> params = new HashMap<String,String>();
        params.put("org.atmosphere.servlet","org.springframework.web.servlet.DispatcherServlet");
        params.put("contextClass","org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
        params.put("contextConfigLocation","net.org.selector.animals.config.ComponentConfiguration");
        registration.setInitParameters(params);
        return registration;
    }

}

更多详情请参阅docs on converting an existing app

但是,与使用 Atmosphere 相比,这些天您可能会更好地使用 Tomcat 和 Spring 中的本机 Websocket 支持(参见 websocket sampleguide 示例)。

【讨论】:

  • 对于其他 url 映射,请使用 registration.addUrlMappings("/whatever/*", "/whatever2/*");
  • 我在研究如何为 Spring Boot servlet 定义过滤器列表时发现了这篇文章,因为尝试允许匿名访问 Spring Boot 应用程序中的命名 servlet 导致出现错误消息。你愿意发表评论吗?这是链接:stackoverflow.com/questions/36489253/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 2012-11-13
  • 2015-06-05
  • 2013-10-03
  • 2016-03-09
  • 2017-07-08
相关资源
最近更新 更多