繁体   English   中英

未注入Apache CXF Spring bean

[英]Apache CXF spring bean is not injected

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

在码头工作得很好。 但是在Tomeeif中不起作用。 如果我使用ContextLoaderListener加载cxf bean,则不会公开Web服务。 之后,我使用config-location。 对于码头,我没有为cxf使用单独的xml。 我将cxf bean合并到我的应用程序bean中,由调度程序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)为null,因此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或代码中添加任何参数。

TomEE是全堆栈Java EE服务器。 如果要部署基于Spring的应用程序,则可能不需要使用beans.xml启用CDI。

如果您绝对肯定需要同时启用CDI和Spring,那么我建议您做两件事来解决该问题:一种使用调试器来确定未注入的bean是Spring还是CDI bean。 第二,打开冗长的Spring日志,看看它注入了什么。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM