如何在web.xml中配置spring-boot servlet?

时间:2014-03-13 20:19:40

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

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

1 个答案:

答案 0 :(得分:24)

如果我从表面上看你的问题(你想要一个复制现有应用的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

但是,使用Tomcat和Spring中的本机Websocket支持(而不是使用Atmosphere),最好不要使用Tomcat和Spring中的本机Websocket支持(例如,参见websocket sampleguide)。