Skip to main content

Configuring a Spring Client (Option 2)

Information noteNote: Building a Client using this configuration is only applicable for those wishing to inject a Client into their Spring ApplicationContext.

This approach requires more explicit Spring bean configuration than the previous option, and may require more configuration data depending on which features are used. To configure a client this way, you'll need to declare a proxy factory bean and also a client bean which is created by that proxy factory. Here is an example:

<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/schemas/jaxws.xsd">

   <bean id="proxyFactory" 
      class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
      <property name="serviceClass" value="demo.spring.HelloWorld"/>
      <property name="address" value="http://localhost:9002/HelloWorld"/>
   </bean>

   <bean id="client" class="demo.spring.HelloWorld"
      factory-bean="proxyFactory" factory-method="create"/>

</beans>

The JaxWsProxyFactoryBean in this case takes two properties. The service class, which is the interface of the Client proxy you wish to create. The address is the address of the service you wish to call.

The second bean definition is for the client. In this case it implements the HelloWorld interface and is created by the proxyFactory <bean> by calling the create() method. You can then reference this "client" bean and inject it anywhere into your application. Here is an example of a very simple Java class which accesses the client bean:

include org.springframework.context.support.ClassPathXmlApplicationContext;

public final class HelloWorldClient {

   private HelloWorldClient() {}

   public static void main(String args[]) throws Exception {
      ClassPathXmlApplicationContext context =
         new ClassPathXmlApplicationContext(
            new String[]{"my/path/to/client-beans.xml"});

      HelloWorld client = (HelloWorld)context.getBean("client");

      String response = client.sayHi("Dan");
      System.out.println("Response: " + response);
      System.exit(0);
   }
}

The JaxWsProxyFactoryBean supports many other properties:

Name

Description

clientFactoryBean

The ClientFactoryBean used in construction of this proxy.

password

The password which the transport should use.

username

The username which the transport should use.

wsdlURL

The wsdl URL the client should use to configure itself.

wsdlLocation

Appears to be the same as wsdlURL?

serviceName

The name of the service to invoke, if this address/WSDL hosts several. It maps to the wsdl:service@name. In the format of "ns:SERVICE_NAME" where ns is a namespace prefix valid at this scope.

endpointName

The name of the endpoint to invoke, if this address/WSDL hosts several. It maps to the wsdl:port@name. In the format of "ns:ENDPOINT_NAME" where ns is a namespace prefix valid at this scope.

inInterceptors

The incoming interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor

inFaultInterceptors

The incoming fault interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor

outInterceptors

The outgoing interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor

outFaultInterceptors

The outgoing fault interceptors for this endpoint. A list of <bean> or <ref> elements. Each should implement org.apache.cxf.interceptor.Interceptor or org.apache.cxf.phase.PhaseInterceptor

features

The features that hold the interceptors for this endpoint. A list of <bean> or <ref> elements

handlers

A list of <bean> or <ref> elements pointing to JAX-WS handler classes to be used for this client. Each should implement javax.xml.ws.handler.Handler or javax.xml.ws.handler.soap.SOAPHandler . These are more portable than CXF interceptors, but may cause the full message to be loaded in as a DOM (slower for large messages).

bindingConfig

 

bindingId

The URI, or ID, of the message binding for the endpoint to use. For SOAP the binding URI(ID) is specified by the JAX-WS specification. For other message bindings the URI is the namespace of the WSDL extensions used to specify the binding.

bus

A reference to a CXF bus bean. Must be provided if, for example, handlers are used. May require additional Spring context imports (e.g. to bring in the default CXF bus bean).

conduitSelector

 

dataBinding

Which DataBinding to use in the endpoint. This can be supplied using the Spring <bean class="MyDataBinding"/> syntax.

properties

A properties map which should be supplied to the JAX-WS endpoint.

serviceFactory

 

Using some of the properties will require additional configuration in the Spring context. For instance, using JAX-WS handlers requires that you explicitly import several CXF Spring configurations, and assign the "bus" property of the JaxWsProxyFactory bean like this:

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

<bean id="clientFactory" 
   class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
   <property name="serviceClass" value="demo.spring.HelloWorld"/>
   <property name="address" value="http://localhost:9002/HelloWorld"/>
   <property name="bus" ref="cxf" />
</bean>

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!