带有Spring的Apache CXF

时间:2011-09-25 07:51:27

标签: spring cxf

我在Spring上使用Apache CXF,请告诉我CXFServlet如何读取myapp-ws-context.xml

<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 个答案:

答案 0 :(得分:3)

您是否见过org.apache.cxf.transport.servlet.CXFServlet @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

请注意,org.springframework.web.context.WebApplicationContext.ROOT是一个Spring类,它尝试在名为{{1}}的servlet上下文属性中查找应用程序上下文。

答案 1 :(得分:0)

实际上你的类路径: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>

但是你可以配置你的Servlet / WebApp范围的Spring对象,比如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>

* 请注意,从您的Web应用程序上下文中,您可以访问从contextConfigLocation加载的上下文中的所有对象。 *