有同事常常会讨论Axis2 与 Spring 的整合说网上没有找到这方面的资料,在这里我就写了一个小例子,希望对广大程序员有帮助
1.下载Axis2
我在这里选用的axis2-1.5.4 是这个版本
下载spring 对应的包
这里我们选用了tomcat 作为服务器(当然jdk 5.0 以上)
2. 使用eclipse 新建dynamicWeb 项目
导入*axis2-1.5/lib 包的所有jar 包加入一个spring中的一个spring.jar 就可以了
这里那包具体怎么样我就不讲了,反正全部都要
3. 在web 容器中添加spring 和 Axis2 信息 配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 指定spring 配置信息 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/com/webservice/spring/conf/webservice-bean.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 设置axisServlet 配置信息 --> <servlet> <servlet-name>AxisServlet</servlet-name> <display-name>AxisServlet</display-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <display-name>bs_interface</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
4. 添加业务处理类
1)
package com.webservice.service;
import com.webservice.entity.UserInfo;
public interface IUserInfoService {
public void add(UserInfo userInfo) throws Exception;
public void delete(UserInfo userInfo) throws Exception;
}
2) 写实现类
package com.webservice.service.impl;
import com.webservice.entity.UserInfo;
import com.webservice.service.IUserInfoService;
public class UserInfoServiceImpl implements IUserInfoService {
@Override
public void add(UserInfo userInfo) throws Exception {
System.out.println("addition successful");
}
@Override
public void delete(UserInfo userInfo) throws Exception {
System.out.println("remove successful");
}
}
3) 添加webservice 业务类
package com.webservice.service.impl;
import com.webservice.entity.UserInfo;
import com.webservice.service.IUserInfoService;
/**
* 2011-3-23
* @author liuqing
*
*/
public class UserInfoWebService {
public void add(UserInfo userInfo) throws Exception {
this.userInfoService.add(userInfo);
}
public void delete(UserInfo userInfo) throws Exception {
this.userInfoService.delete(userInfo);
}
public IUserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(IUserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
private IUserInfoService userInfoService;
}
5. 配置spring 配置文件
<?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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 添加spring ApplicationContextHolder Bean 在Spring 中注入Bean 代理器 --> <bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /> <!-- 添加Spring Bean 对象 --> <bean id="userInfoWebService" class="com.webservice.service.impl.UserInfoWebService"> <property name="userInfoService" ref="userInfoService" /> </bean> <bean id="userInfoService" class="com.webservice.service.impl.UserInfoServiceImpl"> </bean> </beans>
6.在WEB-INF/services/test/META-INF/services.xml
(注意:WEB-INF/services/test 这里的test 是什么都可以的,这里的services目录相当于tomcat 中webapp)
<?xml version="1.0" encoding="UTF-8"?> <serviceGroup> <service name="userWebService"> <description>SyncNotifySPService</description> <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <parameter name="SpringBeanName"> userInfoWebService </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </messageReceivers> </service> </serviceGroup>
启动Tomcat 后输入
http://localhost:8080/bs_interface/services/userWebService?wsdl 就OK
userWebService.wsdl 文件内容为
<?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://impl.service.webservice.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax23="http://entity.webservice.com/xsd" xmlns:ax21="http://service.webservice.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://impl.service.webservice.com"> <wsdl:documentation>userWebService</wsdl:documentation> - <wsdl:types> - <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://entity.webservice.com/xsd"> - <xs:complexType name="UserInfo"> - <xs:sequence> <xs:element minOccurs="0" name="id" nillable="true" type="xs:int" /> <xs:element minOccurs="0" name="other" nillable="true" type="xs:string" /> <xs:element minOccurs="0" name="password" nillable="true" type="xs:string" /> <xs:element minOccurs="0" name="username" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> - <xs:schema xmlns:ax24="http://entity.webservice.com/xsd" xmlns:ax22="http://service.webservice.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://impl.service.webservice.com"> <xs:import namespace="http://service.webservice.com/xsd" /> <xs:import namespace="http://entity.webservice.com/xsd" /> - <xs:element name="setUserInfoService"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="userInfoService" nillable="true" type="ax22:IUserInfoService" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="getUserInfoServiceResponse"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="ax22:IUserInfoService" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:complexType name="Exception"> - <xs:sequence> <xs:element minOccurs="0" name="Exception" nillable="true" type="xs:anyType" /> </xs:sequence> </xs:complexType> - <xs:element name="Exception"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="Exception" nillable="true" type="ns:Exception" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="delete"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="userInfo" nillable="true" type="ax24:UserInfo" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="add"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="userInfo" nillable="true" type="ax24:UserInfo" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> - <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.webservice.com/xsd"> - <xs:complexType name="IUserInfoService"> <xs:sequence /> </xs:complexType> </xs:schema> </wsdl:types> - <wsdl:message name="setUserInfoServiceRequest"> <wsdl:part name="parameters" element="ns:setUserInfoService" /> </wsdl:message> - <wsdl:message name="deleteRequest"> <wsdl:part name="parameters" element="ns:delete" /> </wsdl:message> - <wsdl:message name="Exception"> <wsdl:part name="parameters" element="ns:Exception" /> </wsdl:message> - <wsdl:message name="addRequest"> <wsdl:part name="parameters" element="ns:add" /> </wsdl:message> <wsdl:message name="getUserInfoServiceRequest" /> - <wsdl:message name="getUserInfoServiceResponse"> <wsdl:part name="parameters" element="ns:getUserInfoServiceResponse" /> </wsdl:message> - <wsdl:portType name="userWebServicePortType"> - <wsdl:operation name="setUserInfoService"> <wsdl:input message="ns:setUserInfoServiceRequest" wsaw:Action="urn:setUserInfoService" /> </wsdl:operation> - <wsdl:operation name="delete"> <wsdl:input message="ns:deleteRequest" wsaw:Action="urn:delete" /> <wsdl:fault message="ns:Exception" name="Exception" wsaw:Action="urn:deleteException" /> </wsdl:operation> - <wsdl:operation name="add"> <wsdl:input message="ns:addRequest" wsaw:Action="urn:add" /> <wsdl:fault message="ns:Exception" name="Exception" wsaw:Action="urn:addException" /> </wsdl:operation> - <wsdl:operation name="getUserInfoService"> <wsdl:input message="ns:getUserInfoServiceRequest" wsaw:Action="urn:getUserInfoService" /> <wsdl:output message="ns:getUserInfoServiceResponse" wsaw:Action="urn:getUserInfoServiceResponse" /> </wsdl:operation> </wsdl:portType> - <wsdl:binding name="userWebServiceSoap11Binding" type="ns:userWebServicePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <wsdl:operation name="delete"> <soap:operation soapAction="urn:delete" style="document" /> - <wsdl:input> <soap:body use="literal" /> </wsdl:input> - <wsdl:fault name="Exception"> <soap:fault use="literal" name="Exception" /> </wsdl:fault> </wsdl:operation> - <wsdl:operation name="setUserInfoService"> <soap:operation soapAction="urn:setUserInfoService" style="document" /> - <wsdl:input> <soap:body use="literal" /> </wsdl:input> </wsdl:operation> - <wsdl:operation name="add"> <soap:operation soapAction="urn:add" style="document" /> - <wsdl:input> <soap:body use="literal" /> </wsdl:input> - <wsdl:fault name="Exception"> <soap:fault use="literal" name="Exception" /> </wsdl:fault> </wsdl:operation> - <wsdl:operation name="getUserInfoService"> <soap:operation soapAction="urn:getUserInfoService" style="document" /> - <wsdl:input> <soap:body use="literal" /> </wsdl:input> - <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="userWebServiceSoap12Binding" type="ns:userWebServicePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <wsdl:operation name="delete"> <soap12:operation soapAction="urn:delete" style="document" /> - <wsdl:input> <soap12:body use="literal" /> </wsdl:input> - <wsdl:fault name="Exception"> <soap12:fault use="literal" name="Exception" /> </wsdl:fault> </wsdl:operation> - <wsdl:operation name="setUserInfoService"> <soap12:operation soapAction="urn:setUserInfoService" style="document" /> - <wsdl:input> <soap12:body use="literal" /> </wsdl:input> </wsdl:operation> - <wsdl:operation name="add"> <soap12:operation soapAction="urn:add" style="document" /> - <wsdl:input> <soap12:body use="literal" /> </wsdl:input> - <wsdl:fault name="Exception"> <soap12:fault use="literal" name="Exception" /> </wsdl:fault> </wsdl:operation> - <wsdl:operation name="getUserInfoService"> <soap12:operation soapAction="urn:getUserInfoService" style="document" /> - <wsdl:input> <soap12:body use="literal" /> </wsdl:input> - <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="userWebServiceHttpBinding" type="ns:userWebServicePortType"> <http:binding verb="POST" /> - <wsdl:operation name="delete"> <http:operation location="userWebService/delete" /> - <wsdl:input> <mime:content type="text/xml" part="delete" /> </wsdl:input> </wsdl:operation> - <wsdl:operation name="setUserInfoService"> <http:operation location="userWebService/setUserInfoService" /> - <wsdl:input> <mime:content type="text/xml" part="setUserInfoService" /> </wsdl:input> </wsdl:operation> - <wsdl:operation name="add"> <http:operation location="userWebService/add" /> - <wsdl:input> <mime:content type="text/xml" part="add" /> </wsdl:input> </wsdl:operation> - <wsdl:operation name="getUserInfoService"> <http:operation location="userWebService/getUserInfoService" /> - <wsdl:input> <mime:content type="text/xml" part="getUserInfoService" /> </wsdl:input> - <wsdl:output> <mime:content type="text/xml" part="getUserInfoService" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:service name="userWebService"> - <wsdl:port name="userWebServiceHttpSoap11Endpoint" binding="ns:userWebServiceSoap11Binding"> <soap:address location="http://localhost:8080/bs_interface/services/userWebService.userWebServiceHttpSoap11Endpoint/" /> </wsdl:port> - <wsdl:port name="userWebServiceHttpSoap12Endpoint" binding="ns:userWebServiceSoap12Binding"> <soap12:address location="http://localhost:8080/bs_interface/services/userWebService.userWebServiceHttpSoap12Endpoint/" /> </wsdl:port> - <wsdl:port name="userWebServiceHttpEndpoint" binding="ns:userWebServiceHttpBinding"> <http:address location="http://localhost:8080/bs_interface/services/userWebService.userWebServiceHttpEndpoint/" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
Spring +Axis2 集成开发就OK了(STA)