2011-09-25 80 views
1

我使用Apache CXF与Spring,请告诉我如何CXFServlet读取MYAPP-WS-context.xml的的Apache CXF与Spring

<web-app> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:myapp-ws-context.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 

    <servlet> 
     <display-name>CXF Servlet</display-name> 
     <servlet-name>CXFServlet</servlet-name> 
     <servlet-class> 
      org.apache.cxf.transport.servlet.CXFServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>CXFServlet</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

回答

2

你见过的org.apache.cxf.transport.servlet.CXFServletsources(开源的)?

一切都是超过明确:

@Override 
protected void loadBus(ServletConfig sc) { 
    ApplicationContext wac = WebApplicationContextUtils. 
     getWebApplicationContext(sc.getServletContext()); 
    String configLocation = sc.getInitParameter("config-location"); 
    if (configLocation == null) { 
     try { 
      InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml"); 
      if (is != null && is.available() > 0) { 
       is.close(); 
       configLocation = "/WEB-INF/cxf-servlet.xml"; 
      } 
     } catch (Exception ex) { 
      //ignore 
     } 
    } 
    if (configLocation != null) { 
     wac = createSpringContext(wac, sc, configLocation); 
    } 
    if (wac != null) { 
     setBus(wac.getBean("cxf", Bus.class)); 
    } else { 
     setBus(BusFactory.newInstance().createBus()); 
    } 
} 

注意WebApplicationContextUtils是试图找到名为servlet上下文属性的应用程序上下文Spring类:org.springframework.web.context.WebApplicationContext.ROOT

0

其实你的classpath:myapp-ws-context.xml是由Spring读取的,而不是CXF。

通过在web.xml中加入下面的配置,它会被Spring读取和上下文会被加载:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:myapp-ws-context.xml</param-value> 
</context-param> 
<listener> 
<listener-class> 
    org.springframework.web.context.ContextLoaderListener 
</listener-class> 
</listener> 

但是你可以配置Spring对象的你的servlet/Web应用程序的范围,像multipartResolver等,使对象的范围明确,通过增强像下面您CXFServlet配置:

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

* 请大家注意,从您的网络应用环境中,可以访问从contextConfigLoc加载的上下文中的所有对象通货膨胀。 *