Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          在現今的Web應用中經常使用Spring框架來裝載JavaBean。如果要想將某些在Spring中裝配的JavaBean發布成WebService,使用Axis2Spring感知功能是非常容易做到的。
          ??? 在本文的例子中,除了<Tomcat安裝目錄>\webapps\axis2目錄及該目錄中的相關庫外,還需要Spring框架中的spring.jar文件,將該文件復制到<Tomcat安裝目錄>\webapps\axis2\WEB-INF\lib目錄中。
          ??? 下面先建立一個JavaBean(該JavaBean最終要被發布成WebService),代碼如下:
          package?service;
          import?entity.Person;
          public?class?SpringService
          {
          ????
          private?String?name;
          ????
          private?String?job;
          ????
          public?void?setName(String?name)
          ????{
          ????????
          this.name?=?name;
          ????}
          ????
          public?void?setJob(String?job)
          ????{
          ????????
          this.job?=?job;
          ????}
          ????
          public?Person?getPerson()
          ????{
          ????????Person?person?
          =?new?Person();
          ????????person.setName(name);
          ????????person.setJob(job);
          ????????
          return?person;
          ????}
          ????
          public?String?getGreeting(String?name)
          ????{
          ????????
          return?"hello?"?+?name;
          ????}
          }

          ??? 其中Person也是一個JavaBean,代碼如下:

          package?entity;
          public?class?Person
          {
          ????
          private?String?name;
          ????
          private?String?job;
          ????
          public?String?getName()
          ????{
          ????????
          return?name;
          ????}
          ????
          public?void?setName(String?name)
          ????{
          ????????
          this.name?=?name;
          ????}
          ????
          public?String?getJob()
          ????{
          ????????
          return?job;
          ????}
          ????
          public?void?setJob(String?job)
          ????{
          ????????
          this.job?=?job;
          ????}
          }

          ??? 將上面兩個Java源文件編譯后,放到<Tomcat安裝目錄>\webapps\axis2\WEB-INF\classes目錄中。

          ??? 在<Tomcat安裝目錄>\webapps\axis2\WEB-INF\web.xml文件中加入下面的內容:

          <listener>
          ????????
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          <context-param>
          ??????
          <param-name>contextConfigLocation</param-name>
          ??????
          <param-value>/WEB-INF/applicationContext.xml</param-value>
          </context-param>

          ??? <Tomcat安裝目錄>\webapps\axis2\WEB-INF目錄中建立一個applicationContext.xml文件,該文件是Spring框架用于裝配JavaBean的配置文件,內容如下:

          <?xml?version="1.0"?encoding="UTF-8"?>
          <beans?xmlns="http://www.springframework.org/schema/beans"
          ????????xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
          ????????xmlns:aop
          ="http://www.springframework.org/schema/aop"
          ????????xmlns:tx
          ="http://www.springframework.org/schema/tx"
          ????????xsi:schemaLocation
          ="
          ????????????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          ????????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          ????????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
          >
          ??
          <bean?id="springService"?class="service.SpringService">
          ?????
          <property?name="name"?value="姚明"?/>
          ?????
          <property?name="job"?value="職業男籃"?/>?
          ??
          </bean>???
          </beans>

          ??? applicationContext.xml文件中裝配了service.SpringService類,并被始化了namejob屬性。在配置完SpringService類后,就可以直接在程序中FileSystemXmlApplicationContext類或其他類似功能的類讀取applicationContext.xml文件中的內容,并獲得SpringService類的對象實例。但現在我們并不這樣做,而是將SpringService類發布成WebService

          ??? <Tomcat安裝目錄>\webapps\axis2\WEB-INF\lib目錄中有一個axis2-spring-1.4.1.jar文件,該文件用于將被裝配JavaBean的發布成WebService。在D盤建立一個axi2-spring-ws目錄,并在該目錄中建立一個META-INF子目錄。在META-INF目錄中建立一個services.xml文件,內容如下:

          <service?name="springService">
          ????
          <description>
          ????????Spring?aware
          ????
          </description>
          ????
          <parameter?name="ServiceObjectSupplier">
          ????????org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
          ????
          </parameter>
          ????
          <parameter?name="SpringBeanName">
          ????????springService
          ????
          </parameter>
          ????
          <messageReceivers>
          ????????
          <messageReceiver?mep="http://www.w3.org/2004/08/wsdl/in-out"
          ????????????class
          ="org.apache.axis2.rpc.receivers.RPCMessageReceiver"?/>
          ????
          </messageReceivers>
          </service>????

          ??? Windows控制臺進入axi2-spring-ws目錄,并使用jar命令將axi2-spring-ws目錄中的內容打包成axi2-spring-ws.aar,然后將該文件復制到<Tomcat安裝目錄>\webapps\axis2\WEB-INF\services目錄中,啟動Tomcat后,就可以訪問該WebService了,訪問方式與前面幾篇文章的訪問方式相同。獲得wsdl內容的URL如下:

          http://localhost:8080/axis2/services/springService?wsdl

          ??? 在將Spring中的裝配JavaBean發布成WebService需要注意以下幾點:

          ??? 1.?JavaBean編譯生成的.class文件需要放在WEB-INF\classes目錄中,或打成.jar包后放在WEB-INF\lib目錄中,而WEB-INF\services目錄中的.aar包中不需要包含.class文件,而只需要包含一個META-INF目錄,并在該目錄中包含一個services.xml文件即可。

          ??? 2.?services.xml的配置方法與前幾篇文章的配置方法類似,只是并不需要使用ServiceClass參數指定要發布成WebServiceJava類,而是要指定在applicationContext.xml文件中的裝配JavaBean的名稱(SpringBeanName參數)。

          ??? 3.?services.xml文件中需要通過ServiceObjectSupplier參數指定SpringServletContextObjectSupplier類來獲得SpringApplicationContext對象。

          posted on 2009-09-23 14:41 禮物 閱讀(272) 評論(0)  編輯  收藏 所屬分類: Axis
          主站蜘蛛池模板: 烟台市| 湘潭县| 宁安市| 边坝县| 西昌市| 康平县| 岱山县| 施秉县| 鹰潭市| 白玉县| 平利县| 太和县| 永城市| 四川省| 神池县| 静安区| 固镇县| 拉孜县| 固原市| 若羌县| 金秀| 桃园县| 甘肃省| 都匀市| 确山县| 定陶县| 常山县| 白河县| 濮阳县| 襄汾县| 鹤峰县| 溧水县| 师宗县| 凤台县| 民丰县| 开鲁县| 遂昌县| 长岛县| 五华县| 岗巴县| 彝良县|