一、先看目录结构。
-xfire
+src
-war
-WEB-INF
+class
+lib
二、再看\xfire\war\WEB-INF\build.xml和build.properties。
build.xml
< project name ="xfire" basedir ="." default ="usage" >
< property file ="build.properties" />
< property name ="src.dir" value ="src" />
<!-- <property name="lib.dir" value="lib"/> -->
< property name ="web.dir" value ="war" />
< property name ="build.dir" value ="${web.dir}/WEB-INF/classes" />
< property name ="name" value ="xfire" />
< path id ="master-classpath" >
<!--
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
-->
< fileset dir ="${web.dir}/WEB-INF/lib" >
< include name ="*.jar" />
</ fileset >
<!-- We need the servlet API classes: -->
<!-- for Tomcat 4.1 use servlet.jar -->
<!-- for Tomcat 5.0 use servlet-api.jar -->
<!-- for Other app server - check the docs -->
< pathelement path ="${build.dir}" />
</ path >
< target name ="usage" >
< echo message ="" />
< echo message ="${name} build file" />
< echo message ="-----------------------------------" />
< echo message ="" />
< echo message ="Available targets are:" />
< echo message ="" />
< echo message ="build --> Build the application" />
< echo message ="junit --> Junit Test" />
< echo message ="deploy --> Deploy application as directory" />
< echo message ="deploywar --> Deploy application as a WAR file" />
< echo message ="install --> Install application in Tomcat" />
< echo message ="reload --> Reload application in Tomcat" />
< echo message ="start --> Start Tomcat application" />
< echo message ="stop --> Stop Tomcat application" />
< echo message ="list --> List Tomcat applications" />
< echo message ="" />
</ target >
< target name ="build" description ="Compile main source tree java files" >
< mkdir dir ="${build.dir}" />
< javac destdir ="${build.dir}" target ="1.4" debug ="true" source ="1.4"
deprecation ="false" optimize ="false" failonerror ="true" encoding ="GBK" >
< src path ="${src.dir}" />
<!-- <compilerarg value="-Xlint:unchecked" /> -->
< classpath refid ="master-classpath" />
</ javac >
</ target >
< target name ="deploy" depends ="build" description ="Deploy application" >
< copy todir ="${deploy.path}/${name}" preservelastmodified ="true" >
< fileset dir ="${web.dir}" >
< include name ="**/*.*" />
</ fileset >
</ copy >
</ target >
< target name ="deploywar" depends ="build" description ="Deploy application as a WAR file" >
< war destfile ="${name}.war"
webxml ="${web.dir}/WEB-INF/web.xml" >
< fileset dir ="${web.dir}" >
< include name ="**/*.*" />
</ fileset >
</ war >
< copy todir ="${deploy.path}" preservelastmodified ="true" >
< fileset dir ="." >
< include name ="*.war" />
</ fileset >
</ copy >
</ target >
< target name ="clean" description ="Clean output directories" >
< delete >
< fileset dir ="${build.dir}" >
< include name ="**/*.class" />
</ fileset >
</ delete >
</ target >
< target name ="undeploy" description ="Un-Deploy application" >
< delete >
< fileset dir ="${deploy.path}/${name}" >
< include name ="**/*.*" />
</ fileset >
</ delete >
</ target >
< target name ="junit" depends ="build" description ="Run JUnit Tests" >
< junit printsummary ="on"
fork ="false"
haltonfailure ="false"
failureproperty ="tests.failed"
showoutput ="false" >
< classpath refid ="master-classpath" />
< formatter type ="brief" usefile ="false" />
< batchtest >
< fileset dir ="${build.dir}" >
< include name ="**/Test*.*" />
</ fileset >
</ batchtest >
</ junit >
< fail if ="tests.failed" >
tests.failed=${tests.failed}
***********************************************************
***********************************************************
**** One or more tests failed! Check the output ****
***********************************************************
***********************************************************
</ fail >
</ target >
< target name ="Junitdatabase" depends ="build" description ="Run JUnit Tests" >
< junit printsummary ="on"
fork ="false"
haltonfailure ="false"
failureproperty ="tests.failed"
showoutput ="true" >
< classpath refid ="master-classpath" />
< formatter type ="brief" usefile ="false" />
< batchtest >
< fileset dir ="${build.dir}" >
< include name ="**/TestC*.*" />
</ fileset >
</ batchtest >
</ junit >
< fail if ="tests.failed" >
***********************************************************
***********************************************************
**** One or more tests failed! Check the output ****
***********************************************************
***********************************************************
</ fail >
</ target >
<!-- ============================================================== -->
<!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
<!-- ============================================================== -->
< taskdef name ="install" classname ="org.apache.catalina.ant.InstallTask" >
< classpath >
< path location ="${appserver.home}/server/lib/catalina-ant.jar" />
</ classpath >
</ taskdef >
< taskdef name ="reload" classname ="org.apache.catalina.ant.ReloadTask" >
< classpath >
< path location ="${appserver.home}/server/lib/catalina-ant.jar" />
</ classpath >
</ taskdef >
< taskdef name ="list" classname ="org.apache.catalina.ant.ListTask" >
< classpath >
< path location ="${appserver.home}/server/lib/catalina-ant.jar" />
</ classpath >
</ taskdef >
< taskdef name ="start" classname ="org.apache.catalina.ant.StartTask" >
< classpath >
< path location ="${appserver.home}/server/lib/catalina-ant.jar" />
</ classpath >
</ taskdef >
< taskdef name ="stop" classname ="org.apache.catalina.ant.StopTask" >
< classpath >
< path location ="${appserver.home}/server/lib/catalina-ant.jar" />
</ classpath >
</ taskdef >
< taskdef name ="reload" classname ="org.apache.catalina.ant.ReloadTask" >
< classpath >
< path location ="${appserver.home}/server/lib/catalina-ant.jar" />
</ classpath >
</ taskdef >
< target name ="install" description ="Install application in Tomcat" >
< install url ="${tomcat.manager.url}"
username ="${tomcat.manager.username}"
password ="${tomcat.manager.password}"
path ="/${name}"
war ="${name}" />
</ target >
< target name ="reload" description ="Reload application in Tomcat" >
< reload url ="${tomcat.manager.url}"
username ="${tomcat.manager.username}"
password ="${tomcat.manager.password}"
path ="/${name}" />
</ target >
< target name ="start" description ="Start Tomcat application" >
< start url ="${tomcat.manager.url}"
username ="${tomcat.manager.username}"
password ="${tomcat.manager.password}"
path ="/${name}" />
</ target >
< target name ="stop" description ="Stop Tomcat application" >
< stop url ="${tomcat.manager.url}"
username ="${tomcat.manager.username}"
password ="${tomcat.manager.password}"
path ="/${name}" />
</ target >
< target name ="list" description ="List Tomcat applications" >
< list url ="${tomcat.manager.url}"
username ="${tomcat.manager.username}"
password ="${tomcat.manager.password}" />
</ target >
<!-- End Tomcat tasks -->
</ project >
appserver.home=d:/java/tomcat528
deploy.path=${appserver.home}/webapps
tomcat.manager.url=http://localhost:8082/manager
tomcat.manager.username=admin
tomcat.manager.password=admin
二、看web.xml,applicationContext.xml和xfire-servlet.xml
web.xml
<! 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 >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value >
/WEB-INF/applicationContext.xml
classpath:org/codehaus/xfire/spring/xfire.xml
</ param-value >
</ context-param >
< context-param >
< param-name > log4jConfigLocation </ param-name >
< param-value > /WEB-INF/log4j.properties </ param-value >
</ context-param >
< listener >
< listener-class >
org.springframework.web.util.Log4jConfigListener
</ listener-class >
</ listener >
< listener >
< listener-class >
org.springframework.web.context.ContextLoaderListener
</ listener-class >
</ listener >
< 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 > /services/* </ url-pattern >
</ servlet-mapping >
</ web-app >
applicationContext.xml
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="mathBean" class ="org.xfire.bnbn.service.MathServiceImpl" />
</ beans >
xfire-servelt.xml
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
< property name ="urlMap" >
< map >
< entry key ="/MathService" >
< ref bean ="math" />
</ entry >
</ map >
</ property >
</ bean >
< bean id ="math" class ="org.codehaus.xfire.spring.remoting.XFireExporter" >
< property name ="serviceFactory" >
< ref bean ="xfire.serviceFactory" />
</ property >
< property name ="xfire" >
< ref bean ="xfire" />
</ property >
< property name ="serviceBean" >
< ref bean ="mathBean" />
</ property >
< property name ="serviceClass" >
< value > org.xfire.bnbn.service.MathService </ value >
</ property >
</ bean >
</ beans >
三、看service类,xfire对外都是开放接口,也只能是接口。
public interface MathService {
public long add(int p1, int p2);
public class MathServiceImpl implements MathService {
public long add(int p1, int p2) {
return p1 + p2;
}
}
好到这里service配制好了,在cmd目录\xfire下运行ant build命令,编译class。然后在浏览器中运行http://localhost:8082/xfire/services/MathService?wsdl就可以看到xfire自动生成的wsdl了。
四、再写client吧,先看application-client.xml,
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="Client_Params" class ="org.xfire.bnbn.client.ClientParams" >
< property name ="訪問地址" >
< value > http://localhost:8082/xfire/services/MathService </ value >
</ property >
< property name ="輸入字符串" >
< value > 1234567890ABCDEFG XFIRE與SPRING集成成功! </ value >
</ property >
< property name ="paramentA" >
< value > 400 </ value >
</ property >
< property name ="paramentB" >
< value > 200 </ value >
</ property >
</ bean >
</ beans >
再看client类
public class ClientParams {
private String 訪問地址 = null;
private String 輸入字符串 = null;
private int paramentA = 0;
private int paramentB = 0;
public int getParamentB() {
return paramentB;
}
public void setParamentB(int paramentB) {
this.paramentB = paramentB;
}
public String get訪問地址() {
return 訪問地址;
}
public void set訪問地址(String 訪問地址) {
this.訪問地址 = 訪問地址;
}
public String get輸入字符串() {
return 輸入字符串;
}
public void set輸入字符串(String 輸入字符串) {
this.輸入字符串 = 輸入字符串;
}
public int getParamentA() {
return paramentA;
}
public void setParamentA(int paramentA) {
this.paramentA = paramentA;
}
}
再看test class
import java.net.MalformedURLException;
import junit.framework.TestCase;
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 org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.xfire.bnbn.client.ClientParams;
import org.xfire.bnbn.service.MathService;
public class TestMathService extends TestCase{
public void testMath() {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"war/WEB-INF/applicationContext-client.xml");
ClientParams cp = (ClientParams) ctx.getBean("Client_Params");
int paramentA = cp.getParamentA();
int paramentB = cp.getParamentB();
Service srvcModel = new ObjectServiceFactory()
.create(MathService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
String url = cp.get訪問地址();
try {
MathService srvc = (MathService) factory.create(srvcModel, url);
assertEquals(srvc.add(paramentA, paramentB), 600);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
到此client的Spring运行设置完毕,再运行ant build命令,编译完成再运行ant junit命令,如果没出错,则说明Service和client通信正常。
五、运行环境:jdk1.4.2、tomcat5.0.2.8、spring2.0、xfire1.2.5。把xfire-1.2.5下的xfire-all-1.2.5.jar和lib下的所有包放到项目xfire的web-inf/lib下。
六、可以把作几个批处理文件来运行编译,测试。
tb.bat :
ant build
tu.bat :
ant junit
tu.bat :
ant junit
tdeploy.bat :
ant deploy
treload.bat :
ant reload
tstart.bat :
ant start
tu.stop :
ant stop
tbr.bat :
call tb.bat
call tstop.bat
call tstart.bat
tailstdout.bat :
tail -f D:\java\Tomcat528\logs\stdout.log
td.bat :
call tdeploy.bat
rem call tstop.bat
rem call tstart.bat
tdrr.bat :
call tdeploy.bat
call tstop.bat
call tstart.bat
tr.bat :
rem call tstop.bat
rem call tstart.bat
call treload.bat