如何在web.xml中配置spring-boot servlet? [英] How to configure spring-boot servlet like in web.xml?
问题描述
我在web.xml中有一个简单的servlet配置:
I have a simple servlet configuration in web.xml:
<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?
How can I rewrite it for SpringBootServletInitializer?
推荐答案
如果我把问题看作面值(你想要一个 SpringBootServletInitializer
复制你现有的应用程序)我猜它会是这样的:
If I take your question at face value (you want a SpringBootServletInitializer
that duplicates your existing app) I guess it would look something like this:
@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;
}
}
参见转换文档现有应用以获取更多详细信息。
See docs on converting an existing app for more detail.
但是,使用Tomcat和Spring中的本机Websocket支持,而不是使用Atmosphere,现在可能会更好一些(见 websocket sample 和指南示例。)
But, rather than using Atmosphere, you are probably better off these days using the native Websocket support in Tomcat and Spring (see the websocket sample and guide for examples).
这篇关于如何在web.xml中配置spring-boot servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!