axis2+spring集成
axis2+spring集成
1、新建一个web project项目,最终工程目录如下:
注意:本文只注重webservice服务器端的开发,因此com.ljq.client和com.ljq.test忽略不计
2、添加所需jar
3、接口HelloWorld
package com.ljq.service;
publicinterface HelloWorld {
public String greeting(String name);
public String print();
}
publicinterface HelloWorld {
public String greeting(String name);
public String print();
}
4、接口实现类HelloWorldBean
package com.ljq.service;
publicclass HelloWorldBean implements HelloWorld {
public String greeting(String name) {
return"你好 "+name;
}
public String print() {
return"我叫林计钦";
}
}
publicclass 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
*
*/
publicclass HelloWorldWebService implements ServiceObjectSupplier,
ApplicationContextAware {
privatestatic 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)
thrownew AxisFault("applicationContext is NULL! ");
if (ctx.getBean(beanName) ==null)
thrownew AxisFault("Axis2 Can't find Spring Bean: "+ beanName);
return ctx.getBean(beanName);
} else {
thrownew AxisFault(Messages.getMessage("paramIsNotSpecified",
"SERVICE_SPRING_BEANNAME"));
}
}
publicvoid setApplicationContext(ApplicationContext ctx)
throws BeansException {
this.ctx = ctx;
}
}
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
*
*/
publicclass HelloWorldWebService implements ServiceObjectSupplier,
ApplicationContextAware {
privatestatic 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)
thrownew AxisFault("applicationContext is NULL! ");
if (ctx.getBean(beanName) ==null)
thrownew AxisFault("Axis2 Can't find Spring Bean: "+ beanName);
return ctx.getBean(beanName);
} else {
thrownew AxisFault(Messages.getMessage("paramIsNotSpecified",
"SERVICE_SPRING_BEANNAME"));
}
}
publicvoid 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>
<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>
<!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>
<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访问
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25