对于axis1.4来说,我们常用wsdd的方式来发布服务。下面先看下我们是如何使用的。
定义一个服务类
package com.sunsharing.axistest; public class HelloWordService { public String sayHello(String person){ return "hello ," + person; } }
使用wsdd暴露服务以及方法
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" /> <service name="helloWordService " provider="java:RPC"> <parameter name="className" value="com.sunsharing.axistest.HelloWordService " /> </service> <transport name="http"> <requestFlow> <handler type="URLMapper" /> </requestFlow> </transport> </deployment>
这已经把服务暴露出来了。
接下来,妹子可能会问你,能不能从我传入的person找到它存储在数据库的详细信息,多输出点信息呢?于是乎,你又定义了一个spring的service,就简单的jdbcTemplate来查询想满足妹子吧。
@Service public class SpringHelloWordService { @Autowired JdbcTemplate jdbcTemplate; public String sayHello(String person){ //这个打印为了看是不是spring托管的service,是的话,jdbcTemplate应该不为空,有注入 System.out.println(jdbcTemplate); return "hello,"+person; } }
继续之前的做法,往wsdd再配置个service
<service name="springHelloWordService" provider="java:RPC"> <parameter name="className" value="com.sunsharing.axistest.SpringHelloWordService" /> </service>
呵呵,这样做的话,你的jdbcTemplate肯定是空指针!!!,这个className是axis自己new出来的。你还是再想想办法吧。
这时候,你一想,那我就写在之前定义好的HelloWordService,进行调用spring的服务不就好了。修改下HelloWordService。
public class HelloWordService { public String sayHello(String person){ //ServiceLocator是我们系统初始自定义的获取spring bean的辅助类 SpringHelloWordService service = (SpringHelloWordService)ServiceLocator.getBean("springHelloWordService"); return service.sayHello(person); } }
好像一切都做完了,妹子的要求也满足了。不过对于喜欢偷懒的程序猿来说,总觉得好累,为什么不直接把spring的service直接暴露出来就好了,还多这么一层,真是累啊~~~
稍微看了下源码,wsdd的方式部署中,有个provider。再看看,就从它入手,定义一个SpringProvider(网上copy的,修改了下getServiceClass)
package com.sunsharing.axistest; import org.apache.axis.AxisFault; import org.apache.axis.Handler; import org.apache.axis.MessageContext; import org.apache.axis.components.logger.LogFactory; import org.apache.axis.handlers.soap.SOAPService; import org.apache.axis.providers.java.RPCProvider; import org.apache.axis.transport.http.HTTPConstants; import org.apache.commons.logging.Log; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; 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(); Object bean = getService(beanName,msgContext); 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(); } }
再定义一个wsdd的provider,WSDDJavaSpringProvider
public class WSDDJavaSpringProvider extends WSDDProvider { public static final String PROVIDER_RPC = "SPRING"; public static final QName QNAME = new QName(WSDDConstants.URI_WSDD_JAVA, PROVIDER_RPC); /** * @param service * @param registry XXX * @return XXX * @throws Exception XXX */ @Override public Handler newProviderInstance(WSDDService service, EngineConfiguration registry) throws Exception { return new SpringProvider(); } @Override public String getName() { return PROVIDER_RPC; } }
wsdd再配置个service,请注意allowedMethods,可能你不想把spring service的所有方法暴露,那么可以在此配置声明要暴露出来的方法,用英文逗号隔开即可。
<service name="springHelloWordService" provider="java:SPRING"> <parameter name="beanName" value="springHelloWordService" /> <parameter name="allowedMethods" value="sayHello" /> </service>
还缺少一个重要的事情,就是注册provider,好的,就这么干,在系统启动时候加上注册(注意注册的方法一定要在AxisServlet初始化之前执行,load-on-startup调大点,下面是web.xml对axisServlet的配置和注册的语句)
<servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> <load-on-startup>6</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
WSDDProvider.registerProvider(WSDDJavaSpringProvider.QNAME,new WSDDJavaSpringProvider());
启动~~~访问看看吧~