xfire 与spring完整集成实例(webservice)
?
首先打开Myeclipse8.x以上版本..因为我这里共享那些xfire 项目所需要的jar都是Myeclipse自带添加的,所以要求大家伙准备好myeclipse8.x或者更高版本的..这里是spring2.0与xfire1.2整合..废话少说..转正正传..
?
1.打开Myeclipse--->new project-->项目名随便吧..最好是有意义的名称..看起来比较舒服..
2--配置web.xml
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ? http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
? <context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>classpath:applicationContext.xml,classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
? </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
? ?<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
? <servlet>
? ? <servlet-name>XFireServlet</servlet-name>
? ? <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
? ? <load-on-startup>0</load-on-startup>
? </servlet>
? <servlet-mapping>
? ? <servlet-name>XFireServlet</servlet-name>
? ? <url-pattern>/services/*</url-pattern>
? </servlet-mapping>
? <welcome-file-list>
? ? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>
?
3.配置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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
?http://www.springframework.org/schema/aop
?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
?http://www.springframework.org/schema/tx
?http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
?http://www.springframework.org/schema/jee
?http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
?default-autowire="byName" default-lazy-init="true"
?>
?<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
?<import resource="xfire-servlet.xml"/>
</beans>
?
?
?
?
4.配置xfire-servlet.xml
?
为了不给大家伙困扰.所以我这里用那个就打开那个配置..
?
?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!--?
<bean id="HellWorldSimple" encoding="UTF-8"?>
<beans>
<!--
? ?在没整合spring之前的xfire是这样的配置
?当要整合spring之后的这句代码复制到service 节点
?xmlns="http://xfire.codehaus.org/config/1.0"
?<beans xmlns="http://xfire.codehaus.org/config/1.0">
?-->
<service xmlns="http://xfire.codehaus.org/config/1.0">
?
?<name>UserInfo</name>
?<namespace>http://service.core.xfire.com</namespace>
?<serviceClass>com.xfire.core.service.IUserInfoService</serviceClass>
?<implementationClass>com.xfire.core.service.impl.UserInfoServiceImpl
?</implementationClass>
?<scope>request</scope>
?
?
<!--?
<name>HelloWorldSimple</name>
<namespace>http://service.core.xfire.com</namespace>
<serviceClass>com.xfire.core.service.IHelloWorld</serviceClass>
<implementationClass>com.xfire.core.service.impl.HelloWorldImpl</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>request</scope>
-->
<!-- ?
<name>MyHandle</name>
<namespace>http://service.core.xfire.com</namespace>
<serviceClass>com.xfire.core.handler.IMyHandler</serviceClass>
<implementationClass>com.xfire.core.handler.MyHandlerImpl</implementationClass>
<scope>application</scope>
?-->
?<!--?
<name>ServiceMap</name>
<namespace>http://service.core.xfire.com</namespace>
<serviceClass>com.xfire.core.service.IServiceMap</serviceClass>
<implementationClass>com.xfire.core.service.impl.ServiceMapImpl
</implementationClass>
<scope>request</scope>
?-->
?
?
<!--?
<name>ApplictionException</name>
<namespace>http://service.core.xfire.com</namespace>
<serviceClass>com.xfire.core.service.IMyExceptionSevice</serviceClass>
<implementationClass>com.xfire.core.service.impl.MyExceptionSeviceImpl</implementationClass>
<scope>request</scope>
-->
?
</service>
</beans>
?
?
实现代码..
首先新建一个实体类UseInfor
package com.xfire.core.entity;
?
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
?
/**
?*@author jilongliang
?*@Date 2012-2-29
?*/
@SuppressWarnings("all")
public class?UserInfo?implements Serializable {
?
private Integer identifier;
?
private String address;
private String city;
private String proivice;
?
public Integer getIdentifier() {
return identifier;
}
?
public void setIdentifier(Integer identifier) {
this.identifier = identifier;
}
?
public String getAddress() {
return address;
}
?
public void setAddress(String address) {
this.address = address;
}
?
public String getCity() {
return city;
}
?
public void setCity(String city) {
this.city = city;
}
?
public String getProivice() {
return proivice;
}
?
public void setProivice(String proivice) {
this.proivice = proivice;
}
?
public String getCountry() {
return country;
}
?
public void setCountry(String country) {
this.country = country;
}
?
public String getPostCode() {
return postCode;
}
?
public void setPostCode(String postCode) {
this.postCode = postCode;
}
?
public String[] getArrary() {
return arrary;
}
?
public void setArrary(String[] arrary) {
this.arrary = arrary;
}
?
public List<Integer> getList() {
return list;
}
?
public void setList(List<Integer> list) {
this.list = list;
}
?
public Map<Integer, InnerClass> getMap() {
return map;
}
?
public void setMap(Map<Integer, InnerClass> map) {
this.map = map;
}
?
public boolean isExist() {
return isExist;
}
?
public void setExist(boolean isExist) {
this.isExist = isExist;
}
?
public InnerClass getInner() {
return inner;
}
?
public void setInner(InnerClass inner) {
this.inner = inner;
}
?
private String country;
private String postCode;
private String[] arrary;
private List<Integer> list;
?
private Map<Integer, InnerClass> map;
?
private boolean isExist;
private InnerClass inner;
?
public UserInfo() {
?
list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
?
map = new HashMap<Integer, InnerClass>();
map.put(1, new InnerClass("a"));
map.put(2, new InnerClass("b"));
map.put(3, new InnerClass("c"));
?
inner = new InnerClass();
inner.setInnerName("服务器地址: Address InnerClass");
?
}
?
@Override
public String toString() {
return "Address [address=" + address + ", arrary="
+ Arrays.toString(arrary) + ", city=" + city + ", country="
+ country + ", identifier=" + identifier + ", inner=" + inner
+ ", isExist=" + isExist + ", list=" + list + ", map=" + map
+ ", postCode=" + postCode + ", proivice=" + proivice + "]";
}
?
}
----内置实体类package com.xfire.core.entity;
import java.io.Serializable;
/**?*@Project xfire?*@author jilongliang?*@Date 2012-2-29?*@Class InnerClass.java?*/@SuppressWarnings("all")public class?InnerClass?implements Serializable {
private String innerName = "static InnerClass";
public String getInnerName() {return innerName;}
public void setInnerName(String innerName) {this.innerName = innerName;}
public InnerClass() {}
public InnerClass(String innerName) {super();this.innerName = innerName;
}}
其次一个接口类:package com.xfire.core.service;
import java.util.List;
import com.xfire.core.entity.UserInfo;
/**?*@author jilongliang?*@Date 2012-2-29?*/public interface?IUserInfoService?{public List<UserInfo> getAddressList();
public List<UserInfo> setAddresses(List<UserInfo> list);}
然后新建一个实现类去实现接口
package com.xfire.core.service.impl;
import java.util.ArrayList;import java.util.List;
import com.xfire.core.entity.UserInfo;import com.xfire.core.service.IUserInfoService;
/**?*@author jilongliang?*@Date 2012-2-29?*/public class UserInfoServiceImpl implements IUserInfoService {
private int requestCount = 0;
public List<UserInfo> getAddressList() {requestCount++;System.out.println("requestCount" + requestCount);List<UserInfo> listAdd = new ArrayList<UserInfo>();UserInfo address = new UserInfo();address.setIdentifier(1);address.setCountry("中");address.setProivice("省");address.setCity("江");address.setAddress("春");address.setPostCode("1111111");address.setExist(false);address.setArrary(new String[] { "22", "23", "24" });listAdd.add(address);
address.setIdentifier(2);address.setCountry("中");address.setProivice("省");address.setCity("江");address.setAddress("春");address.setPostCode("1111111");address.setExist(false);address.setArrary(new String[] { "a", "b", "c" });listAdd.add(address);
return listAdd;}
/**?*??* @param list?* @return?*/public List<UserInfo> setAddresses(List<UserInfo> list) {requestCount++;System.out.println("requestCount" + requestCount);return list;}
}
-----客户端
package com.xfire.core.client;
import java.net.MalformedURLException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;
import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.xfire.core.entity.UserInfo;import com.xfire.core.service.IUserInfoService;
/**?*@author jilongliang?*@Date 2012-3-5?*??*/public class?UserInfoClient?{
public static void main(String[] args) {getServiceList();setServiceList();}
static String url = "http://localhost:8081/xfire/services/UserInfo";
/**?*??*/public static void getServiceList() {Service service = new ObjectServiceFactory().create(IUserInfoService.class);
try {IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory().create(service, url);List<UserInfo> list = (ArrayList<UserInfo>) iAddressService.getAddressList();
System.out.println("一共多少条数据:" + list.size());
for (Iterator<UserInfo> iter = list.iterator(); iter.hasNext();) {
UserInfo a = iter.next();
System.out.println(a);}
} catch (MalformedURLException e) {e.printStackTrace();}}
public static void setServiceList() {Service service = new ObjectServiceFactory().create(IUserInfoService.class);
try {IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory().create(service, url);
List<UserInfo> listAdd = new ArrayList<UserInfo>();UserInfo address = new UserInfo();address.setIdentifier(1);address.setCountry("中");address.setProivice("省");address.setCity("江");address.setAddress("春");address.setPostCode("1111111");address.setExist(false);address.setArrary(new String[] { "22", "23", "24" });listAdd.add(address);
address.setIdentifier(2);address.setCountry("中");address.setProivice("省");address.setCity("江");address.setAddress("春");address.setPostCode("1111111");address.setExist(false);address.setArrary(new String[] { "a", "b", "c" });listAdd.add(address);
List<UserInfo> returnAdd = iAddressService.setAddresses(listAdd);
System.out.println("总数:" + returnAdd);
for (Iterator<UserInfo> it = returnAdd.iterator(); it.hasNext();) {UserInfo address2 = it.next();
System.out.println(address2);}
} catch (MalformedURLException e) {e.printStackTrace();}
}}
图解----
?


?
?
?
?
?
?
?
?
?
?
?
电子书下载http://download.csdn.net/detail/l_ji_l/4116804
源代码下载:http://download.csdn.net/detail/l_ji_l/4116788