2016-05-13 77 views
0

我使用Apache CXF 3和Spring 3开发了基于SOAP的Web服务,并部署在Tomee上。我有2个XML(1. beans.xml(cxf服务)2. spring-servlt.xml)。我在我的cxf服务xml中引用了一些DAO层bean。它没有被注射。当我分析了CXFServlet类时。 loadBus方法从WebApplicationContextUtils类获取应用程序上下文。Apache CXF spring bean未注入

它在Jetty中正常工作。但它不在Tomeeif工作。如果我使用ContextLoaderListener加载cxf bean,则Web服务不会公开。之后我使用了config-location。对于码头,我没有使用单独的xml作为cxf。我在我的应用豆合并CXF豆,它是由调度员的servlet加载,并正在良好

CXFServlet.java

protected void loadBus(ServletConfig sc) { 
    ApplicationContext wac = WebApplicationContextUtils. 
     getWebApplicationContext(sc.getServletContext()); 

private ApplicationContext createSpringContext(ApplicationContext ctx, 
                ServletConfig servletConfig, 
                String location) { 
     XmlWebApplicationContext ctx2 = new XmlWebApplicationContext(); 
     createdContext = ctx2; 

     ctx2.setServletConfig(servletConfig); 
     Resource r = ctx2.getResource(location); 
     try { 
      InputStream in = r.getInputStream(); 
      in.close(); 
     } catch (IOException e) { 
      //ignore 
      r = ctx2.getResource("classpath:" + location); 
      try { 
       r.getInputStream().close(); 
      } catch (IOException e2) { 
       //ignore 
       r = null; 
      } 
     } 
     try { 
      if (r != null) { 
       location = r.getURL().toExternalForm(); 
      } 
     } catch (IOException e) { 
      //ignore 
     }   
     if (ctx != null) { 
      ctx2.setParent(ctx); 
      String names[] = ctx.getBeanNamesForType(Bus.class); 
      if (names == null || names.length == 0) { 
       ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml", 
                 location});     
      } else { 
       ctx2.setConfigLocations(new String[] {location});         
      } 
     } else { 
      ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml", 
                location}); 
      createdContext = ctx2; 
     } 
     ctx2.refresh(); 
     return ctx2; 
    } 

如果(CTX!= NULL){ ctx2.setParent(CTX );

由于ctx(wac)为空cxf无法设置父上下文。 bean注入不起作用。请指导我为什么这个wac为空。

的web.xml

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/connector/*</url-pattern> 
</servlet-mapping> 


<servlet> 
    <servlet-name>CXFServlet</servlet-name> 
    <display-name>CXF Servlet</display-name> 
    <servlet-class> 
     org.apache.cxf.transport.servlet.CXFServlet 
    </servlet-class> 
    <init-param> 
     <param-name>config-location</param-name> 
     <param-value>/WEB-INF/beans.xml</param-value>  
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>CXFServlet</servlet-name> 
    <url-pattern>/services/*</url-pattern> 
    </servlet-mapping> 

我尝试了所有最重要的选项已经在形式提供。 我是否需要在Tomee或我的代码中添加任何参数。

+0

如果我使用ContextLoaderListener加载cxf bean,您是否已经将ContextLoaderListener添加到web.xml – andy

+0

,Web服务未公开。之后我使用了config-location。它在码头工作正常。但它不在Tomee工作。 – Gnana

回答

0

TomEE是一个完整的堆栈Java EE服务器。如果您正在部署基于Spring的应用程序,则可能不需要以启用CDI with beans.xml。

如果你是绝对正面你需要启用CDI和Spring,我建议你做两件事来解决这个问题:一个使用调试器来判断未注入的bean是否是Spring或CDI bean。其次,打开详细的Spring日志记录来查看它在那里注入的内容。

祝你好运!