WebService框架整理(二) Axis1+Spring
WebService框架整理(二)?Axis1+Spring?
(2010-11-18 16:13:06)
转载标签:?杂谈?文章分类:Java编程初识Axis1就要把它集成到Spring框架上。一方面是当时的项目要求,在一方面更是我对于Spring情有独钟。?
Axis1+Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持。?
相关链接:?
WebService框架整理(一) Axis1?
WebService框架整理(二) Axis1+Spring?
我们将用到以下Jar:?
引用
activation.jar?
axis.jar?
commons-discovery.jar?
commons-logging.jar?
jaxrpc.jar?
log4j-1.2.15.jar?
mail.jar?
wsdl4j.jar?
spring.jar?
主要就是加入了spring.jar包!?
再看看web.xml,加入了Spring的相关内容。大家都熟悉Spring,我就不废话了!?
Xml代码??spring-axis-1 log4jConfigLocation classpath:log4j.xml log4jRefreshInterval 60000 contextConfigLocation /WEB-INF/applicationContext.xml UTF-8 Filter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true UTF-8 Filter public interface CalcService { int add(int a, int b); } " quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">- ??
- public?interface?CalcService?{??
- ??
- ??????
- ????int?add(int?a,?int?b);??
- ??
- }??
给出对应的实现内容:?
Java代码?- import?org.zlex.axis.service.CalcService;??
- ??
- ??
- public?class?CalcServiceImpl?implements?CalcService?{??
- ??
- ????@Override??
- ????public?int?add(int?a,?int?b)?{??
- ????????return?a?+?b;??
- ????}??
- ??
- }??
再简单不过的1+1问题!
?
将其注入到Spring的容器中,applicationContext.xml如下所示:?
Xml代码??" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">- <?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:context="http://www.springframework.org/schema/context"??
- ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd??
- ????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context.xsd">??
- ????<bean??
- ????????id="calcService"??
- ????????class="org.zlex.axis.service.impl.CalcServiceImpl"?/>??
- </beans>??
作为spring与axis1对接,需要做一个ServletEndpointSupport继承实现WebService,如下所示:?
Java代码?- import?javax.xml.rpc.ServiceException;??
- ??
- import?org.springframework.context.ApplicationContext;??
- import?org.springframework.remoting.jaxrpc.ServletEndpointSupport;??
- import?org.zlex.axis.service.CalcService;??
- ??
- ??
- public?class?WebService?extends?ServletEndpointSupport?{??
- ??
- ????private?ApplicationContext?applicationContext;??
- ????private?CalcService?calcService;??
- ??
- ??????
- ????@Override??
- ????protected?void?onInit()?throws?ServiceException?{??
- ????????//?初始化Spirng?配置??
- ????????applicationContext?=?super.getApplicationContext();??
- ??
- ????????calcService?=?(CalcService)?applicationContext.getBean("calcService");??
- ??
- ????}??
- ??
- ??????
- ????public?String?add(int?a,?int?b)?{??
- ????????return?String.valueOf(calcService.add(a,?b));??
- ????}??
- ??
- }??
这里为了便于在eclipse演示,将返回值定为String类型!?
现在我们将该服务植入Axis中,修改server-config.wsdd文件,在原文件中加入如下内容:?
Xml代码??" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">- <!--?自定义服务?-->??
- <service??
- ????name="WebService"??
- ????provider="java:RPC">??
- ????<parameter??
- ????????name="className"??
- ????????value="org.zlex.axis.WebService"?/>??
- </service>??
修改后的server-config.wsdd文件如下所示:?
Xml代码??http://xml.apache.org/axis/wsdd/ " quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">- <?xml?version="1.0"?encoding="UTF-8"?>??
- <deployment??
- ????xmlns="http://xml.apache.org/axis/wsdd/"??
- ????xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">??
- ????<globalConfiguration>??
- ????????<parameter??
- ????????????name="adminPassword"??
- ????????????value="admin"?/>??
- ????????<parameter??
- ????????????name="sendXsiTypes"??
- ????????????value="true"?/>??
- ????????<parameter??
- ????????????name="sendMultiRefs"??
- ????????????value="true"?/>??
- ????????<parameter??
- ????????????name="sendXMLDeclaration"??
- ????????????value="true"?/>??
- ????????<parameter??
- ????????????name="axis.sendMinimizedElements"??
- ????????????value="true"?/>??
- ????????<requestFlow>??
- ????????????<handler??
- ????????????????type="java:org.apache.axis.handlers.JWSHandler">??
- ????????????????<parameter??
- ????????????????????name="scope"??
- ????????????????????value="session"?/>??
- ????????????</handler>??
- ????????????<handler??
- ????????????????type="java:org.apache.axis.handlers.JWSHandler">??
- ????????????????<parameter??
- ????????????????????name="scope"??
- ????????????????????value="request"?/>??
- ????????????????<parameter??
- ????????????????????name="extension"??
- ????????????????????value=".jwr"?/>??
- ????????????</handler>??
- ????????</requestFlow>??
- ????</globalConfiguration>??
- ????<handler??
- ????????name="Authenticate"??
- ????????type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"?/>??
- ????<handler??
- ????????name="LocalResponder"??
- ????????type="java:org.apache.axis.transport.local.LocalResponder"?/>??
- ????<handler??
- ????????name="URLMapper"??
- ????????type="java:org.apache.axis.handlers.http.URLMapper"?/>??
- ????<service??
- ????????name="AdminService"??
- ????????provider="java:MSG">??
- ????????<parameter??
- ????????????name="allowedMethods"??
- ????????????value="AdminService"?/>??
- ????????<parameter??
- ????????????name="enableRemoteAdmin"??
- ????????????value="false"?/>??
- ????????<parameter??
- ????????????name="className"??
- ????????????value="org.apache.axis.utils.Admin"?/>??
- ????????<namespace>http://xml.apache.org/axis/wsdd/</namespace>??
- ????</service>??
- ????<service??
- ????????name="Version"??
- ????????provider="java:RPC">??
- ????????<parameter??
- ????????????name="allowedMethods"??
- ????????????value="getVersion"?/>??
- ????????<parameter??
- ????????????name="className"??
- ????????????value="org.apache.axis.Version"?/>??
- ????</service>??
- ????<transport??
- ????????name="http">??
- ????????<requestFlow>??
- ????????????<handler??
- ????????????????type="URLMapper"?/>??
- ????????????<handler??
- ????????????????type="java:org.apache.axis.handlers.http.HTTPAuthHandler"?/>??
- ????????</requestFlow>??
- ????</transport>??
- ????<transport??
- ????????name="local">??
- ????????<responseFlow>??
- ????????????<handler??
- ????????????????type="LocalResponder"?/>??
- ????????</responseFlow>??
- ????</transport>??
- ??
- ????<!--?自定义服务?-->??
- ????<service??
- ????????name="WebService"??
- ????????provider="java:RPC">??
- ????????<parameter??
- ????????????name="className"??
- ????????????value="org.zlex.axis.WebService"?/>??
- ????</service>??
- </deployment>??
我们随机抽取2个数进行求和运算,并验证WebService和本地计算结果是否一致,测试用例WebServiceTest如下:?
Axis1+Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持。?
相关链接:?
WebService框架整理(一) Axis1?
WebService框架整理(二) Axis1+Spring?
我们将用到以下Jar:?
引用
activation.jar?
axis.jar?
commons-discovery.jar?
commons-logging.jar?
jaxrpc.jar?
log4j-1.2.15.jar?
mail.jar?
wsdl4j.jar?
spring.jar?
主要就是加入了spring.jar包!?
再看看web.xml,加入了Spring的相关内容。大家都熟悉Spring,我就不废话了!?
Xml代码??spring-axis-1 log4jConfigLocation classpath:log4j.xml log4jRefreshInterval 60000 contextConfigLocation /WEB-INF/applicationContext.xml UTF-8 Filter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true UTF-8 Filter public interface CalcService { int add(int a, int b); } " quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
- ??
- public?interface?CalcService?{??
- ??
- ??????
- ????int?add(int?a,?int?b);??
- ??
- }??
给出对应的实现内容:?
Java代码?
- import?org.zlex.axis.service.CalcService;??
- ??
- ??
- public?class?CalcServiceImpl?implements?CalcService?{??
- ??
- ????@Override??
- ????public?int?add(int?a,?int?b)?{??
- ????????return?a?+?b;??
- ????}??
- ??
- }??
再简单不过的1+1问题!

将其注入到Spring的容器中,applicationContext.xml如下所示:?
Xml代码??" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
- <?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:context="http://www.springframework.org/schema/context"??
- ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd??
- ????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context.xsd">??
- ????<bean??
- ????????id="calcService"??
- ????????class="org.zlex.axis.service.impl.CalcServiceImpl"?/>??
- </beans>??
作为spring与axis1对接,需要做一个ServletEndpointSupport继承实现WebService,如下所示:?
Java代码?
- import?javax.xml.rpc.ServiceException;??
- ??
- import?org.springframework.context.ApplicationContext;??
- import?org.springframework.remoting.jaxrpc.ServletEndpointSupport;??
- import?org.zlex.axis.service.CalcService;??
- ??
- ??
- public?class?WebService?extends?ServletEndpointSupport?{??
- ??
- ????private?ApplicationContext?applicationContext;??
- ????private?CalcService?calcService;??
- ??
- ??????
- ????@Override??
- ????protected?void?onInit()?throws?ServiceException?{??
- ????????//?初始化Spirng?配置??
- ????????applicationContext?=?super.getApplicationContext();??
- ??
- ????????calcService?=?(CalcService)?applicationContext.getBean("calcService");??
- ??
- ????}??
- ??
- ??????
- ????public?String?add(int?a,?int?b)?{??
- ????????return?String.valueOf(calcService.add(a,?b));??
- ????}??
- ??
- }??
这里为了便于在eclipse演示,将返回值定为String类型!?
现在我们将该服务植入Axis中,修改server-config.wsdd文件,在原文件中加入如下内容:?
Xml代码??" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
- <!--?自定义服务?-->??
- <service??
- ????name="WebService"??
- ????provider="java:RPC">??
- ????<parameter??
- ????????name="className"??
- ????????value="org.zlex.axis.WebService"?/>??
- </service>??
修改后的server-config.wsdd文件如下所示:?
Xml代码??http://xml.apache.org/axis/wsdd/ " quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <deployment??
- ????xmlns="http://xml.apache.org/axis/wsdd/"??
- ????xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">??
- ????<globalConfiguration>??
- ????????<parameter??
- ????????????name="adminPassword"??
- ????????????value="admin"?/>??
- ????????<parameter??
- ????????????name="sendXsiTypes"??
- ????????????value="true"?/>??
- ????????<parameter??
- ????????????name="sendMultiRefs"??
- ????????????value="true"?/>??
- ????????<parameter??
- ????????????name="sendXMLDeclaration"??
- ????????????value="true"?/>??
- ????????<parameter??
- ????????????name="axis.sendMinimizedElements"??
- ????????????value="true"?/>??
- ????????<requestFlow>??
- ????????????<handler??
- ????????????????type="java:org.apache.axis.handlers.JWSHandler">??
- ????????????????<parameter??
- ????????????????????name="scope"??
- ????????????????????value="session"?/>??
- ????????????</handler>??
- ????????????<handler??
- ????????????????type="java:org.apache.axis.handlers.JWSHandler">??
- ????????????????<parameter??
- ????????????????????name="scope"??
- ????????????????????value="request"?/>??
- ????????????????<parameter??
- ????????????????????name="extension"??
- ????????????????????value=".jwr"?/>??
- ????????????</handler>??
- ????????</requestFlow>??
- ????</globalConfiguration>??
- ????<handler??
- ????????name="Authenticate"??
- ????????type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"?/>??
- ????<handler??
- ????????name="LocalResponder"??
- ????????type="java:org.apache.axis.transport.local.LocalResponder"?/>??
- ????<handler??
- ????????name="URLMapper"??
- ????????type="java:org.apache.axis.handlers.http.URLMapper"?/>??
- ????<service??
- ????????name="AdminService"??
- ????????provider="java:MSG">??
- ????????<parameter??
- ????????????name="allowedMethods"??
- ????????????value="AdminService"?/>??
- ????????<parameter??
- ????????????name="enableRemoteAdmin"??
- ????????????value="false"?/>??
- ????????<parameter??
- ????????????name="className"??
- ????????????value="org.apache.axis.utils.Admin"?/>??
- ????????<namespace>http://xml.apache.org/axis/wsdd/</namespace>??
- ????</service>??
- ????<service??
- ????????name="Version"??
- ????????provider="java:RPC">??
- ????????<parameter??
- ????????????name="allowedMethods"??
- ????????????value="getVersion"?/>??
- ????????<parameter??
- ????????????name="className"??
- ????????????value="org.apache.axis.Version"?/>??
- ????</service>??
- ????<transport??
- ????????name="http">??
- ????????<requestFlow>??
- ????????????<handler??
- ????????????????type="URLMapper"?/>??
- ????????????<handler??
- ????????????????type="java:org.apache.axis.handlers.http.HTTPAuthHandler"?/>??
- ????????</requestFlow>??
- ????</transport>??
- ????<transport??
- ????????name="local">??
- ????????<responseFlow>??
- ????????????<handler??
- ????????????????type="LocalResponder"?/>??
- ????????</responseFlow>??
- ????</transport>??
- ??
- ????<!--?自定义服务?-->??
- ????<service??
- ????????name="WebService"??
- ????????provider="java:RPC">??
- ????????<parameter??
- ????????????name="className"??
- ????????????value="org.zlex.axis.WebService"?/>??
- ????</service>??
- </deployment>??
我们随机抽取2个数进行求和运算,并验证WebService和本地计算结果是否一致,测试用例WebServiceTest如下:?