webservice 整合SPRING

本文详细介绍了如何将Spring与Web服务注解整合,包括如何在Spring中注册和使用自定义的WebService代理类,以及如何通过修改services.xml文件实现与Axis2的整合。此外,还解释了如何在业务处理类上进行AOP操作,如添加日志打印、程序监控和事务处理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 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中注册的名字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值