1、在web.xml中的配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/applicationContext.xml, /WEB-INF/Hessian-servlet.xml </param-value> </context-param> <servlet> <servlet-name>Hessian</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Hessian</servlet-name> <url-pattern>/hessian/*</url-pattern> </servlet-mapping>
2.必須在WEB-INF目錄下創建一個文件名格式為Hessian-servlet.xml的配置文件
<!-- 業務類 --> <bean id="hessianService" class="com.cjm.webservice.hessian.HessianServiceImpl"/> <!-- 遠程服務 --> <bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="hessianService"/> <property name="serviceInterface"> <value> com.cjm.webservice.hessian.HessianService </value> </property> </bean>
3.客戶端調用
String url = "http://localhost:8888/spring2/hessian/hessianService"; HessianProxyFactory factory = new HessianProxyFactory(); HessianService hessianServer = (HessianService)factory.create(HessianService.class, url); String ret = hessianServer.sayHello("Raymond.chen"); //....................
若使用spring則可通過 HessianProxyFactoryBean在客戶端連接服務,在spring的配置中加入:
<bean id="hessianService " class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8888/spring2/hessian/hessianService"/>
<property name="serviceInterface" value="com.weijy.webservice.hessian.HessianService"/>
</bean>
加入以上的配置後,就可像使用其他的bean一樣去操作了。原來實現一個webservice是可以這麼簡單 的。