如何在经典(非引导)Spring应用程序中注册MessageDispatcherServlet



我目前正在开发一个Web应用程序,该应用程序可以使用Spring 5.1.3和Spring-WS通过SOAP访问,并且不知道如何使用Java配置注册一个额外的servlet(在这种情况下,MessageDispatcherServlet用于Spring-WS)。我应该注意,这是一个非启动应用程序。

我咨询了官方的 Spring 文档寻求帮助,但是本指南面向 Spring Boot(它使用 Spring Boot 独有的ServletRegistrationBean)。根据该指南,MessageDispatcherServlet注册方式如下:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/*");
}
}

这看起来不错且简单明了,除了ServletRegistrationBean驻留在org.springframework.boot.web.servlet=> Spring Boot => 中,我无法使用。 如何在"非引导"标准 Spring 应用程序中注册我的MessageDispatherServlet?非常感谢任何提示或建议。

感谢大家的任何指点。我设法通过WebApplicationInitializer注册了MessageDispatcherServlet

public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebConfig.class);
container.addListener(new ContextLoaderListener(context));
// Message Dispatcher Servlet (SOAP)
MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
messageDispatcherServlet.setApplicationContext(context);
messageDispatcherServlet.setTransformWsdlLocations(true);
ServletRegistration.Dynamic messageDispatcher = container.addServlet("messageDispatcher", messageDispatcherServlet);
messageDispatcher.setLoadOnStartup(1);
messageDispatcher.addMapping("/ws/*");
}
}

一旦你知道怎么做,就非常简单:D

最新更新