title: Apache CXF + Spring: Generating a Simple Client
body: <p>I've started learning the Apache CXF with Spring. First of all, I've tried to create a simple client/server model.</p>
  
  <p><em>The server-side is:</em>
  <strong>service.HelloWorld.java</strong></p>
  
  <pre><code>@WebService
  public interface HelloWorld {
    String sayHi(String text);
  }
  </code></pre>
  
  <p><strong>service.HelloWorldImpl.java</strong></p>
  
  <pre><code>@WebService(endpointInterface = "service.HelloWorld")
  public class HelloWorldImpl implements HelloWorld {
     public String sayHi(String text) {
       return "Hello, " + text;
     }
  }
  </code></pre>
  
  <p><em>The client-side is:</em>
  <strong>client.Client.java</strong>
      public class Client {</p>
  
  <pre><code>    public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new  String[] {"cxf-client-servlet.xml"});
            HelloWorld client = (HelloWorld) context.getBean("client");
            System.out.println(client.sayHi("Batman"));
      }
  }
  </code></pre>
  
  <p><strong>cxf-client-servlet.xml</strong></p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;bean id="client" class="service.HelloWorld" factory-bean="clientFactory" factory-method="create"/&gt;
  
  &lt;bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"&gt;
      &lt;property name="serviceClass" value="service.HelloWorld"/&gt;
      &lt;property name="address" value="http://localhost:8080/services/HelloWorld"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>The problem is: to make the client work I've had to add service.HelloWorld (package + interface) to the clients's project. I've heard that before using a service I need to generate a stub. So it's confusing for me. So that, what is the correct approach and what is the best practice (may be it is better to use some contract-first approach or suchlike)? Later, I want to add WS-Security, so I need a strong background =) </p>
  
  <p>Thanks in advance.</p>