Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you want to reference a spring managed-bean, you can write like this:

Code Block
xml
xml
  <bean id="hello" class="demo.spring.HelloWorldImpl" />

  <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

...

To provide a bean name instead of a classname as an implementor, simply supply the bean-name prepended with "#", e.g. implementor="#myBean".

You can also do more sophisticated things with the <jaxws:endpoint> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the service. For more on this see JAX-WS Configuration.

Setting up the Servlet

We'll need to add two things to our web.xml file:

...

It is important to note that the address that you chose for your endpoint bean must be one your servlet listens on. For instance, if my Servlet was register for "/some-services/*" but my address was "/more-services/HelloWorld", there is no way CXF could receive a request.

Create a Client (Easy Way)

Just like the <jaxws:endpoint> used on the server side, there is a <jaxws:client> that can be used on the client side. You'll give it a bean name, the service interface, and the service URL, and it will create a bean with the specified name, implementing the service interface, and invoking the remote SOAP service under the covers:

Code Block
xml
xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <jaxws:client id="helloClient"
                  serviceClass="demo.spring.HelloWorld"
                  address="http://localhost:9002/HelloWorld" />
</beans>

You can now inject that "helloClient" bean into any other Spring bean, or look it up from the Spring application context manually with code like this:

Code Block
java
java

ApplicationContext context = ...; // your Spring ApplicationContext
HellWorld client = (HelloWorld) context.getBean("helloClient");

You can also do more sophisticated things with the <jaxws:client> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the client. For more on this see JAX-WS Configuration.

Create a Client (More Manual Way)

CXF includes a JaxWsProxyFactory bean which create a client for you from your service interface. You simply need to tell it what your service class is (the HelloWorld interface in this case) and the URL of your service. You can then create a client bean via the JaxWsProxyFactory bean by calling it's create() method.

...

client code at http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/demo/spring/client/Client.java

Note

Some usage scenarios will require more extensive configuration (and this is not the case with the <jaxws:client> syntax described above). For more information, see JAX-WS Configuration.

Advanced Steps

For more information on using Spring you may want to read the Configuration and Spring sections of the User's Guide.