Spring與Hessian整合

來源:互聯網
上載者:User

標籤:

伺服器端開發,分為以下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整合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.