我在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重写它?

最佳答案

如果我以您的面值来回答您的问题(您想要一个与您现有应用程序重复的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中的 native Websocket支持可能会更好一些(有关示例,请参见websocket sampleguide)。

09-11 18:30