我在 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 sample 和 guide 示例)。
关于java - 如何在 web.xml 中配置 spring-boot servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389996/