当前位置: 代码迷 >> Web前端 >> Spring与xFire组合开发WebService
  详细解决方案

Spring与xFire组合开发WebService

热度:441   发布时间:2012-09-27 11:11:17.0
Spring与xFire结合开发WebService

spring xfire是一种比较简单的webservcie方式,下面的步骤描述一个例子。
基本流程是:
1.加载spring,xfire jar文件
2.web.xml加载applicationContext.xml,xfire
3.创建java服务工程,创建远程对外服务接口,实现类,model对象
4.在applicationContext.xml中部署服务
5.测试webservice

?

1.创建一个web工程,加入Maven,pom.xml代码见:

Xml代码 复制代码 收藏代码

?

<dependencies>
  <dependency>
      <groupId>org.codehaus.xfire</groupId>
      <artifactId>xfire-aegis</artifactId>
      <version>1.2.4</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.xfire</groupId>
      <artifactId>xfire-spring</artifactId>
      <version>1.2.4</version>
    </dependency>
    <dependency>
      <groupId>xalan</groupId>
      <artifactId>xalan</artifactId>
      <version>2.7.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>2.0</version>
    </dependency>
    </dependencies>


如果没有使用Maven,将如下jar文件加载到classpath下面:
activation-1.1.jar
aopalliance-1.0.jar
avalon-framework-4.1.3.jar
commons-attributes-api-2.1.jar
commons-beanutils-1.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.1.jar
jaxen-1.1-beta-9.jar
jdom-1.0.jar
junit-3.8.1.jar
log4j-1.2.12.jar
logkit-1.0.1.jar
mail-1.4.jar
qdox-1.5.jar
servlet-api-2.3.jar
spring-1.2.6.jar
spring-aop-2.0.jar
spring-beans-2.0.jar
spring-context-2.0.jar
spring-core-2.0.jar
spring-web-2.0.jar
stax-api-1.0.1.jar
stax-utils-snapshot-20040917.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.0.jar
xalan-2.7.0.jar
xbean-2.2.0.jar
xbean-spring-2.7.jar
xercesImpl-2.6.2.jar
xfire-aegis-1.2.4.jar
xfire-annotations-1.2.4.jar
xfire-core-1.2.4.jar
xfire-spring-1.2.4.jar
xfire-xmlbeans-1.2.4.jar
xml-apis-1.0.b2.jar
xmlParserAPIs-2.6.2.jar
XmlSchema-1.1.jar

?

2.编辑web.xml

Xml代码 复制代码 收藏代码

?

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
 </context-param>
 <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
 <servlet>
  <servlet-name>xfire</servlet-name>
  <servlet-class>
   org.codehaus.xfire.spring.XFireSpringServlet
  </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>xfire</servlet-name>
  <url-pattern>/service/*</url-pattern>
 </servlet-mapping>

?

3.创建java服务工程,在web工程中关联该java工程或将java服务工程打包jar放到web工程classpath下面。
服务对外接口:org.simpledev.spring.xfire.service

Java代码 复制代码 收藏代码

?

HelloService
 String sayHello(String username) throws RemoteException;
 User listUserById(int id) throws RemoteException;

?

接口实现类:org.simpledev.spring.xfire.service.impl

Java代码 复制代码 收藏代码

?

HelloServiceImpl implements HelloService
 public User listUserById(int id) throws RemoteException {
  System.out.println("server listUserById invoke");
  User user = new User(id,"纪纱","女",12,"日本都立海原高校");
  return user;
 }
 public String sayHello(String username) throws RemoteException {
  System.out.println("server sayHello invoke");
  return "你好 " + username;
 }

?

model对象
属性:id,username,sex,age,address
提供构造函数
每个属性提供getXXX(),setXXX()方法

?

4.编辑applicationContext.xml

Xml代码 复制代码 收藏代码
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> 
 <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">  
  <property name="serviceFactory" ref="xfire.serviceFactory" />  
  <property name="xfire" ref="xfire" /> 
 </bean> 
 <bean id="userWS" class="org.simpledev.spring.xfire.service.impl.HelloServiceImpl">
 </bean> 
 <bean id="userService" parent="baseWebService">  
  <property name="serviceBean" ref="userWS" />  
  <property name="serviceClass" value="org.simpledev.spring.xfire.service.HelloService" /> 
 </bean>

?

测试部署是否成功,发布web工程,在IE地址栏中输入: http://127.0.0.1:8080/springxfire/service/,出现链接后: http://127.0.0.1:8080/springxfire/service/HelloService?wsdl
表示部署成功

?

5.测试工程

Java代码 复制代码 收藏代码

?

public static void main(String[] args) {
  try {
   Service serviceModel = new ObjectServiceFactory().create(HelloService.class);
   HelloService service = (HelloService) new XFireProxyFactory().create(
     serviceModel,
     "http://127.0.0.1:8080/springxfire/service/HelloService");
   //方法一 测试
   String str = service.sayHello("世界");
   System.out.println(str);
   //方法二 测试
   User user = service.listUserById(1);
   System.out.println("id:"+user.getId());
   System.out.println("username:"+user.getUsername());
   System.out.println("sex:"+user.getSex());
   System.out.println("age:"+user.getAge());
   System.out.println("address:"+user.getAddress());
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (RemoteException e) {
   e.printStackTrace();
  }
 }


发现的问题:在实际项目中,
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>2.0</version>
</dependency>一些jar包与原来项目中的spring jar可能会产生冲突,可以将spring-web jar去掉或覆盖,如下:
(
aopalliance-1.0.jar
avalon-framework-4.1.3.jar
commons-logging-1.1.jar
log4j-1.2.12.jar
logkit-1.0.1.jar
servlet-api-2.3.jar
spring-aop-2.0.jar
spring-beans-2.0.jar
spring-context-2.0.jar
spring-core-2.0.jar
spring-web-2.0.jar
)
其次web.xml中注意加载:xfire,否则测试是否部署成功时,http://.../service/,其实请求的就是在web.xml中配置的servlet

  相关解决方案