風(fēng)人園

          弱水三千,只取一瓢,便能解渴;佛法無(wú)邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評(píng)論 - 56, 引用 - 0
          數(shù)據(jù)加載中……

          spring 中集成 xfire(ZT)

          XFire可以很好的集成到Spring中,Spring的代碼已經(jīng)做了這方面的集成。
          首先,我們先創(chuàng)建我們的Web服務(wù),采用接口和實(shí)現(xiàn)類的方式:

          接口MathService.java:
          1. package?com.kuaff.xfire.samples;
          2. public?interface?MathService
          3. {
          4. ????public?long?add(int?p1,?int?p2);
          5. }


          實(shí)現(xiàn)類:
          1. package?com.kuaff.xfire.samples;
          2. public?class?MathServiceImpl?implements?MathService
          3. {
          4. ????public?long?add(int?p1,?int?p2)
          5. ????{
          6. ????????return?p1?+?p2;
          7. ????}
          8. }


          META-INF/xfire/service.xml文件可以省略了,因?yàn)閣eb服務(wù)的定義在xfire-servlet.xml中可以找到。
          下面要做的工具就是配置了。
          在WEB-INF文件夾下創(chuàng)建applicationContext.xml文件,這是Spring的配置文件,如果你使用其他的Spring配置文件,可以將下面的bean添加到那個(gè)配置文件中:


          1. <?xml?version="1.0"?encoding="UTF-8"?>
          2. <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">
          3. <beans>
          4. ????<bean?id="mathBean"?class="com.kuaff.xfire.samples.MathServiceImpl"/>
          5. </beans>


          定義了mathBean,這個(gè)Bean就是我們的實(shí)現(xiàn)類,當(dāng)然你也可以在這個(gè)文件中定義其他的需要Spring管理的bean。

          在WEB-INF文件夾下創(chuàng)建xfire-servlet.xml文件,根據(jù)Spring規(guī)范,這個(gè)文件名起做xfire-servlet.xml,其中xfire是web.xml配置的DispatcherServlet的名稱:

          1. <?xml?version="1.0"?encoding="UTF-8"?>
          2. <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">
          3. <beans>
          4. ????<bean?class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          5. ????????<property?name="urlMap">
          6. ????????????<map>
          7. ????????????????<entry?key="/MathService">
          8. ????????????????????<ref?bean="math"/>
          9. ????????????????</entry>
          10. ????????????</map>
          11. ????????</property>
          12. ????</bean>
          13. ???
          14. ????<bean?id="math"?class="org.codehaus.xfire.spring.remoting.XFireExporter">
          15. ????????<property?name="serviceFactory">
          16. ????????????<ref?bean="xfire.serviceFactory"/>
          17. ????????</property>
          18. ????????<property?name="xfire">
          19. ????????????<ref?bean="xfire"/>
          20. ????????</property>
          21. ????????<property?name="serviceBean">
          22. ????????????<ref?bean="mathBean"/>
          23. ????????</property>
          24. ????????<property?name="serviceClass">
          25. ????????????<value>com.kuaff.xfire.samples.MathService</value>
          26. ????????</property>
          27. ????</bean>
          28. </beans>


          這個(gè)文件的上半部分將MathService這個(gè)URL和math這個(gè)bean聯(lián)系在一起。下半部分定義了Web服務(wù)的bean和服務(wù)接口。其中mathBean是我們?cè)赼pplicationContext.xml中配置的那個(gè)Bean。

          最后一步就是修改web.xml文件:
          1. <?xml?version="1.0"?encoding="ISO-8859-1"?>
          2. <!DOCTYPE?web-app
          3. ????PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"
          4. ????"http://java.sun.com/dtd/web-app_2_3.dtd">
          5. <web-app>
          6. ????<context-param>
          7. ????????<param-name>contextConfigLocation</param-name>
          8. ????????<param-value>/WEB-INF/applicationContext.xml
          9. ????????classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
          10. ????</context-param>
          11. ????<context-param>
          12. ????????<param-name>log4jConfigLocation</param-name>
          13. ????????<param-value>/WEB-INF/log4j.properties</param-value>
          14. ????</context-param>
          15. ????<listener>
          16. ????????<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
          17. ????</listener>
          18. ????<listener>
          19. ????????<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          20. ????</listener>
          21. ????<servlet>
          22. ????????<servlet-name>xfire</servlet-name>
          23. ????????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          24. ????</servlet>
          25. ????<servlet-mapping>
          26. ????????<servlet-name>xfire</servlet-name>
          27. ????????<url-pattern>/*</url-pattern>
          28. ????</servlet-mapping>
          29. </web-app>


          需要注意這個(gè)文件的三個(gè)部分:

          ?

          1.??????在定義contextConfigLocation參數(shù)時(shí)一定要加上classpath:org/codehaus/xfire/spring/xfire.xml。

          2.??????定義listener:?org.springframework.web.context.ContextLoaderListener

          3.??????定義DispatcherServlet:?xfire

          這樣,你就可以訪問(wèn)http://localhost:8080/xfire/MathService來(lái)調(diào)用這個(gè)Web服務(wù),也可以通過(guò)網(wǎng)址http://localhost:8080/xfire/MathService?wsdl來(lái)查看wsdl文檔。

          posted on 2006-07-28 11:28 風(fēng)人園 閱讀(223) 評(píng)論(0)  編輯  收藏 所屬分類: Spring

          主站蜘蛛池模板: 贵定县| 武夷山市| 岳阳县| 奎屯市| 临澧县| 德江县| 青龙| 汨罗市| 信阳市| 东城区| 崇仁县| 车险| 蓬莱市| 乐亭县| 清流县| 吐鲁番市| 安庆市| 寿宁县| 杭锦旗| 望都县| 上杭县| 延边| 思茅市| 徐闻县| 自贡市| 新化县| 长海县| 天水市| 湾仔区| 通山县| 班玛县| 宁德市| 新平| 枞阳县| 六盘水市| 大城县| 余干县| 西乡县| 静安区| 阳高县| 敖汉旗|