user663724
user663724

Reputation:

Apache CXF with Spring

I am using Apache CXF with Spring , please tell me how the CXFServlet reads the 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>

Upvotes: 1

Views: 4519

Answers (2)

Bright
Bright

Reputation: 666

Actually your classpath:myapp-ws-context.xml is read by Spring, not CXF.

By adding below configuration in your web.xml, it would be read by Spring and the context would be loaded:

<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>

But you could configure your Servlet/WebApp scope of Spring objects, like multipartResolver etc., to make the objects' scopes clearly, by enhancing your CXFServlet configuration like below:

<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>

*Please take note that from your web-app context, you can access all objects in the context loaded from contextConfigLocation. *

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340933

Have you seen sources of org.apache.cxf.transport.servlet.CXFServlet (open source)?

Everything is more than explicit:

@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());
    }
}

Note that WebApplicationContextUtils is a Spring class that tries to find an application context in servlet context attribute named: org.springframework.web.context.WebApplicationContext.ROOT.

Upvotes: 3

Related Questions