一. spring 与web service annotation 整合
先看一段代码
@WebService
public class LoginDelegateWebService
{
@WebMethod
public Technician login(String id,String pwd) throws Exception {
return null;
}
}
spring 要与这个类整合起来,首先要在spring中注册这个LoginDelegateWebService类,暂时起名叫LoginDelegate, 接着就要新建另一个Web Service代理类来作为对外接口.
@WebService
public class LoginWebService extends SpringBeanAutowiringSupport
{
public static String BEAN_ID = "LoginDelegate";
@Autowired
@Qualifier("LoginDelegate")
private ILoginDelegate springServ;
@Resource
private WebServiceContext wsContext;
private void getSpringService() throws Exception {
if(springServ ==null ) {
//@Autowired does not work in JAX-WS web services on WebLogic server 10.3
//fall back to get the spring bean from WebApplicationContext
ServletContext servletContext= (ServletContext)wsContext.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
WebApplicationContext webContext = WebApplicationContextUtils.getWebApplicationContext( servletContext );
springServ = (ILoginDelegate)webContext.getBean(BEAN_ID);
if( springServ == null )
throw new Exception("Spring service not available: " + BEAN_ID);
}
}
@WebMethod
public Technician login(String id,String pwd) throws Exception {
getSpringService();
return springServ.login( id,pwd );
}
}
原先的LoginDelegateWebService 中的annotation要全部删除掉.至此,配置完成.
原理就是 这个LoginWebService是一个代理类,它接管了对外的接口,面LoginDelegatWebService这个类才是真正的业务处理类.所以之后我们可以对LoginDelegatWebService加一些AOP的操作,例如日志打印,程序监控,事务处理等等.
二.spring 与axis2整合
笔者使用的是AXIS2 1.6.2. 其实这个版本已经默认支持与SPRING整合.
只要我们在services.xml中作一下更改就好了.
<service name="tms/order" > <Description> Please Type your service description here </Description> <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> <!-- <parameter name="ServiceClass" locked="false">com.test.TmsOrderServiceImpl</parameter> --> <!-- 把这里的定义改成spring 定义的名字 就可以了--> <parameter name= "SpringBeanName">test</parameter> </service>
test是com.test.TmsOrderServiceImpl这个类在spring中注册的名字