You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Supplying a Configuration file to CXF

CXF can discover XML configuration files which you have written. The default location that CXF will look for a configuration for is "/cxf.xml" on the classpath. If you wish to override this location, you can specify a command line property: -Dcxf.config.file=some_other_config.xml.

A CXF configuration file is really a Spring configuration file, so all configuration files will start with the following:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- Configuration goes here! -->

</beans>

If you are new to Spring or do not desire to learn more about it, don't worry, you won't have to. The only piece of Spring that you will see is the <beans> element outlined above. Simply create this file, place it on your classpath, and add the configuration for a component you wish to configure (see below).

What can I configure and how do I do it?

If you want to change CXF's default behaviour, enable specific functionality or fine tune a component's behaviour, you can in most cases do so without writing a single line of code, simply by supplying a Spring configuration file.
In some cases it also possible to achieve the same end by extending your wsdl contract: you can add CXF specific extension elements to the wsdl:port element and in that way fine tune the behaviour of the specified transport. Or you can use WS-Policy to express the fact that your application uses WS-Addressing.
Using Spring configuration files however is the most versatile way to achieve a specific goal: you can use it to

  1. Enable functionality via simple constructs called features.
  2. Set properties of runtime components by referring to these runtime components using either plain Spring bean elements, or, more conveniently, using CXF custom beans elements.
  3. Modify the actual composition of the runtime (change the way the runtime is wired up).

The following examples show the what the Spring configuration would look like if you wanted to enable the logging of inbound and outbound messages and faults.

Enabling message logging using plain Spring bean elements

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    <bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
	
    <bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl">
        <property name="inInterceptors">
            <list>
                <ref bean="logInbound"/>
            </list>
        </property>
        <property name="outInterceptors">
            <list>
                <ref bean="logOutbound"/>
            </list>
        </property>
        <property name="outFaultInterceptors">
            <list>
                <ref bean="logOutbound"/>
            </list>
        </property>
    </bean> 
</beans>

In this example, you specify that the CXF bus is implemented by class org.apache.cxf.bus.CXFBusImpl, and that it's id is "cxf". This is the default, but you have to re-iterate the fact if you want the bus to contribute the logging interceptors to the outbound and inbound interceptor chain for all client and server endpoints. You can avoid this duplication by using the next form of configuration:

Enabling message logging using custom CXF bean elements

beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    <bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

    <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="logInbound"/>
        </cxf:inInterceptors>
        <cxf:outInterceptors>
            <ref bean="logOutbound"/>
        </cxf:outInterceptors>
        <cxf:inFaultInterceptors>
            <ref bean="logOutbound"/>
        </cxf:inFaultInterceptors>
    </cxf:bus> 
</beans>

Here, there is no need to specify the implementation class of the bus - nor the fact that the inInterceptors, outInterceptors and inFautlInterceptors child elements are of type list. All of this information is embedded in the underlying schema and the bean definition parser that's invoked for <cxf:bus> elemens. Note that you have to specify the location of this schema in the schemaLocation attribute of the <beans> element so that Spring can validate the configuration file. But it gets even simpler in the next example:

Enabling message logging using the Logging feature

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <cxf:bus>
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
    </cxf:bus> 
</beans>

The following sections explain which of the above mechanisms you can use to enable and/or configure the various components in CXF, and what your configuration options are:

Embedding CXF inside Spring

You can also embed CXF within an existing Spring application. Since all XML configuration files are Spring xml files, the two approaches should be equivalent.

CXF includes Spring configuration files which configure the various CXF modules. You will want to import these into your application. Here is an example that imports the core CXF components, the SOAP binding, and the servlet transport:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  ...
</beans>

To include all the CXF modules you can use a wildcard expression:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath*:META-INF/cxf/cxf-extension-*.xml" />
  ...
</beans>

The only module this won't include is the servlet transport. If you want to include the servlet transport you must specify it specifically. This is because it will tell CXF to no longer use the standalone HTTP transport, and you may not always want to do this.

Advanced Configuration

If you are writing your own component for CXF, please see Configuration for Developers page.

  • No labels