首页 > 学院 > 开发设计 > 正文

axis2+spring集成

2019-11-14 21:22:02
字体:
来源:转载
供稿:网友
axis2+sPRing集成转载自:http://www.VEVb.com/linjiqin/archive/2011/07/05/2098316.html1、新建一个web project项目,最终工程目录如下:

注意:本文只注重webservice服务器端的开发,因此com.ljq.client和com.ljq.test忽略不计

2、添加所需jar

精简包

3、接口HelloWorld

package com.ljq.service;public interface HelloWorld {    public String greeting(String name);    public String print();}

4、接口实现类HelloWorldBean

复制代码
package com.ljq.service;public class HelloWorldBean implements HelloWorld {    public String greeting(String name) {        return "你好 "+name;    }    public String print() {        return "我叫林计钦";    }}
复制代码

5、webservice类HelloWorldWebService

复制代码
package com.ljq.service;import org.apache.axis2.AxisFault;import org.apache.axis2.ServiceObjectSupplier;import org.apache.axis2.description.AxisService;import org.apache.axis2.description.Parameter;import org.apache.axis2.i18n.Messages;import org.springframework.beans.BeansException;import org.springframework.context.applicationContext;import org.springframework.context.ApplicationContextAware;/** * 可能出现Axis2 spring bean not found 或者 Spring applicationContext not found。 *  * 解决办法:构建自己的ServiceObjectSupplier,实现接口ServiceObjectSupplier,同时也实现Spring的ApplicationContextAware接口 *  *  * @author Administrator *  */public class HelloWorldWebService implements ServiceObjectSupplier,        ApplicationContextAware {    private static ApplicationContext ctx;    public Object getServiceObject(AxisService axisService) throws AxisFault {        Parameter springBeanName = axisService.getParameter("SpringBeanName");        String beanName = ((String) springBeanName.getValue()).trim();        if (beanName != null) {            if (ctx == null)                throw new AxisFault("applicationContext is NULL! ");            if (ctx.getBean(beanName) == null)                throw new AxisFault("Axis2 Can't find Spring Bean: " + beanName);            return ctx.getBean(beanName);        } else {            throw new AxisFault(Messages.getMessage("paramIsNotSpecified",                    "SERVICE_SPRING_BEANNAME"));        }    }    public void setApplicationContext(ApplicationContext ctx)            throws BeansException {        this.ctx = ctx;    }}
复制代码

6、配置web.xml文件

复制代码
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <!-- 添加spring监听器 -->    <listener>        <listener-class>            org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>    <!-- 加载spring的配置文件 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/applicationContext.xml</param-value>    </context-param>        <!-- 注册axis2的servlet -->    <servlet>        <servlet-name>AxisServlet</servlet-name>        <servlet-class>            org.apache.axis2.transport.http.AxisServlet        </servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>AxisServlet</servlet-name>        <url-pattern>/services/*</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>
复制代码

7、在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>    <bean id="applicationContext"        class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />    <bean id="helloWorld" class="com.ljq.service.HelloWorldBean"></bean></beans>
复制代码

8、在WEB-INF/services/axis/META-INF/目录下配置services.xml(不存在则自己创建)

复制代码
<?xml version="1.0" encoding="UTF-8"?><service name="hwWebService">    <description>axis2与spring集成案例</description>    <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->    <parameter name="ServiceObjectSupplier">        org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier    </parameter>    <!--        SpringBeanName固定的不能改       helloWorld是spring中注册的实现类得id     -->    <parameter name="SpringBeanName">helloWorld</parameter>    <!--     在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。    例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,    而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。     -->    <messageReceivers>        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"            class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />    </messageReceivers></service>
复制代码

axis2+spring集成到此已经开发完成,接下把工程部署到tomcat,

然后通过http://localhost:8083/axis2spring/services/hwWebService?wsdl访问


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表