Java 如何像在 web.xml 中一样配置 spring-boot servlet?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22389996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure spring-boot servlet like in web.xml?
提问by Selector
I have a simple servlet configuration in web.xml:
我在 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>
How can I rewrite it for SpringBootServletInitializer?
如何为 SpringBootServletInitializer 重写它?
采纳答案by Dave Syer
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:
如果我从表面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;
}
}
See docs on converting an existing appfor more detail.
有关更多详细信息,请参阅有关转换现有应用程序的文档。
But, rather than using Atmosphere, you are probably better off these days using the native Websocket support in Tomcat and Spring (see the websocket sampleand guidefor examples).
但是,与使用 Atmosphere 相比,现在使用 Tomcat 和 Spring 中的原生 Websocket 支持可能会更好(请参阅websocket 示例和指南以获取示例)。