WebService与Spring整合

WebService体系之——与spring的整合

 

        摘要:好处意义什么的不再赘述、前面讲的东西也都是对webservice的认识、知道它基本的实现步骤、学习最终的目的就是应用、而spring在项目中的出镜率已经爆表、本篇笔记就是两者的整合的过程。

 

一:简介

 

        spring与webservice的整合很简单、如过对spring有所了解的话、不难猜想两者是如何结合的、我们使用spring的目的就是看重他的依赖注入功能、可以为我们通过配置文件自动生成bean、同时可以将我们指定的属性设置好、那么两者的整合肯定是通过配置spring的配置文件、让spring为我们生成项目运行中需要使用的关于webservice的bean、并且有我们想要的一切!

        1、新建web项目。

        2、引入jar包。

        3、修改web.xml。

        4、创建服务端webservice接口。

        5、实现服务端webservice接口实现类。

        6、新建spring的配置文件

        7、将项目发布到Tomcat服务器中、并启动。

        8、访问地址:有结果则说明webservice发布成功。

        9、新建测试类测试webservice发布的接口。

 

二:同一项目下具体的实现步骤

 

        1、创建web项目。右键。。新建。。。

        2、jar包的引入:前面提到过、下载下来的jar包中有一部分是spring的jar、当时没有过多提及、在这里就自然而然的想到是两者整合需要的jar包、我们直接将lib文件夹拷贝到项目的WEB-INFO下面即可。

        3、web.xml的配置:主要配置spring监听和配置文件加载位置以及CXF的配置。具体代码:


[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.   
  10.     <context-param>  
  11.         <param-name>contextConfigLocation</param-name>  
  12.         <param-value>classpath*:applicationContext-server.xml</param-value>  
  13.     </context-param>  
  14.   
  15.     <!-- Spring 配置 -->  
  16.     <listener>  
  17.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  18.     </listener>  
  19.     <listener>  
  20.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  21.     </listener>  
  22.       
  23.     <!-- WebServices设置 -->    
  24.     <servlet>    
  25.         <servlet-name>CXFServices</servlet-name>    
  26.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
  27.         <load-on-startup>0</load-on-startup>    
  28.     </servlet>    
  29.     <servlet-mapping>    
  30.         <servlet-name>CXFServices</servlet-name>    
  31.         <url-pattern>/services/*</url-pattern>    
  32.     </servlet-mapping>    
  33. </web-app>  

        4、创建服务端webservice接口——HelloService:


[java]  view plain  copy
  1. package com.chy.ws.service;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. @WebService  
  6. public interface HelloService {  
  7.     public String sayHello(String name);  
  8. }  

        5、实现服务端webservice接口实现类——HelloServiceImpl:


[java]  view plain  copy
  1. package com.chy.ws.service;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. @WebService(endpointInterface="com.chy.ws.service.HelloServiceImpl")    
  6. public class HelloServiceImpl implements HelloService {  
  7.   
  8.     public String sayHello(String name) {  
  9.         return "hello " + name;  
  10.     }  
  11. }  

        6、在web.xml中指定的spring配置文件的加载位置:src下新建applicationContext-server.xml:


[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans  xmlns="http://www.springframework.org/schema/beans"    
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.         xmlns:jaxws="http://cxf.apache.org/jaxws"    
  5.         xsi:schemaLocation="      
  6.             http://www.springframework.org/schema/beans       
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      
  8.             http://cxf.apache.org/jaxws      
  9.             http://cxf.apache.org/schemas/jaxws.xsd">    
  10.               
  11.     <!-- Import apache CXF bean definition 固定-->    
  12.     <import resource="classpath:META-INF/cxf/cxf.xml" />    
  13.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    
  14.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    
  15.         
  16.     <!-- services接口配置 -->    
  17.     <bean id="helloServicesBean" class="com.chy.ws.service.HelloServiceImpl" />    
  18.       
  19.     <!-- CXF 配置WebServices的服务名及访问地址 -->    
  20.     <jaxws:server id="helloService" address="/HelloService" serviceClass="com.chy.ws.service.HelloService">    
  21.             <jaxws:serviceBean>    
  22.                 <ref bean="helloServicesBean"/>    
  23.             </jaxws:serviceBean>    
  24.     </jaxws:server>    
  25. </beans>    

        7、将项目发布到Tomcat服务器中、并启动。

        8、访问地址:http://localhost:8080/services  若有结果则服务发布成功。

        9、新建测试类——HelloServiceClient代码:


[java]  view plain  copy
  1. package com.chy.ws.service.client;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.   
  5. import com.chy.ws.service.HelloService;  
  6.   
  7. public class HelloServiceClient {  
  8.     public static void main(String[] args) {  
  9.         invokByJava();  
  10.     }  
  11.       
  12.     public static void invokByJava(){  
  13.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  14.         factory.setServiceClass(HelloService.class);  
  15.         factory.setAddress("http://localhost:8080/webservice_spring_server/services/HelloService");  
  16.         HelloService helloService = (HelloService)factory.create();  
  17.         System.out.println(helloService.sayHello("andyChen"));  
  18.     }  
  19. }  

 

三:不同项目下客户端具体的实现步骤

 

        1、实现服务端。

        2、创建web项目、引入jar包、和前面一样。

        3、配置web.xml——主要配置spring监听、配置文件的加载路径。

        4、创建与服务器端一样的接口(注意名称、包名也要一样、可以直接拷贝)——HelloService代码与服务端相同。

        5、spring配置文件applicationContext-client.xml:


[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans  xmlns="http://www.springframework.org/schema/beans"    
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.         xmlns:jaxws="http://cxf.apache.org/jaxws"    
  5.         xsi:schemaLocation="      
  6.             http://www.springframework.org/schema/beans       
  7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      
  8.             http://cxf.apache.org/jaxws      
  9.             http://cxf.apache.org/schemas/jaxws.xsd">    
  10.     <!-- Import apache CXF bean definition -->    
  11.     <import resource="classpath:META-INF/cxf/cxf.xml" />    
  12.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    
  13.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    
  14.         
  15.     <!-- CXF webservices 客户端配置 -->    
  16.     <jaxws:client id="helloClient" serviceClass="com.chy.ws.service.HelloService"     
  17.             address="http://localhost:8080/webservice_spring_server/services/HelloService">    
  18.     </jaxws:client>    
  19. </beans>    


        6、发布、启动。

        7、新建测试类:此时我们可以有两种选择:一种是使用原始的方式获取服务端、另一种是使用spring获取服务端——HelloServiceClient:

 

[java]  view plain  copy
  1. package com.chy.ws.service.test;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.chy.ws.service.HelloService;  
  8.   
  9. public class HelloServiceClient {  
  10.     public static void main(String[] args) {  
  11.         invokByJava();  
  12.     }  
  13.       
  14.     /** 
  15.      * use spring's application context method to obtain web service. 
  16.      */  
  17.     public static  void invokBySpring(){  
  18.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  19.         HelloService helloService = context.getBean("helloClient", HelloService.class);  
  20.         System.out.println(helloService.sayHello("andyChen"));  
  21.     }  
  22.       
  23.     /** 
  24.      * use original method to obtain web service. 
  25.      */  
  26.     public static void invokByJava(){  
  27.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  28.         factory.setServiceClass(HelloService.class);  
  29.         factory.setAddress("http://localhost:8080/webservice_spring_server/services/HelloService");  
  30.         HelloService helloService = (HelloService)factory.create();  
  31.         System.out.println(helloService.sayHello("andyChen"));  
  32.     }  
  33. }  

四:补充

 

        完整项目图:


                

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值