標籤:
伺服器端開發,分為以下3步:
第一步,在服務端定義好介面和實現
public interface EchoService { String say(String var1);}
import com.caucho.hessian.server.HessianServlet;import org.springframework.stereotype.Service;@Servicepublic class EchoServiceImpl implements EchoService {public String say(String msg) {return "Hello:"+msg;}}
第二步,在spring設定檔中暴露服務
<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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:component-scan base-package="com.guge.group" /><context:annotation-config /><bean id="echoService" class="com.guge.group.service.EchoServiceImpl"></bean><bean name="/echoService" class="org.springframework.remoting.caucho.HessianServiceExporter"><property name="service" ref="echoService"/><property name="serviceInterface" value="com.guge.group.service.EchoService"/></bean></beans>
第三步,在web.xml中配置servlet
<servlet> <servlet-name>echo-dispatch</servlet-name> <servlet-class> com.caucho.hessian.server.HessianServlet </servlet-class> <init-param> <param-name>service-class</param-name> <param-value>com.guge.group.service.EchoServiceImpl</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>echo-dispatch</servlet-name> <url-pattern>/call/echo</url-pattern> </servlet-mapping>
用戶端調用服務,有以下兩種方式:
第一種,通過HessianProxyFactory調用
import com.caucho.hessian.client.HessianProxyFactory;import com.guge.group.service.EchoService;public class BlogList { public static void main(String[] args){ try { String url = "http://localhost:2011/call/user"; HessianProxyFactory factory = new HessianProxyFactory(); EchoService echoService = (EchoService) factory.create(EchoService.class,url); System.out.println(echoService.say("guest")); } catch (Exception e) { e.printStackTrace(); } }}
第二種,通過spring的bean配置擷取服務
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="echoService " class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:2011/call/echo " /> <property name="serviceInterface" value="com.guge.group.service.EchoService"/> </bean></beans>
import com.guge.group.service.EchoService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by google on 15/5/22. */public class BlogList { public static void main(String[] args){ ApplicationContext contex = new ClassPathXmlApplicationContext( "config/spring/appcontext-client.xml"); EchoService echo = (EchoService)contex.getBean("echoService"); System.out.println(echo.say("nb")); }}
Spring與Hessian整合