引自官方网站说明: http://cwiki.apache.org/CXF20DOC/writing-a-service-with-spring.html
This example will lead you through creating your first service with Spring. You'll learn how to:
- Set up your build for CXF
- Writing a simple JAX-WS service
- Set up the HTTP transport
This example corresponds to the spring_http example in the CXF distribution.
Setting up your build
Open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory.
commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
The Spring jars:
aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar
And the CXF jar:
Writing your Service
First we'll write our service interface. It will have one operation called "sayHello" which says "Hello" to whoever submits their name.
package demo.spring;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
Our implementation will then look like this:
package demo.spring;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello " + text;
}
}
The @WebService annotation on the implementation class lets CXF know which interface to use when creating WSDL. In this case its simply our HelloWorld interface.
Declaring your server beans
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 "beans.xml" file in our WEB-INF directory which declares an endpoint bean:
<beans xmlns="http:
xmlns:xsi="http:
xmlns:jaxws="http:
xsi:schemaLocation="
http:http:
<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" />
<jaxws:endpoint
id="helloWorld"
implementor="demo.spring.HelloWorldImpl"
address="/HelloWorld" />
</beans>
If you want to reference a spring managed-bean, you can write like this:
<bean id="hello" class="demo.spring.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
The bean uses the following properties:
- id specifies the id of the bean in the Spring context.
- implementor specifies the implementation class.
- address specifies the location the service will be hosted. This should just be a related path. This is because CXF can't know the war name and the servlet container's listening port, CXF will update the endpoint address with the request url at the runtime.
To provide a bean name instead of a classname as an implementor, simply supply the bean-name prepended with "#", e.g. implementor="#myBean".
Setting up the Servlet
We'll need to add two things to our web.xml file:
- the Spring ContextLoaderLister. This starts Spring and loads our beans.xml file. We can specify where our file is via a context-param element.
- the CXF Servlet
<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>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</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.
Create a Client
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.
Here's 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/schema/jaxws.xsd">
<bean id="client" class="demo.spring.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld"/>
<property name="address" value="http://localhost:9002/HelloWorld"/>
</bean>
</beans>
If you were going to access your client you could now simply pull it out of the Spring context (or better yet, inject it into your application using Spring!):
ApplicationContext context = ...; HellWorld client = (HelloWorld) context.getBean("client");
client code at
http://svn.apache.org/repos/asf/incubator/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/demo/spring/client/Client.java
分享到:
相关推荐
为了解决wsdl解析 生成对应的Java类对象 本网址可以直接点击下载
Apache CXF 框架是一个比较有前途的开源 Web Services 框架,也是构建 SOA 架构应用的利器。本书采用案例源码和解说形式全面介绍 Apache CXF 框架的功能。 本书共 15 章,大致分为三个部分。第一部分介绍关于 SOA 和...
Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且...
2013版的 <基于Apache CXF构建SOA应用> 源码 Apache CXF是一个开放源码的Web服务框架,提供了一个易于使用,用于开发Web Services标准为基础的编程模型。本书主要介绍Apache CXF在构建SOA架构各个方面的应用说明和...
apache cxf 2.1.3 的用户手册
apache CXF 2.0.9源代码....
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。
Apache CXF Web Service Development
Apache CXF Web Service Development Developing+Web+Services+with+Apache+CXF+and+Axis2+(3rd+Edition)
apache cxf 一个helloworld的例子
apache cxf 3.1.8 java web service 开源框架
webserviceApache CXF java springboot利用Apache CXF创建webserice接口 Apache CXF 核心架构是以BUS为核心,整合其他组件。 * Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的...
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且...
Apache CXF Web Service Development 源码
java通过cxf实现webservice所需jar包。java通过cxf实现webservice所需jar包。
Apache cxf 与spring集成