在Spring集成XFire
XFire可以很好的集成到Spring中,Spring的代碼已經(jīng)做了這方面的集成。
首先,我們先創(chuàng)建我們的Web服務(wù),采用接口和實(shí)現(xiàn)類的方式:
接口MathService.java:
package com.kuaff.xfire.samples;
public interface MathService
{
? public long add(int p1, int p2);
}
實(shí)現(xiàn)類:
package com.kuaff.xfire.samples;
public class MathServiceImpl implements MathService
{
? public long add(int p1, int p2)
? {
? ? return p1 + p2;
? }
}
META-INF/xfire/service.xml文件可以省略了,因?yàn)閣eb服務(wù)的定義在xfire-servlet.xml中可以找到。
下面要做的工具就是配置了。
在WEB-INF文件夾下創(chuàng)建applicationContext.xml文件,這是Spring的配置文件,如果你使用其他的Spring配置文件,可以將下面的bean添加到那個(gè)配置文件中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
? <bean id="mathBean" class="com.kuaff.xfire.samples.MathServiceImpl"/>
</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的名稱:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
? <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
? ? <property name="urlMap">
? ? ? ? <map>
? ? ? ? ? <entry key="/MathService">
? ? ? ? ? ? <ref bean="math"/>
? ? ? ? ? </entry>
? ? ? ? </map>
? ? </property>
? </bean>
?
? <bean id="math" class="org.codehaus.xfire.spring.remoting.XFireExporter">
? ? <property name="serviceFactory">
? ? ? ? <ref bean="xfire.serviceFactory"/>
? ? </property>
? ? <property name="xfire">
? ? ? ? <ref bean="xfire"/>
? ? </property>
? ? <property name="serviceBean">
? ? ? ? <ref bean="mathBean"/>
? ? </property>
? ? <property name="serviceClass">
? ? ? ? <value>com.kuaff.xfire.samples.MathService</value>
? ? </property>
? </bean>
</beans>
這個(gè)文件的上半部分將MathService這個(gè)URL和math這個(gè)bean聯(lián)系在一起。下半部分定義了Web服務(wù)的bean和服務(wù)接口。其中mathBean是我們?cè)赼pplicationContext.xml中配置的那個(gè)Bean。
最后一步就是修改web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
? PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
? "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
? <context-param>
? ? <param-name>contextConfigLocation</param-name>
? ? <param-value>/WEB-INF/applicationContext.xml
? ? classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
? </context-param>
? <context-param>
? ? <param-name>log4jConfigLocation</param-name>
? ? <param-value>/WEB-INF/log4j.properties</param-value>
? </context-param>
? <listener>
? ? <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
? </listener>
? <listener>
? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? </listener>
? <servlet>
? ? <servlet-name>xfire</servlet-name>
? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? </servlet>
? <servlet-mapping>
? ? <servlet-name>xfire</servlet-name>
? ? <url-pattern>/*</url-pattern>
? </servlet-mapping>
</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-03 16:42 風(fēng)人園 閱讀(523) 評(píng)論(0) 編輯 收藏 所屬分類: Spring