要弄清楚什么是 Holder 類型,得先搞清楚 IN 參數, OUT 參數, INOUT 參數的概念。
IN 參數是 JAVA 因有的參數, OUT , INOUT 參數不是 JAVA 固有的。
復制傳遞:IN參數
java 編程語言對作為參數傳遞給方法調用的所有原始值采用復制傳遞的方式,即傳遞的值是方法調用中所使用變量的復制,而不是原始值本身。比如定義一個方法
test(intintValue,DatedateValue){intValue=5;dateValue=newDate(0);}
在作如下調用時
intintValue=1;DatedateValue=newDate();test(intVlaue,dateValue);System.out.println(intValue)//打印1System.out.pritnln(dateValue)//打印現在的時間。
但是在 test 方法中對參數的值作了修改。而實際的參數值并未發生改變。
引用傳遞 : INOUT 參數 OUT 參數 .
在實現引用傳遞的編程語言中,如果 test 方法的兩面個參數定義為引用傳遞 , 對上述 test 方法調用后,再打印 intValue 跟 dateValue 的值,會發現這兩個值已經發生了改變。但是 OUT 參數更象一個返回值,因為值要從方法中傳出而不是傳入。使用 OUT 參數數,激活方法前不訪問變量的值,即傳入的值被忽略了。
Holder 類:
在 JAX-RPC 中支持 INOUT,OUT 參數。 Holder 是一個接口,它只是提供了一個補救措施,以在 java 中支持引用傳遞。這樣就使得需要用 java 與支持 INOUT,OUT 參數的編程語言寫的 web 服務進行通信 .
從 WSDL 映射 HOLDER 類型 :
接口定義
publicinterfaceHolderTestBeanInterface1extendsRemote{publicvoidechoCalendar(CalendarHolderval,Datedate)throwsRemoteException;publicvoidechoBeanHolder(BeanTestHolderbeanTestHolder,BeanTestbeanTest)throwsRemoteException;}
WSDL 文檔
<?xmlversion="1.0"encoding="UTF-8"?><definitionsname='HolderTestBeanInterface1'targetNamespace='http://holder'xmlns='http://schemas.xmlsoap.org/wsdl/'xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'xmlns:tns='http://holder'xmlns:xsd='http://www.w3.org/2001/XMLSchema'><messagename='HolderTestBeanInterface1_echoCalendar'><partname='Calendar_1'type='xsd:dateTime'/><partname='Date_2'type='xsd:dateTime'/></message><messagename='HolderTestBeanInterface1_echoCalendarResponse'><partname='Calendar_1'type='xsd:dateTime'/></message><operationname='echoCalendar'parameterOrder='Calendar_1Date_2'><inputmessage='tns:HolderTestBeanInterface1_echoCalendar'/><outputmessage='tns:HolderTestBeanInterface1_echoCalendarResponse'/></operation></portType>…
標記為 Calendar_1 的 part 元素既在 input 消息中又在 ouput 消息中 ,jax-rpc 編譯器知道它是一個 INOUT 類型 , 同時也需要 Holer 類型。如果在output消息定義一個part元素,這表示它是一個返回值或者一個OUT參數,如果part是一個OUT參數,part標記會列在operation元素的parameterOrder屬性中.OUT參數也需要Holder類型.
Javax.xml.rpc.holders包中提供了一個Holder類,用于映射到java原始類型的INOUT參數和OUT參數。如IntHolder等,同時也提供了一組為nillable內置類型定義的Holder類型,如IntegerWrapperHolder等.
自定義Holder類型。必須實現Holder標志接口, 其屬性變量名必須為 value 并且必須定義一個空的構造函數就象下面這樣:
publicclassBeanTestHolderimplementsjavax.xml.rpc.holders.Holder{publicBeanTestvalue;publicBeanTestHolder(){}publicBeanTestHolder(BeanTestbeanTest){value=beanTest;}}
Jboss Web Service 的 Holder 類型的具體實現 , 采用 Jboss 4.04 版本 .
定義一個 bean 類 :
publicclassBeanTest{privateStringbeanName;publicBeanTest(){}publicStringgetBeanName(){returnbeanName;}publicvoidsetBeanName(StringbeanName){this.beanName=beanName;}}
給 bean 定制一個 Holder:
packageholder;publicclassBeanTestHolderimplementsjavax.xml.rpc.holders.Holder{publicBeanTestvalue;publicBeanTestHolder(){}publicBeanTestHolder(BeanTestbeanTest){value=beanTest;}}
定義一個接口 :
publicinterfaceHolderTestBeanInterface1extendsRemote{publicvoidechoCalendar(CalendarHolderval,Datedate)throwsRemoteException;publicvoidechoBeanHolder(BeanTestHolderbeanTestHolder,BeanTestbeanTest)throwsRemoteException;}
接口的實現類 :
importjavax.xml.rpc.holders.*;importjava.util.Date;publicclassHolderTestBeanimplementsHolderTestBeanInterface1{publicvoidechoCalendar(CalendarHolderval,Datedate){System.out.println("echoCalendar:"+val.value.getTime());val.value.setTime(newDate());System.out.println(date);}publicvoidechoBeanHolder(BeanTestHolderbeanTestHolder,BeanTestbeanTest){BeanTestbeantest=beanTestHolder.value;System.out.println(beantest.getBeanName()+"holder");beantest.setBeanName("newname");System.out.println(beantest.getBeanName()+"holder");System.out.println(beanTest.getBeanName()+"bean");beanTest.setBeanName("newnametoo");System.out.println(beanTest.getBeanName()+"bean");}}
用于 jboss 自帶的 wstools 工具的配置文件 wstools-config.xml
<?xmlversion="1.0"encoding="UTF-8"?><!--wstools-cpclasses-configwstools-config.xml--><configurationxmlns="http://www.jboss.org/jbossws-tools"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.jboss.org/jbossws-toolshttp://www.jboss.org/jbossws-tools/schema/jbossws-tool_1_0.xsd"><java-wsdl><servicename="HolderTestBeanInterface1"style="rpc"endpoint="holder.HolderTestBeanInterface1"/><namespacestarget-namespace="http://holder"type-namespace="http://holder"/><mappingfile="HolderTestBeanInterface1.xml"/><webservicesservlet-link="HolderTestBeanInterface1"/></java-wsdl></configuration>
生成所需的 webservices.xml,jax-rpc 映射文件 , 及 wsdl 文檔
wstools -config wstools-config.xml
把生成的 wsdl 文件夾 ,webservices.xml 及映射文件放到 web-inf 目錄下。
配置 web.xml 文件
<servlet><display-name>HolderTestBeanInterface1Servlet<servlet-name>HolderTestBeanInterface1<servlet-class>holder.HolderTestBean</servlet><servlet-mapping><servlet-name>HolderTestBeanInterface1<url-pattern>/HolderTestBeanInterface1</servlet-mapping><servlet-mapping><servlet-name>HolderTestBeanInterface1<url-pattern>/services/*</servlet-mapping>
打包成 war 文件并發布
客戶端的調用 :
importjava.net.URL;importjavax.xml.rpc.*;importjavax.xml.namespace.QName;importjava.util.*;importjava.io.File;importjavax.xml.rpc.holders.CalendarHolder;importorg.jboss.ws.jaxrpc.ServiceFactoryImpl;publicclassClientTest{privateHolderTestBeanInterface1getPort()throwsException{ServiceFactoryImplfactory=newServiceFactoryImpl();URLwsdlURL=newFile("holderTest/WEB-INF/wsdl/HolderTestBeanInterface1.wsdl").toURL();URLmappingURL=newFile("holderTest/WEB-INF/HolderTestBeanInterface1.xml").toURL();QNameqname=newQName("http://holder","HolderTestBeanInterface1");Serviceservice=factory.createService(wsdlURL,qname,mappingURL);HolderTestBeanInterface1port=(HolderTestBeanInterface1)service.getPort(HolderTestBeanInterface1.class);returnport;}publicstaticvoidmain(String[]args)throwsException{ClientTestclienttest=newClientTest();HolderTestBeanInterface1h=clienttest.getPort();Datedate=newDate();System.out.println(date+"datebegin...");GregorianCalendarvalue=newGregorianCalendar(2006,1,1,23,59,59);CalendarHolderholder=newCalendarHolder(value);System.out.println(holder.value.getTime()+"calendarHolderbegin...");h.echoCalendar(holder,date);System.out.println(holder.value.getTime()+"calendarHolderend...");System.out.println(date+"dateend...");BeanTestbeanTest=newBeanTest();beanTest.setBeanName("test");BeanTestbeanTest2=newBeanTest();beanTest2.setBeanName("test2");System.out.println(beanTest.getBeanName()+"holderbegin..");System.out.println(beanTest2.getBeanName()+"beanbegin..");BeanTestHolderbeanTestHolder=newBeanTestHolder(beanTest);h.echoBeanHolder(beanTestHolder,beanTest2);System.out.println(beanTest2.getBeanName()+"beanend..");System.out.println(beanTestHolder.value.getBeanName()+"holderend..");}}
到此,關于“Holder類型是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注本站網站,小編會繼續努力為大家帶來更多實用的文章!
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...
2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...
:喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...
鹽業銀行成立于什么時間?鹽業銀行成立于1915年3月,總管理處設干北京。1913年梁士詒代理財政總長時,曾向當時國務院建議設立鹽務實業銀行。鹽業銀行成立時,由袁世凱的表弟張鎮芳任經理。清末時張曾任鹽運使,民國初曾任河南督軍、總統府顧問。原由鹽務署撥給官款,實行官商合辦,經收全部鹽稅收入;并“得代理國庫金的一部分”。第二年袁世凱病死,鹽務署不撥官款,改為商辦,成立時實收資本 ...
招行朝朝寶和朝朝盈有啥區別?1.發售機構不一樣。朝朝寶是招商銀行代銷招銀理財公司的理財產品;朝朝盈是招商銀行自營的理財產品。2.投資標的不一樣。朝朝寶對接5只理財產品;朝朝盈對接1支基金。3.資金贖回到賬時間不一樣。朝朝寶快速贖回(限額5萬)實時到賬,朝朝盈普通贖回T+1日到賬,快速贖回立即到賬(限額1萬)。4.資金可消費性不一樣。朝朝寶的資金不贖回可直接使用;朝朝盈的資金不能直接消費,需贖回方可...
a股單日最大跌幅是哪一天?1996年12月16日,滬深兩交易所發出通知,決定自16日起對在兩交易所上市和交易的股票(含A、B股)和基金類證券的交易價格實行10%的漲跌幅限制。當時大盤連續暴跌4天,其中兩天的跌幅是5.44%,5.70%,9.91%,9.44%。2007年2月27日,A股各指數跌幅均超過8%,其中上證綜指和深成指創近10年最大單日跌幅,分別為8.84%和9.29%。兩市1327只可交...