您的位置:首页 > 编程语言 > Java开发

WebService从零到项目开发使用4—技术研究之Axis2 集成Spring框架

2013-03-22 18:08 579 查看

Axis2 集成Spring框架

1. 简介

Axis2整合Spring发生在:需要Spring将预先加载好的Bean实例提供给ARR包的server.xml中定义的消息接收器MessageReceiver。Axis2的消息接收器通常下会使用server.xml中定义的ServiceClass参数的Bean,作为可替换方案,我们通过在server.xml文件中定义ServiceObjectSupplier 参数值去提供Bean,实际上是Spring管理Axis2中的Service实例——由Axis2类加载器管理转为Spring类加载器管理(参照上一节)。

有两种方式使用ServiceObjectSupplier ,一种是将Axis2部署在Servlet容器中,另一种是Axis2作为单独服务发布器。前一种比较简单而流行,建议采用。

ServiceObjectSupplier 是将Axis2和Spring整合到一起的关键技术,Spring框架中的Bean将作为Axis2的发布的服务,由于各类Bean都要被消息接收器使用,因此目前所有实现org.apache.axis2.receivers.AbstractMessageReceiver的消息接收器都支持。

2. 配置Spring

在web.xml中配置spring监听器:

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

3. 将Axis2 war部署到tomcat中

4. 配置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>

<!—Spring简单Bean,但会作为Axis2 Web服务-->

<bean id="springAwareService" class="spring.SpringAwareService">

<property name="myBean" ref="myBean"/>

</bean>

<!—Spring另一个简单实现Bean, Spring将其注入到springAwareService,也就是我们所说的Axis2 Web服务-->

<bean id="myBean" class="spring.MyBeanImpl">

<property name="val" value="Spring, emerge thyself" />

</bean>

</beans>

(提示:如果Service运行在Servlet容器中,则Axis2能够拿到ServletContext。)

5. Servers.xml,使用SpringServletContextObjectSupplier

<service name="SpringAwareService">

<description>

simple spring example

</description>

<parameter name="ServiceObjectSupplier">

org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier

</parameter>

<parameter name="SpringBeanName">springAwareService</parameter>

<operation name="getValue">

<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>

</operation>

</service>

上面示例使用RawXMLINOutMessageReceiver作为该服务的消息接收器,

6. 如果使用单独的Axis2服务器则使用起来比较复杂,可参照Axis2官网文档。下面简单介绍下。

7. 如果使用单独的Axis2服务器,Axis2不能获得ServletContext,没有ServletContext则Service无法获取Spring上下文,因此Axis2提供了通过Spring框架拿到applicationContext的方法。在services.xml中定义SpringAppContextAwareObjectSupplier :

<parameter name="ServiceObjectSupplier">

org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier

</parameter>

下面这个最重要,注意了。

在没有ServletContext情况下,获取Spring application context的一种方法是:在你的Service方法(start-up)里运行一次加载applicationContext.xml、挂钩Spring类加载器的代码,如下:

import org.springframework.context.support.ClassPathXmlApplicationContext;

public void createSpringAppCtx(ClassLoader cl)

throws Exception {

ClassPathXmlApplicationContext ctx = new

ClassPathXmlApplicationContext(new String[] {Constants.MY_PATH +

"spring/applicationContext.xml"}, false);

ctx.setClassLoader(cl);

ctx.refresh();

}

8. 通过以上说明,我们明白了Axis2和Spring application context的各种联系方式,好了,接下来继续coding(占用下各位抽根烟的时间^_^)。

9. 无论使用SpringServletContextObjectSupplier 还是SpringAppContextAwareObjectSupplier ,我们的服务端的Service如下:(采用Axis2的AXIOM数据绑定,何为AXISM?问度娘)

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axiom.om.OMText;

public class SpringAwareService {

private MyBean myBean = null;

//spring 'injects' this implementation

public void setMyBean(MyBean myBean) {

this.myBean = myBean;

}

// The web service

public OMElement getValue(OMElement ignore) {

OMFactory factory=

OMAbstractFactory.getOMFactory();

OMNamespace payloadNs= factory.createOMNamespace(

"http://springExample.org/example1", "example1");

OMElement payload =

factory.createOMElement("string", payloadNs);

OMText response = factory.createOMText(this.myBean.emerge());

payload.addChild(response);

return payload;

}

}

10. 接下来我们编写另一个Bean,使用面向接口编程

/** Interface for Spring aware Bean */

public interface MyBean {

String emerge();

}

/** Spring wired implementation */

public class MyBeanImpl implements MyBean {

String str = null;

// spring 'injects' this value

public void setVal(String s) {

str = s;

}

// web service gets this value

public String emerge() {

return str;

}

}

11. 编写客户端调用测试类,采用阻塞模式(客户端—服务器同步通信,这种方式只适合纯客户端,就是永久的客户端,不会将客户端又去作为服务器,如SOA系统总会这么干,那时我们会采用异步双工会明显提高性能,暂不详说)。

import java.io.StringWriter;

import javax.xml.stream.XMLOutputFactory;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

public class TestClient {

private static EndpointReference targetEPR =

new EndpointReference(

"http://localhost:8080/axis2/services/SpringAwareService");

/**

* Simple axis2 client.

* @param args Main

*/

public static void main(String[] args) {

try {

OMFactory factory = OMAbstractFactory.getOMFactory();

OMNamespace omNs = factory.createOMNamespace(

"http://springExample.org/example1", "example1");

OMElement method = factory.createOMElement("getValue", omNs);

OMElement value = factory.createOMElement("Text", omNs);

value.addChild(factory.createOMText(value, "Some String "));

method.addChild(value);

ServiceClient serviceClient = new ServiceClient();

Options options = new Options();

serviceClient.setOptions(options);

options.setTo(targetEPR);

//Blocking invocation

OMElement result = serviceClient.sendReceive(method);

StringWriter writer = new StringWriter();

result.serialize(XMLOutputFactory.newInstance()

.createXMLStreamWriter(writer));

writer.flush();

System.out.println("Response: " + writer.toString());

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

接下来的那点儿发布和测试的事儿可参照上节,欢迎牛人介绍下自己的发布方式,更为项目节省时间。

12. 注意以上运行的Spring都是在AAR包外,如果想在AAR包内集成Spring jar包运行,则需要在services.xml等做额外的配置和开发,比如之前用到的:

<parameter name="ServiceTCCL">composite</parameter>

强制确保Spring的类加载器能应用到该Service。

同理可以将Hibernate放到AAR包中运行,需要了解Hibernate寻找HibernateContext的方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: