集成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)。





以上類圖中,紫色的是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();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章