主要介绍 axis2 接口在 spring 项目中的整合配置。

使用jar包:axis2-1.6.2、spring2.5.6

目录结构:

关键代码:

package com.alfred.bean;

public class PersonBean {

	private String username;

	public PersonBean() {
} public PersonBean(String username) {
this.username = username;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String say(String str) {
return username + " say:" + str;
}
}

PersonBean.java

package com.alfred.service;

import org.apache.axis2.AxisFault;

import com.alfred.bean.PersonBean;

public class SoapService {

	private PersonBean personBean;

	public String sayHello(String username) throws AxisFault {
personBean.setUsername(username);
return personBean.say("hello");
} public String saySorry() {
return personBean.say("sorry");
} public PersonBean getPersonBean() {
return personBean;
} public void setPersonBean(PersonBean personBean) {
this.personBean = personBean;
} }

SoapService.java

package com.alfred.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 alfred
*
*/
public class SoapServiceSupplier implements ServiceObjectSupplier,
ApplicationContextAware { private static ApplicationContext ctx; public Object getServiceObject(AxisService axisService) throws AxisFault {
System.out.println("in getServiceObject");
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;
}
}

SoapServiceSupplier.java

package com.alfred.client;

import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient; public class ServiceClient {
public static void main(String args[]) throws AxisFault {
sendAxis2();
} /**
* 发送axis2的接口信息
* @throws AxisFault
*/
private static void sendAxis2() throws AxisFault {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://127.0.0.1:8080/aws/services/mySoapService");
options.setTo(targetEPR);
// 指定sayHello方法的参数值,如果有多个,继续往后面增加即可
Object[] opAddEntryArgs = new Object[] { "alfred" };
// 指定sayHello方法返回值的数据类型的Class对象
Class[] classes = new Class[] { String.class };
// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL,文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
// 第二个参数是要调用的方法名
QName opAddEntry = new QName("http://service.alfred.com", "saySorry");
// 返回参数类型,这个和axis1有点区别
// invokeBlocking方法有三个参数:
// 第一个参数的类型是QName对象,表示要调用的方法名;
// 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
// 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[];
// 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}
// 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
// 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
Object ret = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,
classes)[0];
System.out.println(ret);
} }

ServiceClient.java

<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean class="com.alfred.service.SoapServiceSupplier"></bean>
<bean id="soapService" class="com.alfred.service.SoapService">
<property name="personBean" ref="personBean" />
</bean>
<bean id="personBean" class="com.alfred.bean.PersonBean">
<!-- 设置username的初始值 -->
<property name="username">
<value>john</value>
</property>
</bean>
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<!-- 可以指定发布多个service -->
<service name="mySoapService">
<description>axis2与spring集成案例</description>
<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
<!-- com.alfred.service.SoapServiceSupplier -->
</parameter>
<!--
SpringBeanName固定的不能改
soapService是spring中注册的实现类得id
-->
<parameter name="SpringBeanName">soapService</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>

services.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"> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<description>配置spring读取的xml文件,param-name是spring规定,spring自动加载以下内容</description>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>
<!--Axis2 config start-->
<display-name>Apache-Axis2</display-name>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<!--<init-param>-->
<!--<param-name>axis2.xml.path</param-name>-->
<!--<param-value>/WEB-INF/conf/axis2.xml</param-value>-->
<!--<param-name>axis2.xml.url</param-name>-->
<!--<param-value>http://localhost/myrepo/axis2.xml</param-value>-->
<!--<param-name>axis2.repository.path</param-name>-->
<!--<param-value>/WEB-INF</param-value>-->
<!--<param-name>axis2.repository.url</param-name>-->
<!--<param-value>http://localhost/myrepo</param-value>-->
<!--</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>AxisAdminServlet</servlet-name>
<servlet-class>
org.apache.axis2.webapp.AxisAdminServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<!--Axis2 end--> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

web.xml

spring 与 axis2 集成的关键就是 services.xml 文件中由原先的接口类地址指定改为注入 spring 的接口类对象。

Axis2之Spring装配的更多相关文章

  1. spring装配---处理自动装配的歧义性

    一.歧义性 当我们使用spring的注解进行自动装配bean时,如果不仅有一个bean能够匹配结果的话,会抛出NoUniqueBeanDefinitionException: 例如本例中 当sprin ...

  2. Spring 装配Bean

    Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介 ...

  3. Spring装配bean

    Spring配置的可选方案 Spring提供了如下三种装配机制: (1)在XML中显式配置 (2)在Java中显式配置 (3)隐式的bean发现机制和自动装配 Spring有多种方式可以装配bean, ...

  4. Spring装配Bean之XML装配bean

    在Spring刚出现的时候,XML是描述配置的主要方式,在Spring的名义下,我们创建了无数行XML代码.在一定程度上,Spring成为了XML的同义词. 现在随着强大的自动化配置和Java代码的配 ...

  5. JAVA入门[13]-Spring装配Bean

    一.概要 Sping装配bean主要有三种装配机制: 在XML中进行显式配置. 在Java中进行显式配置. 隐式的bean发现机制和自动装配. 原则: 建议尽可能地使用自动配置的机制,显式配置越少越好 ...

  6. AXIS2整合spring需要的jar,以及大部分缺少jar所报的异常

    axis2 webservice 服务端jar包: -->axis2-kernel-1.6.1.jar                              -->axis2-spri ...

  7. spring/java ---->记录和整理用过的注解以及spring装配bean方式

    spring注解 @Scope:该注解全限定名称是:org.springframework.context.annotation.Scope.@Scope指定Spring容器如何创建Bean的实例,S ...

  8. spring装配注解(IOC容器加载控制)ComponentScan及ComponentScans使用

    ComponentScan,只写入value,可扫描路径下装配的@Contrller.@Service.@Repository @ComponentScan(value = "com.tes ...

  9. Spring装配Bean的过程补充

    对上一篇的<Spring装配Bean的过程>的过程说一下,不然真产生了误区. 误区在哪里呢?那就是spring bean的作用域问题. 说哈常用的两种作用域:默认是scope = sing ...

随机推荐

  1. EXPLAIN执行计划中要重点关注哪些要素(叶金荣)

    原文:http://mp.weixin.qq.com/s/CDKN_nPcIjzA_U5-xwAE5w 导读 EXPLAIN的结果中,有哪些关键信息值得注意呢? MySQL的EXPLAIN当然和ORA ...

  2. 4.0-uC/OS-III目录结构

    本文章都是基于学习野火STMF4系列的开发板的学习做的,大部分都是开发手册的内容,做笔记用,具体请参考野火官方的开发手册. 1. uC/OS-III 文件结构 ①配置文件,通过定义这些文件里宏的值可以 ...

  3. 【SQL】SQL存储过程相关当前理解。(@temp=……)

    1.下图左侧红框中的是SQL的存储过程,是程序同SQL之间互相调用的函数.——这里先作为了解不做深入研究. 比如客户通过ATM提款机取200元钱,程序中提交取200元钱操作,后续可能会进入SQL进行一 ...

  4. 数据库——MongoDB增删改查

    MongoDB增删改查操作 本文包含对数据库.集合以及文档的基本增删改查操作 数据库操作 #1.增 use config #如果数据库不存在,则创建并切换到该数据库,存在则直接切换到指定数据库. #2 ...

  5. what's the python之变量、基本数据类型

    what's the 变量? Python 中的变量赋值不需要类型声明. 变量在内存中创建,包括变量的标识,名称和数据. 变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 等号(=)用来给变量赋 ...

  6. Vuex 页面刷新后store保存的数据会丢失 取cookie值

    在store.js中 export default new vuex.Store({ // 首先声明一个状态 state state:{ pcid: '', postList: [], } //更新状 ...

  7. Linux用户群组权限恢复

    /etc/passwd:该文件用于存放用户详细信息:例如 root:x:0:0:root:/root:/bin/bash 用户id 0:就表示root用户 bin下的bash:表示可以登入操作系统 s ...

  8. pyc文件是什么【转载】

    转自:https://blog.51cto.com/bella41981/2045108 1.概念 pyc文件是py文件编译后生成的字节码文件(byte code).pyc文件经过python解释器最 ...

  9. mac上命令行解压rar

    时间进入到2018年12月,mac上好用的rar解压工具要收费了.被逼的没办法,用命令行吧,谁让咱擅长呢? 1,使用Homebrew安装unrar,没有自己装去 brew install unrar ...

  10. linux 执行远程linux上的shell脚本或者命令以及scp 上传文件到ftp--免密码登陆

    场景:在linux A 上执行Linux B上的shell脚本和命令 步骤1.设置ssh免登陆 1.SSH无密码登录 # 本地服务器执行(A机器):生成密钥对 ssh-keygen -t dsa -P ...