- 浏览: 173133 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (186)
- Ant (11)
- Axis2 (15)
- Car (9)
- Eclipse (1)
- Java (19)
- Java-EM (4)
- Javascript (11)
- Jsp (1)
- Hibernate (9)
- Mysql (1)
- Ms-Dos (5)
- Music (0)
- Oracle (3)
- Postgresql (0)
- Photoshop (1)
- Spring (17)
- Struts (8)
- Selenium (5)
- Ubuntu (13)
- News (17)
- Others (7)
- SSH (11)
- 算法 (5)
- FreeMarker (4)
- Tomcat (2)
- Linux (5)
最新评论
Axis2 Integration With The Spring Framework
This document is a guide on how to use Axis2 with the Spring Framework
Introduction
The idea behind Axis2 and Spring integration is that Axis2 simply needs to have Spring supply one of its pre-loaded beans to the Axis2 Message Receiver defined in the AAR services.xml . While Axis2 typically uses reflection to instantiate the ServiceClass defined in the services.xml that the Message Receiver will use, alternatively one can define a ServiceObjectSupplier that will supply the Object.
This guide will show how to use two separate ServiceObjectSupplier classes that are part of the Axis2 standard distribution: One for use with a ServletContext, and one without. Once configured, the web service itself acts like any other Spring wired bean. These Spring beans can be loaded any way desired, as Axis2 has no configuration file dependencies from Spring. Spring versions 1.2.6, 1.2.8 and 2.0 have been tested, but probably any version would work as only core functionality is required.
This guide assumes some basic knowledge of Axis2. See the User's Guide for more information.
Programming Model
From an Axis2 standpoint, two hooks are needed to be placed into the AAR services.xml: The ServiceObjectSupplier that hooks Axis2 and Spring together, and the name of Spring bean that Axis2 will use as the service. All Message Receivers are currently supported, as would be any Message Receiver that extends org.apache.axis2.receivers.AbstractMessageReceiver .
Simple Spring Config Example
For the purpose of this example, and for no other reason besides simplicity, we'll configure Spring via a WAR file's web.xml. Lets add a context-param and a listener:
<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>
Next we will show two examples of Spring's /WEB-INF/applicationContext.xml referenced in the web.xml listener - one using a ServletContext, and one without.
With a ServletContext
This 'with a ServletContext' example applicationContext.xml should be familiar to any Spring user:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Axis2 Web Service, but to Spring, its just another bean that has dependencies -->
<bean id="springAwareService" class="spring.SpringAwareService">
<property name="myBean" ref="myBean"/>
</bean>
<!-- just another bean / interface with a wired implementation, that's injected by Spring
into the Web Service -->
<bean id="myBean" class="spring.MyBeanImpl">
<property name="val" value="Spring, emerge thyself" />
</bean>
</beans>
If the service is running in a Servlet Container, i.e., Axis2 will be able to get a hold of ServletContext, the services.xml for the example would be using SpringServletContextObjectSupplier such as:
<service name="SpringAwareService">
<description>
simple spring example
</description>
<parameter name="ServiceObjectSupplier" locked="false">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
<parameter name="SpringBeanName" locked="false">springAwareService</parameter>
<operation name="getValue">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
While the above example uses RawXMLINOutMessageReceiver as its messageReceiver class, all Message Receivers are currently supported, as would be any Message Receiver that extends org.apache.axis2.receivers.AbstractMessageReceiver .
Without a ServletContext
In the case Axis2 can't get a ServletContext, ie another transport or running inside the AAR etc, you have the option of defining a bean that takes advantage of Spring's internal abilities (ApplicationContextAware interface, specifically) to provide an Application Context to Axis2, with a bean ref 'applicationContext' :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Configure spring to give a hook to axis2 without a ServletContext -->
<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<!-- Axis2 Web Service, but to Spring, its just another bean that has dependencies -->
<bean id="springAwareService"
class="spring.SpringAwareService">
<property name="myBean" ref="myBean" />
</bean>
<!-- just another bean with a wired implementation, that's injected by Spring
into the Web Service -->
<bean id="myBean"
class="spring.MyBeanImpl">
<property name="val" value="Spring, emerge thyself" />
</bean>
</beans>
If the service is _NOT_ running in a Servlet Container, i.e., Axis2 will _NOT_ be able to get a hold of ServletContext or you prefer not to, the services.xml for the example would be using SpringAppContextAwareObjectSupplier such as:
<service name="SpringAwareService">
<description>
simple spring example
</description>
<parameter name="ServiceObjectSupplier" locked="false">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>
<parameter name="SpringBeanName" locked="false">springAwareService</parameter>
<operation name="getValue">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
While the above example uses RawXMLINOutMessageReceiver as its messageReceiver class, all Message Receivers are currently supported, as would be any Message Receiver that extends org.apache.axis2.receivers.AbstractMessageReceiver .
In a 'without a ServletContext' environment, one way you could load the applicationContext.xml file is in a place that will be run once, upon start-up, execute:
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();
Putting It All Together
From here, its just standard Axis2 coding, only now the service has Spring wiring capabilities. The implementation is the same whether using either SpringServletContextObjectSupplier or SpringAppContextAwareObjectSupplier. The service is below:
package spring;
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;
}
}
For those new to Spring, one of the ideas is that you program to an Interface, and the implementation is pluggable. This idea is referenced in the Spring config file above. Below is the interface:
package spring;
/** Interface for Spring aware Bean */
public interface MyBean {
String emerge();
}
Here's the implementation:
/** 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;
}
}
Lastly here's the client - not really necessary for the example, other than for completeness:
package client;
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();
}
}
}
The examples above assumes that both the spring framework jar and the axis2-spring-*.jar are under WEB-INF/lib. In such a case, the classes shown in this tutorial need to be placed in a JAR under WEB-INF/lib. In this example the JAR layout is:
./mySpring.jar
./META-INF
./META-INF/MANIFEST.MF
./spring
./spring/MyBean.class
./spring/MyBeanImpl.class
./spring/SpringAwareService.class
Since all the user classes are in mySpring.jar in this example, the AAR merely contains the services.xml file:
./springExample.aar
./META-INF
./META-INF/MANIFEST.MF
./META-INF/services.xml
To run this example, make sure you have the axis2-spring*.jar that comes from the axis2-std-*-bin distro in the server side WEB-INF/lib, as well as the appropriate Spring jar - most will use the full spring.jar, but the minimum requirements are spring-core, spring-beans, spring-context and spring-web. Running the client, you should see this output:
Response: <example1:string xmlns:example1="http://springExample.org/example1"
xmlns:tns="http://ws.apache.org/axis2">Spring, emerge thyself</example1:string>
Spring Inside an AAR
Frequently Axis2 users wish to run Spring inside the AAR. Here we show you how. There are four points to be aware of:
(A) You need to configure Spring to use the Axis2 Service Classloader. See the Known issues running Spring inside the AAR area.
(B) Its up to you to load Spring, though we give an example below.
(C) For reasons such as classloader isolation the SpringAppContextAwareObjectSupplier is the best choice.
(D) The springframework jar and axis2-spring-*.jar will be placed inside the AAR under the lib directory. Please MOVE the axis2-spring-*.jar from WEB-INF/lib to inside the AAR, as shown below - it will NOT work otherwise.
./springExample.aar
./META-INF
./META-INF/MANIFEST.MF
./META-INF/services.xml
./applicationContext.xml
./lib
./lib/axis2-spring-SNAPSHOT.jar
./lib/spring.jar
./spring
./spring/MyBean.class
./spring/MyBeanImpl.class
./spring/SpringAwareService.class
./spring/SpringInit.class
As explained in the Without a ServletContext section, likewise the 'Spring inside an AAR' config needs to hook Axis2 and Spring together via a Spring bean. Place the following in your Spring config file:
<!-- Configure spring to give a hook to axis2 without a ServletContext -->
<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
One way to initialize Spring inside the AAR is to use the org.apache.axis2.engine.ServiceLifeCycle interface. Here we give an example:
package spring;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringInit implements ServiceLifeCycle {
/**
* This will be called during the deployement time of the service.
* irrespective
* of the service scope this method will be called
*/
public void startUp(ConfigurationContext ignore, AxisService service) {
try {
System.out.println("Starting spring init");
ClassLoader classLoader = service.getClassLoader();
ClassPathXmlApplicationContext appCtx = new
ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}, false);
appCtx.setClassLoader(classLoader);
appCtx.refresh();
System.out.println("spring loaded");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* This will be called during the system shut down time.
* irrespective
* of the service scope this method will be called
*/
public void shutDown(ConfigurationContext ctxIgnore, AxisService ignore) {
}
}
Here's the services.xml that now includes SpringInit and the SpringAwareService shown above. There is also the composite parameter which is needed when loading Spring in the AAR - see the Known issues running Spring inside the AAR area.
<serviceGroup>
<!-- Invoke SpringInit on server startup and shutdown -->
<service name="SpringAwareService" class="spring.SpringInit">
<description>
simple spring example - inside the AAR
</description>
<!-- need the TCCL param when using spring inside the AAR -->
<parameter name="ServiceTCCL" locked="false">composite</parameter>
<parameter name="ServiceObjectSupplier" locked="false">org.apache.axis2.extensions.spring.rec ...
发表评论
-
Spring Web Service 学习之Hello World篇2
2008-11-03 15:32 10306, 修改配置文件spring-ws- ... -
webservice之axis2方式开发总结
2008-11-04 09:33 691webservice之axis2方式开发总结 关键字: we ... -
基于Axis2开发WebService代码详解
2008-11-04 09:34 884基于Axis2开发WebService代码详解 关键字: we ... -
基于Tomcat5.0和Axis2开发Web Service应用实例
2008-11-04 09:38 735基于Tomcat5.0和Axis2开发Web Service应 ... -
使用Axis2来构建Web Service客户端
2008-11-04 09:46 728使用Axis2来构建Web Service客户端 2 ... -
webservice-之使用axis+spring开发
2008-11-04 17:42 601... -
webservice-之使用axis+spring开发2
2008-11-04 17:42 769三、配置文件 (全部放在 ... -
Axis 开发WebService
2008-11-04 18:16 694Axis 开发WebService Axis 开发WebSe ... -
spring与axis的整合
2008-11-04 18:23 669spring与axis的整合 eclipse resin ax ... -
在Eclipse中创建基于Axis2的web services
2008-11-05 09:04 1002本实验的目的是让你尽可能快的创建使用 Axis2 的服务和客户 ... -
Axis2快速上手指南
2008-11-05 09:06 685本指南的目的是让你尽可能快的创建使用Axis2的服务和客户端, ... -
Axis2快速上手指南2
2008-11-05 09:07 662创建服务 在这个部分,我们将看看根据StockQuoteSe ... -
Axis2快速上手指南4
2008-11-05 09:08 824使用ADB生成一个客户端 执行以下步骤来使用Axis Dat ... -
定义web service接口的十点注意事项
2008-11-05 14:03 1206一、接口是自说明的。 也就是说,接口的名字、参数和返回值在一看 ...
相关推荐
简单例子:axis2整合spring发布webservice,以及session的管理
axis2与spring的集成,在application中配置要发布的Java类,然后配置aar文件,在aar打包文件中的services.xml要嵌入 <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers....
使用axis2与spring3进行集成,实现的webservice服务。 资料为myeclipse下的web工程,可解压后直接导入。 发布到tomcat6下运行。
本示例是在 Axis2 通过 外部 Spring容器 获取指定的实现Bean,以提供服务。全部自己写WSDL和服务实现类,不使用 Axis2提供的 Java2WSDL 和 WSDL2Java 工具
在hibernate spring项目基础上通过aixs2-1.4.1把程序发布成webService,包括aixs2以对象数组和AXIOM方式处理map,list的程序代码;和aixs2-1.4-1的jar包
spring3.2.5与axis2 1.6.2整合
axis2 和 spring3.2.0 的包。 这里http://blog.csdn.net/linlinv3/article/details/9017767详细讲解了 axis2 的用法
axis2和axis2+spring发布服务指南
axis2-spring-1.5.4.jar是axis2与spring整合时所需要的jar包
Axis2集成Spring发布WebServices服务
spring-framework-3.0.5.RELEASE-dependencies 好不容易找到了,赶紧分享一下 因为不能大于20M,共分了8个包,都是独立的,我列了目录,可以只下载需要的包,这是1号包: 1号包: edu.emory.mathcs.backport edu.oswego.cs....
axis2+spring2.5整合(webservice)
spring+axis2整合,项目下载之后就能使用,里面简单的编写的发布服务的代码,适合初学者使用
Axis2WebService与Spring的整合
Spring集成AXIS2的Web_service配置方法
Chapter 13, Enterprise Integration Patterns - Learn about some enterprise SOA deployment patterns that make use of the underlying Axis2 clustering infrastructure. Chapter 14, Axis2 Advanced Features ...
Spring整合axis2经典
springboot集成axis2-1.7.9部署和调用实例
axis2-idea-plugin-1.7.9
axis2+spring+ibatis整合的一个项目,含webservice文件传输