Language/JSP

Configuring JAX-RS services in container with Spring configuration file.

적외선 2013. 7. 10. 13:07

web.xml

In web.xml one needs to register one or more CXFServlet(s) and link to an application context configuration.

Using Spring ContextLoaderListener

<?xml version="1.0" encoding="ISO-8859-1"?>
 
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>
 
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
 
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-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>

The application context configuration is shared between all the CXFServlets

Using CXFServlet init parameters

<?xml version="1.0" encoding="ISO-8859-1"?>
 
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<web-app>
    <servlet>
        <servlet-name>CXFServlet1</servlet-name>
        <display-name>CXF Servlet1</display-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
                <init-param>
                   <param-name>config-location</param-name>
                   <param-value>/WEB-INF/beans1.xml</param-value>
                </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
        <servlet>
        <servlet-name>CXFServlet2</servlet-name>
        <display-name>CXF Servlet2</display-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
                <init-param>
                   <param-name>config-location</param-name>
                   <param-value>/WEB-INF/beans2.xml</param-value>
                </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
 
    <servlet-mapping>
        <servlet-name>CXFServlet1</servlet-name>
        <url-pattern>/1/*</url-pattern>
    </servlet-mapping>
 
        <servlet-mapping>
        <servlet-name>CXFServlet2</servlet-name>
        <url-pattern>/2/*</url-pattern>
    </servlet-mapping>
</web-app>

Each CXFServlet can get a unique application context configuration. Note, no Spring ContextLoaderListener is registered in web.xml in this case.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
  xsi:schemaLocation="
 
  <!-- do not use import statements if CXFServlet init parameters link to this beans.xml -->
 
  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 
  <jaxrs:server id="customerService" address="/service1">
    <jaxrs:serviceBeans>
      <ref bean="customerBean" />
    </jaxrs:serviceBeans>
  </jaxrs:server>
 
  <bean id="customerBean" class="demo.jaxrs.server.CustomerService" />
</beans>

In the above configuration all resources will be configured as singletons, see below for information on creating per-request resources.


출처 - http://cxf.apache.org/docs/jaxrs-services-configuration.html