`
human_zx
  • 浏览: 63601 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

xfire与spring结合的几种方式-

阅读更多

接着我上一篇继续学习。

根据org.codehaus.xfire.spring.XFireSpringServlet和org.codehaus.xfire.spring.ServiceBean配置。

1,创建web 动态工程XFireService,倒入包。

创建接口类,我这个接口只有一个方法。

 

package com.zx.xfiretext.webserver;

import com.zx.xfiretext.domain.User;   
public interface UserService {   
    public User queryUserByAccoutId(String accountId);   
  
} 
 

 

 创建接口的实现类:

package com.zx.xfiretext.webserver;

import java.util.Date;    
import org.apache.commons.logging.Log;    
import org.apache.commons.logging.LogFactory;   
  
import com.zx.xfiretext.domain.User;   
import com.zx.xfiretext.webserver.UserService;   
  
public class UserServiceImpl  implements UserService{   
    private static final Log log = LogFactory.getLog(UserServiceImpl.class);   
       
    public User queryUserByAccoutId(String accountId){    
        log.debug("accountId=" + accountId);    
        User user = new User();    
        user.setAccountId("testAccount");    
        user.setLastLogin(new Date());    
        user.setUserName("测试用户");    
        user.setUserId(new Long(123L));   
        return user;    
        }   
} 
 

 

 创建实体类:

 

package com.zx.xfiretext.domain;

import java.io.Serializable;    
import java.util.Date;

public class User implements Serializable{
	 private static final long serialVersionUID = 6517808321041980976L; private Long userId;    
	 private String accountId;   
	 private String userName;    
	 private Date lastLogin;    
	 public String getAccountId()    
	 {   
	     return accountId;    
	 }    
	 public void setAccountId(String accountId)    
	 {    
	     this.accountId = accountId;    
	 }    
	 public Date getLastLogin()    
	 {    
	     return lastLogin;    
	 }    
	 public void setLastLogin(Date lastLogin)    
	 {    
	     this.lastLogin = lastLogin;    
	 }    
	 public Long getUserId()    
	 {    
	     return userId;    
	 }    
	 public void setUserId(Long userId)    
	 {   
	     this.userId = userId;    
	 }    
	 public String getUserName()    
	 {    
	     return userName;    
	 }    
	 public void setUserName(String userName)    
	 {    
	     this.userName = userName;    
	 }  
}
 

   在WEB-INF先新建applicationContext.xml文件,如下:

 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/> 

<bean id="userService" class="com.zx.xfiretext.webserver.UserServiceImpl"/> 
<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/> 

<bean name="UserService" class="org.codehaus.xfire.spring.ServiceBean"> 
<property name="serviceBean" ref="userServiceBean"/> 
<property name="serviceClass" value="com.zx.xfiretext.webserver.UserService"/>
 <property name="inHandlers"> 
 <list> 
 <ref bean="addressingHandler"/> </list> 
 </property>
</bean>
<bean id="userServiceBean" class="com.zx.xfiretext.webserver.UserServiceImpl"/> 
</beans> 
 

修改web.xml文件,如下

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Spring Image Database</display-name> 
<description>Spring Image Database sample application</description> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param>
 <listener> 
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 </listener> 
 <servlet> 
 <servlet-name>XFireServlet</servlet-name> 
 <display-name>XFire Servlet</display-name> 
 <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
  </servlet> 
  <servlet-mapping> 
  <servlet-name>XFireServlet</servlet-name> 
  <url-pattern>/services/*</url-pattern> 
  </servlet-mapping> 
  </web-app> 
 

这样就发布了一个UserService,然后就可以进行测试:

通过ie测试:

启动tomcat,在ie中输入http://localhost:8080/XFireService/services/UserService?wsdl 出现wsdl文挡,或者输入http://localhost:8080/XFireService/services/ 出现

Available Services:

  • UserService [wsdl] ,<!----><!---->

则说明配置成功,

客户端测试:

 首先把服务器端的打成jar包

新建一个web动态工程,添加包,

创建一个测试类:

import java.net.MalformedURLException; 
import java.rmi.RemoteException;
import org.codehaus.xfire.client.XFireProxyFactory; 
import org.codehaus.xfire.service.Service; 
import org.codehaus.xfire.client.*; 
import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
import com.zx.xfiretext.domain.User; 
import com.zx.xfiretext.webserver.UserService; 
public class MyClient 
{ 
	public static void main(String[] args) 
	{
try {
	Service serviceModel = new ObjectServiceFactory() .create(UserService.class); 
	UserService service = (UserService) new XFireProxyFactory().create( serviceModel, "http://localhost:8080/XFireService/services/UserService"); 
	User user = service.queryUserByAccoutId("123"); 
	System.out .println("userId=" + user.getUserId() + ", userName=" + user.getUserName() + ", lastLogin=" + user.getLastLogin()); 
	}
catch (MalformedURLException e) {
	e.printStackTrace(); 
	}
} 
	} 

 

 运行测试类,如果在控制台输入的是

userId=123, userName=测试用户, lastLogin=Thu May 22 11:26:48 CST 2008

则说明测试成功。

Ps:如果出现javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.processor.TransformerFactoryImpl not found错误,

错误原因是:
认为是由于jdk1.5 与 tomcat5.0之间的关于 TransformerFactoryImpl 类的冲突造成的。
tomcat-5.0.28\common\endorsed下有两个jar包:xercesImpl.jar和xml-apis.jar,其中的类 javax.xml.transform.TransformerFactory 与jdk1.5中的类org.apache.xalan.processor.TransformerFactoryImpl其实是同一个类。
in tomcat java is called with the following argument:
-Djava.endorsed.dirs="X:\my_app\Portal\tomcat\common\endorsed"
In this directory you find two jar files: xercesImpl.jar and xml-apis.jar needed by tomcat and that must be loaded before all xmsl stuff present in the jdk (1.4 naming problem). And in the file xml-apis.jar the TransformerFactoryImpl is set to "org.apache.xalan.processor.TransformerFactoryImpl".

解决办法:
1. 将xml-apis.jar移出endorsed文件夹。
2. 用xalan系列jar包替换原来的xercesImpl.jar和xml-apis.jar。
xalan系列jar包:serializer.jar、xalan.jar、xercesImpl.jar和xml-apis.jar。

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics