...
This example corresponds to the java_first_spring_http support example in the CXF distribution.
...
Wiki Markup |
---|
{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/service/HelloWorld.java?revision=HEAD} |
...
Wiki Markup |
---|
{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/service/HelloWorldImpl.java?revision=HEAD} |
...
CXF contains support for "nice XML" within Spring 2.0. For the JAX-WS side of things, we have a <jaxws:endpoint> bean which sets up a server side endpoint.
Lets create a "beanscxf-servlet.xml" file in our WEB-INF directory which declares an endpoint bean:
Wiki Markup |
---|
{snippet:id=beans|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/webapp/WEB-INF/beanscxf-servlet.xml?revision=HEAD} |
If you want to reference a spring managed-bean, you can write like this:
...
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
Since we're relying on the default "cxf-servlet.xml" file the default web.xml referenced by many samples can be used.
Alternatively, for arbitrarily named configuration files such as beans.xml, application-context.xml, etc. we can add the following elementsWe'll need to add two things to our web.xml file:
- the Spring
ContextLoaderLister
. This starts Spring and loads our beans.xml explicitly loads the configuration file. We can specify where our file is via acontext-param
element.the CXF Servlet {snippet:id=webxml|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/webapp/WEB-INF/web.xml?revision=HEAD}Wiki Markup
An example:
Code Block | ||||
---|---|---|---|---|
| ||||
<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>
</web-app>
|
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.
...
Code Block | ||||
---|---|---|---|---|
| ||||
ApplicationContext context = ...; // your Spring ApplicationContext HellWorldHelloWorld client = (HelloWorld) context.getBean("client"); |
...