天天看点

在 Spring中集成CXF--->演示webservice服务的发布及调用

一.前言:

CXF的使用:主要是完成传统webService的增强

WebService主要作用:1.是服务的发布        2.是使用服务完成客户端。

框架和Spring集成的核心: 把框架的核心类(Service服务),交给Spring管理

二.功能实现:spring中集成CXF----->服务的发布

  第一步:创建web项目

  第二步:搭建spring环境

    2.1 导spring 的jar包(本节只导必须包/基础包且为3.0版本)

com.springsource.net.sf.cglib-2.1.3

com.springsource.org.aopalliance-1.0.0

com.springsource.org.apache.commons.logging-1.1.1

com.springsource.org.aspectj.tools-1.6.6.RELEASE

commons-dbcp-1.3

commons-pool-1.5.6

org.springframework.aop-3.0.5.RELEASE

org.springframework.asm-3.0.5.RELEASE

org.springframework.beans-3.0.5.RELEASE

org.springframework.context.support-3.0.5.RELEASE

org.springframework.context-3.0.5.RELEASE

org.springframework.core-3.0.5.RELEASE

org.springframework.expression-3.0.5.RELEASE

org.springframework.jdbc-3.0.5.RELEASE

org.springframework.orm-3.0.5.RELEASE

org.springframework.transaction-3.0.5.RELEASE

org.springframework.web-3.0.5.RELEASE

-----------------CXF的核心Jar包-------------------------

asm-3.3.1.jar

commons-logging-1.1.1.jar

cxf-2.6.2.jar

neethi-3.0.2.jar

wsdl4j-1.6.2.jar

xmlschema-core-2.0.3.jar

 2.2准备配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
 <!-- myDate  用于测试 -->
 <bean class="java.util.Date" id="myDate"></bean>
 <!--导包  -->
 <import resource="applicationContext-service.xml"/>
</beans>
           

 2.3进行测试-----采用Junit3的测试----SpringTest

public class SpringTest {
 /**
  * 目的:1. 测试Date 可以获取  2.判断是否是单例的
  * 步骤:  junit3测试 不能使用注解
  *   1.通过main进行测试  public static void main(String[] args)
  *   2.获取application
  */
 public static void main(String[] args) {
  //第一步  获取上下午配置文件
  ApplicationContext  aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
  //第二步:通过上下午获取beans
  Date bean = aContext.getBean(Date.class);
  //第三步;做测试
  System.out.println("bean="+bean);
  //第四步:测试 是不是 单例,思路通过 线程睡眠 测试是不是单例的
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  System.out.println("bean="+bean);
 }
}
           

第三步:让spring在web中一启动就初始化---->配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <!-- web项目一启动就初始化Spring 思路:通过配置Spring容器监听器来做-->
  <listener id="contextLoaderListener" >
  <!--contextLoaderListener  会默认到web-inf  下面去找applicationContext.xml
  配置文件,故而需要制定路径:解决方案
  通过context-param标签指定  -->
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置路径 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!--专门提供服务的servlet  -->
  <!--  集成CXFServlet  :完成后通过此地址在浏览器访问:  http://localhost:8080/ws-->
  <servlet>
   <servlet-name>CXFServlet</servlet-name>
   <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>CXFServlet</servlet-name>
   <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>
           

第四步:创建cxf-servlet.xml文件(位于:WEB-INF目录下面)

<?xml version="1.0" encoding="UTF-8"?>
<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/schemas/jaxws.xsd">
<!-- 第二种方式:默认地址(常用) 
 cxf 的发布条件(3个参数):1.地址   2.接口   3.实现类
 可增加拦截器
-->
  <jaxws:server id="hellowServer" address="/hi" >
     <jaxws:serviceBean>
     <!--引用已经存在的service  -->
      <ref bean="hellowServiceImpl"/>
  </jaxws:serviceBean>
  <!-- 添加拦截器:日志拦截器 -->
  <jaxws:inInterceptors>
   <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
  </jaxws:inInterceptors>
  <jaxws:outInterceptors>
   <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  </jaxws:outInterceptors>
  </jaxws:server>
</beans>
           

--------------处于规范的要求-----------

单独创建  applicationContext-service.xml文件与applicationContext.xml同目录

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
 <bean id="hellowServiceImpl" class="cn.gaoshan.springCXF.service.impl.HellowServiceImpl"/>
</beans>
           

第五步:在浏览器中输入:http://localhost:8080/ws  进行测试

三.功能实现:spring中集成CXF----->服务的发布

     第一步:新建一个web项目      第二步:导入CXF的资源包(略)-----与上面的一致           第三步:添加Spring配置文件------applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
	
	<import resource="applicationContext-cxf.xml"/>
</beans>
           

    第四步:添加cxf配置文件      

<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/schemas/jaxws.xsd">
	<!--cxf  调用 客户端:需要两个参数
		address="http://192.168.1.101:8080/ws/hi"  ---服务端地址
		serviceClass="cn.gaoshan.springCXF.test.IHellowService"----服务接口
		最后调用一个create  ,生成一个代理,放到spring容器中
	  -->
	<jaxws:client id="hellowService" address="http://192.168.1.101:8080/ws/hi"
		serviceClass="cn.gaoshan.springCXF.test.IHellowService">
		
	</jaxws:client>
</beans>
           

   第五步:找到wsdl文件地址,生成客户端代码        

在需要生成代码的根目录,执行命令:wsdl2java -p cn.gaoshan.springCXF.test  http://localhost:8080/ws/hi?wsdl

备注:易出现错误提示:

1.wsdl2java既不是内部命令也不是外部命令

    解决方案:重写配置cxf的环境变量

        新建CXF_HOME      C:\ProgramFiles(my)\apache-cxf-2.7.2

        在CLASSPATH          %CSF_HOME%\lib

        在PATH中添加           %CSF_HOME%\bin

2.错误提示:WSDLToJava Error: http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl [15,19]: undefined element declaration 's:schema'......

   解决方案:检查路径:wsdl2java -p cn.gaoshan.springCXF.test  (此处要间隔两个空格)http://localhost:8080/ws/hi?wsdl是否有错,

http://localhost:8080/ws/hi?wsdl必须是当前能访问的地址

第六步:进行测试---->SpringTest(两个版本)

xml版本

public class SpringTest {
	//调用客户端服务之----------xml版本
	public static void main(String[] args) {
		//第一步  获取上下午配置文件
		ApplicationContext  aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		//第二步:通过上下午获取本地接口的服务代理
		IHellowService hellowService = aContext.getBean(IHellowService.class);
		//第三步:调用服务
		System.out.println(hellowService.sayHellow("金顺鹏"));
	}
}
           
注解版本
public class SpringTest {
	//调用客户端服务之----------非xml版本
	public static void main(String[] args) {
		/**
		 * 流程:1.先创建JaxWsProxyFactoryBean  用于接收服务
		 * 		2.设置 服务的发布地址
		 * 		3.设置 服务的发布接口
		 * 		4.通过create  返回服务接口的实例
		 */
		JaxWsProxyFactoryBean Bean = new JaxWsProxyFactoryBean();
		Bean.setAddress("http://192.168.1.101:8080/ws/hi");
		Bean.setServiceClass(IHellowService.class);
		IHellowService hellowService = (IHellowService) Bean.create();
		System.out.println(hellowService.sayHellow("王振涛"));
	}
}
           
以上!