天易19----java-xfireWebservice和spring整合的简单实现

来源:互联网 发布:pla算法 纠正 编辑:程序博客网 时间:2024/06/02 13:12

声明:本人这里虽然整合了spring但没有建立连接池和jdbc链接数据库的配置,如果用到可自行配置


XFireXFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成,它使得Java开发人员终于可以获得和.Net开发人员一样的开发效率。

Webservice:Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。

一:1)在配置文件之前,先引进XFire1.2 CoreLibraries文件和XFire 1.2 HTTP Client Libraries两个文件和spring所需的jar包,请参照下面的目录结构图

web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 运行服务时用于加载applicationContext.xml和xfire-servlet.xml配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value></context-param><!-- 启动时加载SpringContextServlet --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>  org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- end spring配置 --><!-- begin xFire 配置 -->  <servlet>          <servlet-name>xfire</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    </servlet>       <servlet-mapping>        <servlet-name>xfire</servlet-name>       <url-pattern>*.ws</url-pattern>    </servlet-mapping><servlet>       <!-- 配合Spring容器中XFire一起工作的Servlet,必不可少-->       <servlet-name>xfireServlet</servlet-name>       <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>    </servlet><servlet-mapping>       <servlet-name>xfireServlet</servlet-name>       <!-- 在这个URI下开放Web Service服务 -->       <url-pattern>/service/*</url-pattern>    </servlet-mapping>    <!-- end XFire 配置 -->      <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
2)在WEB-INF目录下新建xfire-servlet.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-2.0.xsd"             default-autowire="byName"><!-- 引入XFire预配置信息 -->    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /><!-- 这一步必不可少,必须写 -->          <!-- 定义访问的url -->    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">       <property name="urlMap">                        <map>                 <!-- 对外提供的服务的名称 -->                            <entry key="/Service.ws">                                    <ref bean="service" />                          </entry>           </map>       </property>         </bean>              <!-- 使用XFire导出器 -->    <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">       <!-- 引用xfire.xml中定义的工厂 -->       <property name="serviceFactory" ref="xfire.serviceFactory" />       <!-- 引用xfire.xml中的xfire实例 -->       <property name="xfire" ref="xfire" />    </bean>    <bean id="service" parent="baseWebService">       <!-- 业务服务bean -->       <property name="serviceBean" ref="mywebservice" /><!-- 此处的mywebservice主要用于在applicationContext.xml配置文件中使用 -->       <!-- 业务服务bean的窄接口类 -->       <property name="serviceClass" value="com.service.support.IJavaWebService" />    </bean></beans>

3)applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- 程序入口实现类 分配具体调用哪个实现类,这里面主要注入在实现类里所声明的dao或其它对象 --><bean id="mywebservice" class="com.service.impl.JavaWebService">    </bean></beans>
二:新建一个接口和一个实现类,就是 配置文件中的IJavaWebService接口和JavaWebService实现类,目录结构如下图


1)接口代码:

package com.service.support;public interface IJavaWebService {public String getMessage(String name,int age,String email); }

2)实现类代码:

package com.service.impl;import com.service.support.IJavaWebService;public class JavaWebService implements IJavaWebService {public String getMessage(String name, int age, String email) {// TODO Auto-generated method stubreturn name+" "+age+" "+email;}}

三:测试前面的配置是否成功:

1)将新建的web project(这里我建的是JavaWebService工程)工程发布到tomcat服务器上

2)在浏览器地址栏里输入如下地址进行测试:http://192.168.0.104:8081/JavaWebService/Service.ws?wsdl(Ip地址换成自己本地地址),如果出现如下画面说明配置成功!


四:新建工程进行测试

1.工程目录结构如下:


2.新建一个接口类,这个接口类必须和前面webservice工程中定义的接口类中的方法一样,包括参数的个数类型还有返回值都要一样,代码如下:

package com.ceshi.support;public interface ICeShiService {public String getMessage(String name,int age,String email); }

3.工程的测试类代码如下:

package com.ceshi.test;import java.net.MalformedURLException;import org.codehaus.xfire.XFire;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;import com.ceshi.support.ICeShiService;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubService service = new ObjectServiceFactory().create(ICeShiService.class);//这个类为定义的接口类//XFire xFire = XFireFactory.newInstance().getXFire();XFireProxyFactory factory = new XFireProxyFactory(xFire);String url = "http://192.168.0.104:8081/JavaWebService/Service.ws";//这里是需要访问的webservice地址,即自己的本地地址和服务器端口号加上在xfire-servlet配置文件中设置的webservice对外提供的服务的名称try {ICeShiService ic = (ICeShiService) factory.create(service, url);String result=ic.getMessage("姓名:"+"王子虎"+" "+"年龄:",22," "+"E-mail:"+ "wangzihu@hotmail.com");System.out.println("result===========>>>"+result);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

四:运行main方法,如果出现如下画面,说明该工程调用webservice成功: