axis2系列之与spring集成

来源:互联网 发布:查找两列不同数据 编辑:程序博客网 时间:2024/04/25 20:40

Spring与Axis2集成注意事项以及步骤:

  1. spring使用配置文件进行实体bean的注入方式不变,applicationContext.xml文件内容也不变只是需要将applicationContext.xml文件放在WEB-INF下。
  2. 在web.xml文件中加入spring的监听器,以及applicationcontext.xml配置文件的读取。
  3. 在spring配置文件中加入:<bean id="applicationContext"     class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
  4. 重点:在WEB-INF/services文件下创建任一文件夹(名字随便,但必须创建),在该新创建的文件夹下创建一个文件夹名为META-INF文件夹,在META-INF文件夹下创建services.xml文件。
  5. 配置services.xml,在配置该文件中需要注意三个parameter的配置:
(1)、ServiceObjectSupplier的配置,固定值:org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
在 services.xml 文件中需要通过 ServiceObjectSupplier 参数指定 SpringServletContextObjectSupplier
类来获得 Spring 的 ApplicationContext 对象
(2)、SpringBeanName的配置:applicationContext.xml中指定bean的id。

服务器端:
public interface IWeatherService {void setWeather(Weather w);Weather getWeather();}
public class WeatherService implements IWeatherService {Weather weather;public void setWeather(Weather w) {weather = w;}public Weather getWeather() {return weather;}}

services.xml文件
<service name="springService"><description>springService</description><parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter><parameter name="SpringBeanName">weatherService</parameter><messageReceivers><messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /></messageReceivers></service>
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="weatherService" class="com.test.springservice.impl.WeatherService">        <property name="weather">            <bean class="com.test.bean.Weather">                <property name="temperature" value="89.9" />                <property name="forecast" value="Sunny" />                <property name="rain" value="false" />                <property name="howMuchRain" value="0.2" />            </bean>        </property>    </bean></beans>
客户端:
public static void main(String[] args1) throws AxisFault {EndpointReference targetEPR = new EndpointReference("http://localhost:8080/springAxis2project/services/springService");RPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();options.setTo(targetEPR);QName opGetWeather = new QName("http://impl.springservice.test.com","getWeather");Object[] opGetWeatherArgs = new Object[] {};Class[] returnTypes = new Class[] { Weather.class };Object[] response = serviceClient.invokeBlocking(opGetWeather,opGetWeatherArgs, returnTypes);Weather result = (Weather) response[0];if (result == null) {System.out.println("Weather didn't initialize!");} else {System.out.println();System.out.println("Temperature               : "+ result.getTemperature());System.out.println("Forecast                  : "+ result.getForecast());System.out.println("Rain                      : "+ result.getRain());System.out.println("How much rain (in inches) : "+ result.getHowMuchRain());}}





0 0
原创粉丝点击