cxf-Writing a service with Spring

译者:kaiwii

原文位置:

http://cxf.apache.org/docs/writing-a-service-with-spring.html

本例子带领你去开发你的第一个Spring service.从中,你将会学会以下的内容:

1、       cxf准备环境(set up build for cxf

2、       创建一个简单的AX-WS service

(这个例子对应CXF发行包里面的spring_http例子)

 

以下具体步骤:

1Setting up your build

打开你自己喜欢使用的IDE,然后创建一个project。首先,你需要将CXF所依赖的所有包添加到这个project中(这些包可以CXF的发行包里面的lib目录中找到)。如下所示:

commons-logging-1.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.7.1.jar (or Sun's JavaMail jar)
geronimo-servlet_3.0_spec-1.0.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.13.jar
jaxws-api-2.1.jar
neethi-3.0.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
stax2-api-3.1.1.jar
wsdl4j-1.6.2.jar
woodstox-core-asl-4.0.8.jar
xmlschema-core-2.0.jar
xml-resolver-1.2.jar

The Spring jars:

aopalliance-1.0.jar
spring-core-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar

And the CXF jar:

cxf-2.4.0.jar

2Writing your Service

首先,我们需要创建我们的service的接口。其中,有一个方法,名为“sayhello”,方法作用是,发送“Hello”来回应那些已经发送他们的名字过来的对象。如下所示:

package demo.spring;
 
import javax.jws.WebService;
 
@WebService
public interface HelloWorld {
    String sayHi(String text);
}

我们的实现类,如下所示:

译者:kaiwii

package demo.spring;
 
import javax.jws.WebService;
 
@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
 
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
}

实现类中的标签@WebService告诉CXF在创建WSDL的时候,这个实现类将会使用哪个接口。在这个例子中,就是我们的接口HelloWorld

 

 

3Declaring your server beans

CXF支持Spring 2.0的“nice XML“。为了支持JAX-WS,声明server beans,我们使用一个<jaxws:endpoint>bean来注册server终端。

我们通过在web-inf目录下建立一个"beans.xml"来声明一个终端bean,如下所示:

 

<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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
 
     <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>

如果你需要引用一个spring管理的bean,那么你可以这样子写:

<bean id="hello" class="demo.spring.HelloWorldImpl" />
 
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

bean使用到以下一些属性:

Id:spring容器中的id

Implementor:接口的实现类

如果你想用bean名而不是类名来作为implementor标签的值,你只需要在bean名之前加上前续“#”,例如,implementor="#myBean"

如果你想<jaxws:endpoint>标签更加复杂,比如加上嵌套的标签来将AX-WS Handlers 或者 CXF Interceptors添加上去。详情请参考:JAX-WS Configuration

4Setting up the Servlet

我们需要在web.xml中添加两样东西:

1、       Spring ContextLoaderLister:负责开启spring容器和加载我们的beans.xml文件。我们的文件路径可以通过context-param项来设置

2、       CXF Servlet

如下所示:

1.    <web-app>
2.          <context-param>
3.               <param-name>contextConfigLocation</param-name>
4.               <param-value>WEB-INF/beans.xml</param-value>
5.          </context-param>
6.     
7.          <listener>
8.               <listener-class>
9.                org.springframework.web.context.ContextLoaderListener
10.                                        </listener-class>
11.                                   </listener>
12.                              
13.                                   <servlet>
14.                                        <servlet-name>CXFServlet</servlet-name>
15.                                        <display-name>CXF Servlet</display-name>
16.                                        <servlet-class>
17.                                         org.apache.cxf.transport.servlet.CXFServlet
18.                                        </servlet-class>
19.                                        <load-on-startup>1</load-on-startup>
20.                                   </servlet>
21.                              
22.                                   <servlet-mapping>
23.                                        <servlet-name>CXFServlet</servlet-name>
24.                                        <url-pattern>/*</url-pattern>
25.                                   </servlet-mapping>
26.                             </web-app>

需要注意的是,你为你的终端bean指定的地址必须要是你的servlet正在侦听的地址之一。例如,如果我的servlet注册在"/some-services/*"下,但是我的地址是"/more-services/HelloWorld",那么CXF将不能正常收到请求。

5Create a Client (Easy Way)

如同<jaxws:endpoint>可以用在服务器端,<jaxws:client>可以用在客户端。对于<jaxws:client>标签,当赋给它一个bean名,一个service的接口,和serviceurl,框架将会创建一个bean,并且这个bean将会使用上面指定的名字以及实现指定的service接口,并且会调用远程的soap service,如下图所示:

<?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>

现在,你可以将“helloclient”注入到其他spring bean中,或者通过以下的代码,手工地查找这个bean:

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

5Create a Client (More Manual Way)

CXF包括一个JaxWsProxyFactory bean,这个bean负责通过service interface创建一个client。你只需要告诉框架,你的service class是什么(本例就是HelloWorld 接口)以及serviceurl。之后,你就可以通过JaxWsProxyFactorycreate()方法创建一个client

译者:kaiwii

下面就是一个例子:

<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.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>

如果你希望访问你的client,你只需要从Spring容器中将它检索出来(或者,更加简单,你可以用Spring将它注入到你的应用中去)

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

//译者:kaiwii

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

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.

译者:kaiwii

阅读更多
个人分类: J2EE spring apache cxf
上一篇cxf基本功能
下一篇什么是soap
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭