自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談Spring2.5集合Web Service

開發(fā) 后端
本文將簡單談談Spring2.5集合Web Service。Spring為JAX-RPC servlet的端點實現(xiàn)提供了一個方便的基類 - ServletEndpointSupport. 未來暴露我們的 AccountService我們擴展Spring的ServletEndpointSupport類并在這里實現(xiàn)了我們的業(yè)務邏輯,通常將調用交給業(yè)務層。

使用JAX-RPC暴露基于servlet的web服務

Spring為JAX-RPC servlet的端點實現(xiàn)提供了一個方便的基類 - ServletEndpointSupport. 未來暴露我們的 AccountService我們擴展Spring的ServletEndpointSupport類并在這里實現(xiàn)了我們的業(yè)務邏輯,通常將調用交給業(yè)務層。

  1. /**  
  2. * JAX-RPC compliant RemoteAccountService implementation that simply delegates  
  3. * to the AccountService implementation in the root web application context.  
  4. *  
  5. * This wrapper class is necessary because JAX-RPC requires working with dedicated  
  6. * endpoint classes. If an existing service needs to be exported, a wrapper that  
  7. * extends ServletEndpointSupport for simple application context access is  
  8. * the simplest JAX-RPC compliant way.  
  9. *  
  10. * This is the class registered with the server-side JAX-RPC implementation.  
  11. * In the case of Axis, this happens in "server-config.wsdd" respectively via  
  12. * deployment calls. The web service engine manages the lifecycle of instances  
  13. * of this class: A Spring application context can just be accessed here.  
  14. */import org.springframework.remoting.jaxrpc.ServletEndpointSupport;  
  15.  
  16. public class AccountServiceEndpoint extends ServletEndpointSupport implements RemoteAccountService {  
  17.       
  18.     private AccountService biz;  
  19.  
  20.     protected void onInit() {  
  21.         this.biz = (AccountService) getWebApplicationContext().getBean("accountService");  
  22.     }  
  23.  
  24.     public void insertAccount(Account acc) throws RemoteException {  
  25.         biz.insertAccount(acc);  
  26.     }  
  27.  
  28.     public Account[] getAccounts(String name) throws RemoteException {  
  29.         return biz.getAccounts(name);  
  30.     }  

AccountServletEndpoint需要在Spring中同一個上下文的web應用里運行,以獲得對Spring的訪問能力。如果使用Axis,把AxisServlet定義復制到你的'web.xml'中,并且在'server-config.wsdd'中設置端點(或使用發(fā)布工具)。參看JPetStore這個例子中OrderService是如何用Axis發(fā)布成一個Web服務的。

使用JAX-RPC訪問web服務

Spring提供了兩個工廠bean用來創(chuàng)建Web服務代理,LocalJaxRpcServiceFactoryBean 和 JaxRpcPortProxyFactoryBean。前者只返回一個JAX-RPC服務類供我們使用。后者是一個全功能的版本,可以返回一個實現(xiàn)我們業(yè)務服務接口的代理。本例中,我們使用后者來為前面段落中暴露的AccountService端點創(chuàng)建一個代理。你將看到Spring對Web服務提供了極好的支持,只需要很少的代碼 - 大多數(shù)都是通過類似下面的Spring配置文件:

  1. <bean id="accountWebService" class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean"> 
  2.     <property name="serviceInterface" value="example.RemoteAccountService"/> 
  3.     <property name="wsdlDocumentUrl" value="http://localhost:8080/account/services/accountService?WSDL"/> 
  4.     <property name="namespaceUri" value="http://localhost:8080/account/services/accountService"/> 
  5.     <property name="serviceName" value="AccountService"/> 
  6.     <property name="portName" value="AccountPort"/> 
  7. </bean> 

serviceInterface是我們客戶端將使用的遠程業(yè)務接口。 wsdlDocumentUrl是WSDL文件的URL. Spring需要用它作為啟動點來創(chuàng)建JAX-RPC服務。 namespaceUri對應.wsdl文件中的targetNamespace。 serviceName 對應.wsdl文件中的服務名。 portName 對應.wsdl文件中的端口號。

現(xiàn)在我們可以很方便的訪問web服務,因為我們有一個可以將它暴露為RemoteAccountService接口的bean工廠。我們可以在Spring中這樣使用:

  1. <bean id="client" class="example.AccountClientImpl"> 
  2.     ...  
  3.     <property name="service" ref="accountWebService"/> 
  4. </bean> 

從客戶端代碼上看,除了它拋出RemoteException,我們可以把這個web服務當成一個普通的類進行訪,。

  1. public class AccountClientImpl {  
  2.  
  3.     private RemoteAccountService service;  
  4.       
  5.     public void setService(RemoteAccountService service) {  
  6.         this.service = service;  
  7.     }  
  8.       
  9.     public void foo() {  
  10.         try {  
  11.             service.insertAccount(...);  
  12.         }  
  13.         catch (RemoteException ex) {  
  14.             // ouch  
  15.         }  
  16.     }  

我們可以不檢查受控異常RemoteException,因為Spring將它自動轉換成相應的非受控異常RemoteException。這也需要我們提供一個非RMI的接口。現(xiàn)在配置文件如下:

  1. <bean id="accountWebService" class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean"> 
  2.     <property name="serviceInterface" value="example.AccountService"/> 
  3.     <property name="portInterface" value="example.RemoteAccountService"/> 
  4. </bean> 

我們的serviceInterface變成了非RMI接口。我們的RMI接口現(xiàn)在使用portInterface屬性來定義。我們的客戶端代碼可以避免處理異常java.rmi.RemoteException:

  1. public class AccountClientImpl {  
  2.  
  3.     private AccountService service;  
  4.       
  5.     public void setService(AccountService service) {  
  6.         this.service = service;  
  7.     }  
  8.       
  9.     public void foo() {  
  10.         service.insertAccount(...);  
  11.     }  

請注意你也可以去掉"portInterface"部分并指定一個普通業(yè)務接口作為"serviceInterface"。這樣JaxRpcPortProxyFactoryBean將自動切換到JAX-RPC "動態(tài)調用接口", 不使用固定端口存根來進行動態(tài)調用。這樣做的好處是你甚至不需要使用一個RMI相關的Java接口(比如在非Java的目標web服務中);你只需要一個匹配的業(yè)務接口。查看JaxRpcPortProxyFactoryBean的javadoc來了解運行時實行的細節(jié)。

注冊JAX-RPC Bean映射

T為了傳遞類似Account等復雜對象,我們必須在客戶端注冊bean映射。

注意

在服務器端通常在'server-config.wsdd'中使用Axis進行bean映射注冊。

我們將使用Axis在客戶端注冊bean映射。為此,我們需要通過程序注冊這個bean映射:

  1. public class AxisPortProxyFactoryBean extends JaxRpcPortProxyFactoryBean {  
  2.  
  3.     protected void postProcessJaxRpcService(Service service) {  
  4.         TypeMappingRegistry registry = service.getTypeMappingRegistry();  
  5.         TypeMapping mapping = registry.createTypeMapping();  
  6.         registerBeanMapping(mapping, Account.class, "Account");  
  7.         registry.register("http://schemas.xmlsoap.org/soap/encoding/", mapping);  
  8.     }  
  9.  
  10.     protected void registerBeanMapping(TypeMapping mapping, Class type, String name) {  
  11.         QName qName = new QName("http://localhost:8080/account/services/accountService", name);  
  12.         mapping.register(type, qName,  
  13.                 new BeanSerializerFactory(type, qName),  
  14.                 new BeanDeserializerFactory(type, qName));  
  15.     }  

注冊自己的JAX-RPC 處理器

本節(jié)中,我們將注冊自己的javax.rpc.xml.handler.Handler 到Web服務代理,這樣我們可以在SOAP消息被發(fā)送前執(zhí)行定制的代碼。Handler是一個回調接口。jaxrpc.jar中有個方便的基類javax.rpc.xml.handler.GenericHandler供我們繼承使用:

  1. public class AccountHandler extends GenericHandler {  
  2.  
  3.     public QName[] getHeaders() {  
  4.         return null;  
  5.     }  
  6.  
  7.     public boolean handleRequest(MessageContext context) {  
  8.         SOAPMessageContext smc = (SOAPMessageContext) context;  
  9.         SOAPMessage msg = smc.getMessage();  
  10.         try {  
  11.             SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();  
  12.             SOAPHeader header = envelope.getHeader();  
  13.             ...  
  14.         }  
  15.         catch (SOAPException ex) {  
  16.             throw new JAXRPCException(ex);  
  17.         }  
  18.         return true;  
  19.     }  

我們現(xiàn)在要做的就是把AccountHandler注冊到JAX-RPC服務,這樣它可以在消息被發(fā)送前調用 handleRequest(..)。Spring目前對注冊處理方法還不提供聲明式支持,所以我們必須使用編程方式。但是Spring中這很容易實現(xiàn),我們只需覆寫專門為此設計的 postProcessJaxRpcService(..) 方法:

  1. public class AccountHandlerJaxRpcPortProxyFactoryBean extends JaxRpcPortProxyFactoryBean {  
  2.  
  3.     protected void postProcessJaxRpcService(Service service) {  
  4.         QName port = new QName(this.getNamespaceUri(), this.getPortName());  
  5.         List list = service.getHandlerRegistry().getHandlerChain(port);  
  6.         list.add(new HandlerInfo(AccountHandler.class, null, null));  
  7.         logger.info("Registered JAX-RPC AccountHandler on port " + port);  
  8.     }  

***,我們要記得更改Spring配置文件來使用我們的工廠bean:

  1. <bean id="accountWebService" class="example.AccountHandlerJaxRpcPortProxyFactoryBean"> 
  2.     ...  
  3. </bean> 

使用JAX-WS暴露基于servlet的web服務
Spring為JAX-WS servlet端點實現(xiàn)提供了一個方便的基類 - SpringBeanAutowiringSupport。要暴露我們的AccountService接口,我們可以擴展Spring的SpringBeanAutowiringSupport類并實現(xiàn)我們的業(yè)務邏輯,通常把調用交給業(yè)務層。我們將簡單的使用Spring 2.5的@Autowired注解來聲明依賴于Spring管理的bean。

  1. /**  
  2. * JAX-WS compliant AccountService implementation that simply delegates  
  3. * to the AccountService implementation in the root web application context.  
  4. *  
  5. * This wrapper class is necessary because JAX-WS requires working with dedicated  
  6. * endpoint classes. If an existing service needs to be exported, a wrapper that  
  7. * extends SpringBeanAutowiringSupport for simple Spring bean autowiring (through  
  8. * the @Autowired annotation) is the simplest JAX-WS compliant way.  
  9. *  
  10. * This is the class registered with the server-side JAX-WS implementation.  
  11. * In the case of a Java EE 5 server, this would simply be defined as a servlet  
  12. * in web.xml, with the server detecting that this is a JAX-WS endpoint and reacting  
  13. * accordingly. The servlet name usually needs to match the specified WS service name.  
  14. *  
  15. * The web service engine manages the lifecycle of instances of this class.  
  16. * Spring bean references will just be wired in here.  
  17. */import org.springframework.web.context.support.SpringBeanAutowiringSupport;  
  18.  
  19. @WebService(serviceName="AccountService")  
  20. public class AccountServiceEndpoint extends SpringBeanAutowiringSupport {  
  21.  
  22.     @Autowired  
  23.     private AccountService biz;  
  24.  
  25.     @WebMethod  
  26.     public void insertAccount(Account acc) {  
  27.        biz.insertAccount(acc);  
  28.     }  
  29.  
  30.     @WebMethod  
  31.     public Account[] getAccounts(String name) {  
  32.        return biz.getAccounts(name);  
  33.     }  

為了能夠讓Spring上下文使用Spring設施,我們的AccountServletEndpoint類需要運行在同一個web應用中。在Java EE 5環(huán)境中這是默認的情況,它使用JAX-WS servlet端點安裝標準契約。詳情請參閱Java EE 5 web服務教程。

使用JAX-WS暴露單獨web服務
Sun JDK 1.6提供的內置JAX-WS provider 使用內置的HTTP服務器來暴露web服務。Spring的SimpleJaxWsServiceExporter類檢測所有在Spring應用上下文中配置的l@WebService 注解bean,然后通過默認的JAX-WS服務器(JDK 1.6 HTTP服務器)來暴露它們。

在這種場景下,端點實例將被作為Spring bean來定義和管理。它們將使用JAX-WS來注冊,但其生命周期將一直跟隨Spring應用上下文。這意味著Spring的顯示依賴注入可用于端點實例。當然通過@Autowired來進行注解驅動的注入也可以正常工作。

  1. <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"> 
  2.     <property name="baseAddress" value="http://localhost:9999/"/> 
  3. </bean> 
  4.  
  5. <bean id="accountServiceEndpoint" class="example.AccountServiceEndpoint"> 
  6.     ...  
  7. </bean> 
  8.  
  9. ... 

AccountServiceEndpoint類可能源自Spring的 SpringBeanAutowiringSupport類,也可能不是。因為這里的端點是由Spring完全管理的bean。這意味著端點實現(xiàn)可能像下面這樣沒有任何父類定義 - 而且Spring的@Autowired配置注解仍然能夠使用:

  1. @WebService(serviceName="AccountService")  
  2. public class AccountServiceEndpoint {  
  3.  
  4.     @Autowired  
  5.     private AccountService biz;  
  6.  
  7.     @WebMethod  
  8.     public void insertAccount(Account acc) {  
  9.        biz.insertAccount(acc);  
  10.     }  
  11.  
  12.     @WebMethod  
  13.     public Account[] getAccounts(String name) {  
  14.        return biz.getAccounts(name);  
  15.     }  

使用Spring支持的JAX-WS RI來暴露服務

Sun的JAX-WS RI被作為GlassFish項目的一部分來開發(fā),它使用了Spring支持來作為JAX-WS Commons項目的一部分。這允許把JAX-WS端點作為Spring管理的bean來定義。這與前面章節(jié)討論的單獨模式類似 - 但這次是在Servlet環(huán)境中。注意這在Java EE 5環(huán)境中是不可遷移的,建議在沒有EE的web應用環(huán)境如Tomcat中嵌入JAX-WS RI。

與標準的暴露基于servlet的端點方式不同之處在于端點實例的生命周期將被Spring管理。這里在web.xml將只有一個JAX-WS servlet定義。在標準的Java EE 5風格中(如上所示),你將對每個服務端點定義一個servlet,每個服務端點都代理到Spring bean (通過使用@Autowired,如上所示)。

關于安裝和使用詳情請查閱https://jax-ws-commons.dev.java.net/spring/

使用JAX-WS訪問web服務
類似JAX-RPC支持,Spring提供了2個工廠bean來創(chuàng)建JAX-WS web服務代理,它們是LocalJaxWsServiceFactoryBean和JaxWsPortProxyFactoryBean。前一個只能返回一個JAX-WS服務對象來讓我們使用。后面的是可以返回我們業(yè)務服務接口的代理實現(xiàn)的完整版本。這個例子中我們使用后者來為AccountService端點再創(chuàng)建一個代理:

  1. <bean id="accountWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean"> 
  2.     <property name="serviceInterface" value="example.AccountService"/> 
  3.     <property name="wsdlDocumentUrl" value="http://localhost:8080/account/services/accountService?WSDL"/> 
  4.     <property name="namespaceUri" value="http://localhost:8080/account/services/accountService"/> 
  5.     <property name="serviceName" value="AccountService"/> 
  6.     <property name="portName" value="AccountPort"/> 
  7. </bean> 

serviceInterface是我們客戶端將使用的遠程業(yè)務接口。 wsdlDocumentUrl是WSDL文件的URL. Spring需要用它作為啟動點來創(chuàng)建JAX-RPC服務。 namespaceUri對應.wsdl文件中的targetNamespace。 serviceName 對應.wsdl文件中的服務名。 portName 對應.wsdl文件中的端口號。

現(xiàn)在我們可以很方便的訪問web服務,因為我們有一個可以將它暴露為AccountService接口的bean工廠。我們可以在Spring中這樣使用:

  1. <bean id="client" class="example.AccountClientImpl"> 
  2.     ...  
  3.     <property name="service" ref="accountWebService"/> 
  4. </bean> 

從客戶端代碼上我們可以把這個web服務當成一個普通的類進行訪問:

  1. public class AccountClientImpl {  
  2.  
  3.     private AccountService service;  
  4.  
  5.     public void setService(AccountService service) {  
  6.         this.service = service;  
  7.     }  
  8.  
  9.     public void foo() {  
  10.         service.insertAccount(...);  
  11.     }  

注意: 上面被稍微簡化了,因為JAX-WS需要端點接口及實現(xiàn)類來使用@WebService, @SOAPBinding等注解。 這意味著你不能簡單的使用普通的Java接口和實現(xiàn)來作為JAX-WS端點,你需要首先對它們進行相應的注解。這些需求詳情請查閱JAX-WS文檔。

使用XFire來暴露Web服務

XFire是一個Codehaus提供的輕量級SOAP庫。暴露XFire是通過XFire自帶的context,這個context將和RemoteExporter風格的bean相結合,后者需要被加入到在你的WebApplicationContext中。對于所有讓你來暴露服務的方法,你需要創(chuàng)建一個DispatcherServlet類并有相應的WebApplicationContext來封裝你將要暴露的服務:

  1. <servlet> 
  2.     <servlet-name>xfire</servlet-name> 
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
  4. </servlet> 

你還必須鏈接XFire配置。這是通過增加一個context文件到由ContextLoaderListener(或者ContextLoaderServlet)加載的 contextConfigLocations 參數(shù)中。

  1. <context-param> 
  2.     <param-name>contextConfigLocation</param-name> 
  3.     <param-value>classpath:org/codehaus/xfire/spring/xfire.xml</param-value> 
  4. </context-param> 
  5.  
  6. <listener> 
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  8. </listener> 

在你加入一個Servlet映射后(映射/*到上面定義的XFire Servlet),你只需要增加一個額外的bean來使用XFire暴露服務。例如,在 'xfire-servlet.xml' 中增加如下配置:

  1. <beans> 
  2.  
  3.     <bean name="/Echo" class="org.codehaus.xfire.spring.remoting.XFireExporter"> 
  4.         <property name="serviceInterface" value="org.codehaus.xfire.spring.Echo"/> 
  5.         <property name="serviceBean"> 
  6.             <bean class="org.codehaus.xfire.spring.EchoImpl"/> 
  7.         </property> 
  8.         <!-- the XFire bean is defined in the xfire.xml file --> 
  9.         <property name="xfire" ref="xfire"/> 
  10. </bean> 
  11.  
  12. </beans> 

XFire處理了其他的事情。它檢查你的服務接口并產生一個WSDL文件。這里的部分文檔來自XFire網(wǎng)站,要了解更多有關XFire Spring的集成請訪問 docs.codehaus.org/display/XFIRE/Spring。

【編輯推薦】

  1. JSF和Spring的集成
  2. 在Spring中進行集成測試
  3. 比較JSF、Spring MVC、Stripes、Struts 2、Tapestry、Wicket
  4. Spring中的TopLink ServerSession
  5. Spring is coming
責任編輯:彭凡 來源: 百度空間
相關推薦

2009-06-24 09:22:04

Spring2.5新特

2009-06-25 13:23:50

Spring2.5

2009-06-18 13:44:05

Spring2.0spring2.5

2009-10-13 11:22:46

VB.NET調用Web

2009-06-18 09:42:52

SpringXFire構建Web

2009-06-29 15:51:48

Spring容器

2009-09-11 11:25:35

LINQ函數(shù)集合

2009-06-01 12:04:38

JPASpringJAVA

2017-06-06 10:30:12

前端Web寬度自適應

2009-06-26 17:34:29

Spring入門

2009-08-18 09:06:41

C#對象和集合

2010-04-14 15:09:49

Oracle函數(shù)

2009-12-08 17:48:28

Web Service

2009-08-20 15:38:50

C#建立Web Ser

2009-07-06 10:25:14

Java Web Se

2011-09-08 17:48:33

Web Widget

2009-09-14 14:01:21

LINQ泛型數(shù)據(jù)集

2011-08-12 11:36:07

2012-04-19 09:34:21

ibmdw

2012-11-08 10:27:22

WEB產品架構架構
點贊
收藏

51CTO技術棧公眾號