`

WebService应用之CXF集成spring详解

阅读更多
一. CXF与spring集成
1.简介:
CXF是基于JAX-WS实现的,JAX-WS规范是一组XML web services的JAVA API,它使用户无需编写复杂的SOAP ENV,WSDL。在 JAX-WS中,一个远程调用可以转换为一个基于XML的协议例如SOAP。在使用JAX-WS过程中,开发者不需要编写任何生成和处理SOAP消息的代码。JAX-WS的运行时实现会将这些API的调用转换成为对于SOAP消息。
在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI (service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。
在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。
2.实现过程:
a) 为CXF设置编译和开发环境
在http://cxf.apache.org/download.html 下载相应的CXF包,/lib目录下的jar 文件引入工程
b) 新建工程,cxfspring,包含WebRoot/WEB-INF目录
c) 编写服务
首先写一个服务接口,例子中的HelloWorld.java。我们要注意的是这个例子使用了JSR181规范中的声明“@WebService”。
package demo.spring;
 
import javax.jws.WebService;
 
@WebService
public interface HelloWorld {
    String sayHi(String text);
}

下一步实现这个服务接口,例子中的HelloWorldImpl.java。这个例子代码中的“@WebService”标签只包含一个endpointInterface的属性,这个属性让CXF知道根据哪个接口生成WSDL文件。这里有点和我们第一个例子不同,没有包含ServiceName属性。这是因为这个属性会在Spring的配置文件中声明,请参考下面的beans.xml这个配置文件。
package demo.spring;
 
import javax.jws.WebService;
/**
 * javaDoc:http://download.oracle.com/javase/6/docs/api/overview-summary.html
 * 
 * @WebService
 * - <wsdl:service name="HelloWorldImplService">
 * - <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding"  name="HelloWorldImplServicePort">
 * 	 - <soap:address location="http://localhost:8081/TestWebservice/HelloWorld" />
 * - </wsdl:port>
 * - </wsdl:service> 
 * targetNamespace: 命名空间,对应tns的定义;没写,默认是“http://”+包(package)反过来写。
 * 					加了这个属性,则参数的定义会用<wsdl:import location="http://localhost:8081/TestWebservice/HelloWorldService?wsdl=HelloWorldService.wsdl" ..>,
 * 					没加,参数定义就写在同一个wsdl文档
 * serviceName:对应wsdl文档,对应:<wsdl:service name="HelloWorldServieImplService">的name;不写,默认是"类名+Service";
 * portName:对应<wsdl:port binding="tns:HelloWorldServieImplServiceSoapBinding" name="HelloWorldServieImplPort">的name;不写,默认是"类名+ServicePort";
 * wsdlLocation: 对应映射的wsdl的物理地址
 * endpointInterface: 实现的接口
 * 
 * @Features:调用时,将请求和响应的SOAP信息打印出来
 */ 

@WebService(name="HelloWorldImpl",
//			targetNamespace="http://spring.demo",
			serviceName="HelloWorldImplService",
			portName="HelloWorldImplServicePort",
//			wsdlLocation = "file:/e:/healthEventRegistration.wsdl",
			endpointInterface="demo.spring.HelloWorld")
//@Features(features = "org.apache.cxf.feature.LoggingFeature")
public class HelloWorldImpl implements HelloWorld {
 
    public String sayHi(String text) {
        return "Hello " + text;
    }
}

d) 在spring中声明服务的Bean
CXF中包含对Spring2.0很好的支持。对于JAX-WS这一类配置,我们有<jaxws:endpoint>bean作为服务端节点的配置说明。
创建一个”beans.xml”文件在我们项目的WEB-INF路径下,注意这个文件的例子可以在“D:\cxf\samples\java_first_spring_support\”中找到。
<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>

<jaxws:endpoint>有三个属性id, implementor和address。
“id”指定这个Bean在Spring上下文中唯一的标识。
“implementor”指定了这个Web Service的实现类。
“address”指定了服务在Web服务器上发布的地址。这个地址可以包含ip和端口的完整地址,也可以是只包含相对路径的地址。

e) 配置web.xml
配置的过程中需要在web.xml中添加如下两项:
第一个是Spring的ContextLoaderListerp类会在启动时加载上面配置beans.xml文件。我们需要设定context-param节点;第二个是增加一个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>

这里需要注意的是配置的endpoint的地址(address)属性必须和在Beans.xml中定义的一致。尽管例子中没有在HelloWorldImpl.java中声明“address“属性,也需要在实际的配置中有所注意。
如果你在客户端代码要使用这个bean,代码会非常简单,例子如下
ApplicationContext context = ...; // your Spring ApplicationContext
HellWorld client = (HelloWorld) context.getBean("client");


如果朋友们觉得这篇文章对您有用,而且您需要茶叶茶具和零食之类的东东,请大家到下面这家店铺购买“品润茶业”,做批发的,价格超便宜,希望大家多多支持!
地址:http://prtea.taobao.com
请转载的朋友,把以上文字也带上,珍惜别人的劳动果实,谢谢!



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics