POJO based on the Spring Framework#

service.xml#

service.xml 파일에는 두개의 서비스가 정의되어 있다. 여기서 WeatherSpringService는 실제 서비스를 담당하는 역할을 하고 SpringInit는 실제 스프링의 설정파일을 초기화하는 역할을 담당한다. SpringInit서비스에서 ServiceTCCL 이라는 파라미터는 Spring 클래스 로더로 하여금 웹서비스를 위해 사용하도록 만들어준다. ServiceTCCL 값은 composite이나 service가 될수 있다.

<serviceGroup>
  <service name="SpringInit"
    class="kr.or.openframework.spring.service.SpringInit">
    <description>This web service initializes Spring.</description>
    <parameter name="ServiceClass">
      kr.or.openframework.spring.service.SpringInit
    </parameter>
    <parameter name="ServiceTCCL">composite</parameter>
    <parameter name="load-on-startup">true</parameter>
    <operation name="springInit">
      <messageReceiver
        class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
    </operation>
  </service>
  <service name="WeatherSpringService">
    <description>
      Weather Spring POJO Axis2 AAR deployment
    </description>
    <parameter name="ServiceClass">
      kr.or.openframework.spring.service.WeatherSpringService
    </parameter>
    <parameter name="ServiceObjectSupplier">
      org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
    </parameter>
    <parameter name="SpringBeanName">
      weatherSpringService
    </parameter>
    <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>
</serviceGroup>

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="weatherSpringService" class="kr.or.openframework.spring.service.WeatherSpringService">
    <property name="weather" ref="weatherBean"/>
  </bean>

  <bean id="weatherBean" class="kr.or.openframework.spring.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>
</beans>

SpringInit.java#

package kr.or.openframework.spring.service;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.description.AxisService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SpringInit implements ServiceLifeCycle {
        
    private static Log logger = LogFactory
        .getLog(SpringInit .class);

    // The web service
    public OMElement springInit(OMElement ignore) {

        return null;
    }

    public void init(ServiceContext serviceContext) {
        
    }

    public void setOperationContext(OperationContext arg0) {

    }

    public void destroy(ServiceContext arg0) {

    }

     /**
     * 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) {
        ClassLoader classLoader = service.getClassLoader();
        ClassPathXmlApplicationContext appCtx = new
            ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}false);
        appCtx.setClassLoader(classLoader);
        appCtx.refresh();
        if (logger.isDebugEnabled()) {
            logger.debug("\n\nstartUp() set spring classloader via axisService.getClassLoader() ... ");
        }
    }
    /**
     * this will be called during the deployement time of the service. irrespective
     * of the service scope this method will be called
     */
    public void shutDown(ConfigurationContext ignore, AxisService service) {
        
    }
}

WeatherSpringService#

package kr.or.openframework.spring.service;

import kr.or.openframework.spring.bean.Weather;

public class WeatherSpringService{
    Weather weather;
    
    public void setWeather(Weather w){
        weather = w;
    }

    public Weather getWeather(){
        return weather;
    }
}

Weather.java#

package kr.or.openframework.spring.bean;

public class Weather{
    float temperature;
    String forecast;
    boolean rain;
    float howMuchRain;
    
    public void setTemperature(float temp){
        temperature = temp;
    }

    public float getTemperature(){
        return temperature;
    }
    
    public void setForecast(String fore){
        forecast = fore;
    }

    public String getForecast(){
        return forecast;
    }
    
    public void setRain(boolean r){
        rain = r;
    }

    public boolean getRain(){
        return rain;
    }
    
    public void setHowMuchRain(float howMuch){
        howMuchRain = howMuch;
    }

    public float getHowMuchRain(){
        return howMuchRain;
    }
}

WeatherSpringRPCClient.java#

package client;

import javax.xml.namespace.QName;

import kr.or.openframework.spring.bean.Weather;

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 WeatherSpringRPCClient {

  @SuppressWarnings("unchecked")
  public static void main(String[] args1throws AxisFault {

    RPCServiceClient serviceClient = new RPCServiceClient();
    Options options = serviceClient.getOptions();
    EndpointReference targetEPR = 
      new EndpointReference("http://localhost:8080/axis2/services/WeatherSpringService");
    options.setTo(targetEPR);

    // Get the weather (no setting, the Spring Framework has already initialized it for us)
    QName opGetWeather = new QName("http://bean.spring.openframework.or.kr/xsd""getWeather");
    Object[] opGetWeatherArgs = new Object[] {};
    Class[] returnTypes = new Class[] { Weather.class };
    Object[] response = serviceClient.invokeBlocking(opGetWeather, opGetWeatherArgs, returnTypes);
    Weather result = (Weatherresponse[0];

    // display results
    if (result == null) {
      System.out.println("Weather didn't initialize!");
    else {
      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());
    }
  }
}

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-3) was last changed on 04-Aug-2007 21:56 by DongGukLee