集成Spring和Axis

  集成Spring Framework和Axis的一个简单方案,使Axis Web服务引擎可以使用Spring的IoC容器和AOP技术。要使用SpringProvider,Axis的server-config.wsdd配置例子如下:
        <service name="SpringTestService" provider="java:SPRING">
              <parameter name="beanName" value="springTest"/>
              <parameter name="allowedMethods" value="*"/>
        </service>
 
        配置Spring的applicationContext.xml如下:
        <beans>
              <bean id="springTest" class="org.samples.SpringAxisTest"/>
        </beans>
 
        以下是集成Spring Framework和Axis引擎的类结构图,可以使用同样的方法集成Axis和其他IoC容器(如PicoContainer)。
 

Click to view in original size 496 x 444

 

       以上类图中,紫色的是Spring Framework原有的类;草绿和天蓝色的类是Axis原有的类,其中草绿色的类需要做些修改;只有橙色的是需要开发的类。以下是SpringProvider类的代码片断:

public class SpringProvider extends RPCProvider {
      protected static Log log = LogFactory.getLog(SpringProvider.class.getName());

      public static final String OPTION_BEANNAME = "beanName";

      protected Object makeNewServiceObject(MessageContext msgContext, String clsName) throws Exception {
           String beanName = getStrOption(OPTION_BEANNAME, msgContext.getService());
           return getService(beanName, msgContext);
      }

      protected String getServiceClassNameOptionName() {
           return OPTION_BEANNAME;
      }

      protected Object getService(String beanName, MessageContext context) throws AxisFault {
           ApplicationContext appContext = getAppContext(context);
           if (appContext == null) {
                 log.fatal("Spring ApplicationContext is NULL.");
                 throw new AxisFault("get Spring ApplicationContext error.");
           }

           Object bean = appContext.getBean(beanName);
           if (bean == null) {
                 log.error("bean named:" + beanName + " is NULL");
                 throw new AxisFault("bean named:" + beanName + " is NULL");
           }
           return bean;
       }

       protected String getStrOption(String optionName, Handler service) {
            String value = null;
            if (service != null)
                  value = (String) service.getOption(optionName);
            if (value == null)
                  value = (String) getOption(optionName);
            return value;
       }

       protected Class getServiceClass(String beanName, SOAPService service, MessageContext msgContext) throws AxisFault {
            ConfigurableListableBeanFactory beanFactory = getBeanFactory(msgContext);
            if (beanFactory == null) {
                 log.error("BeanFactory is NULL");
                 throw new AxisFault("BeanFactory is NULL");
            }
            Object bean = beanFactory.getBeanDefinition(beanName);
            return bean.getClass();
        }

        protected XmlWebApplicationContext getAppContext(MessageContext msgContext) {
             HttpServlet servlet = (HttpServlet) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLET);
             ServletContext servletContext = servlet.getServletContext();
             XmlWebApplicationContext appContext = (XmlWebApplicationContext) servletContext
                     .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
             return appContext;
        }

        private final ConfigurableListableBeanFactory getBeanFactory(MessageContext msgContext) {
             XmlWebApplicationContext appContext = getAppContext(msgContext);
             return appContext.getBeanFactory();
        }
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页