使用tomcat和cxf-servlet进行春季引导 [英] spring-boot with tomcat and cxf-servlet

查看:100
本文介绍了使用tomcat和cxf-servlet进行春季引导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用spring-boot来支撑嵌入式Tomcat。我想在应用程序中为一组Web服务使用CXF,但是我不知道如何站起CXF Servlet。

I'm trying to stand up an embedded Tomcat with spring-boot. I want to use CXF for a set of web services in the app but I can not figure out how to stand up the CXF servlet.

我的Main类如下所示……

My Main class looks like this...

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.connecture.services.documentservice.webservice"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(new Class[] { Application.class, CfxInitializer.class }, args);
    }
    
    @Bean
  public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
      TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory("", 8080);
      return factory;
  }

}

我的CfxInitializer就是这样...

And my CfxInitializer like this...

public class CfxInitializer implements ServletContextInitializer
{

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException
  {
    XmlWebApplicationContext rootContext = new XmlWebApplicationContext();  
    rootContext.setConfigLocations(new String[] { "classpath*:applicationContext.xml" });  
    servletContext.addListener(new ContextLoaderListener(rootContext));  

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("CXFServlet", CXFServlet.class);  
    dispatcher.addMapping("/api/*");  
  }

}

当我尝试使用典型命令 ./ gradlew build&& java -jar build / libs / gs-spring-boot-0.1.0.jar

When I try to build and start the jar with the typical command ./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar

我收到多个上下文的异常。

I get an Exception for multiple Contexts.

Java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:277)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4971)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

这是一个更完整的粘贴框- http://pastebin.com/bcJ2ULhM

Here is a more complete pastebin - http://pastebin.com/bcJ2ULhM

与Dave的答案类似,我可以通过删除ServletContextInitializer并将其添加到Application Class中来修复它。

Similarly to Dave's answer I was able to fix it by removing the ServletContextInitializer and adding a bean to the Application Class.

@Bean
  public ServletRegistrationBean servletRegistrationBean(){
      return new ServletRegistrationBean(new CXFServlet(),"/api/*");
  }


推荐答案

Spring Boot嵌入式servlet功能旨在与 Servlet ServletRegistration @Beans 一起使用,而不是使用 ContextLoaderListener (这似乎是试图窃取 ServletContext 属性作为根上下文)。尝试为您的servlet添加 ServletRegistration ;如果它是Spring已知的,并且假设它具有允许您更改应用程序上下文或上下文位置的接口,那么您应该能够在注册中对其进行配置。

The Spring Boot embedded servlet features are designed to work with Servlet and ServletRegistration @Beans, and not with the ContextLoaderListener (which looks like it is trying to steal the ServletContext attribute for the root context). Try adding a ServletRegistration for your servlet instead; if it is Spring aware, assuming it has an interface that lets you change the application context or the context location, then you should be able to configure it in the registration.

这篇关于使用tomcat和cxf-servlet进行春季引导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆