Spring与Hessian整合

服务器端开发,分为以下3步:

第一步,在服务端定义好接口和实现

public interface EchoService {
    String say(String var1);
}
import com.caucho.hessian.server.HessianServlet;
import org.springframework.stereotype.Service;

@Service
public 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.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://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"));
    }
}

  

时间: 2024-08-30 10:15:53

Spring与Hessian整合的相关文章

Spring使用Hessian实现远程调用

1.Spring中除了提供HTTP调用器方式的远程调用,还对第三方的远程调用实现提供了支持,其中提供了对Hessian的支持. Hessian是由Caocho公司发布的一个轻量级的二进制协议远程调用实现方案,Hessian也是基于HTTP协议的,其工作原理如下: (1).客户端: a.发送远程调用请求: 客户端程序->发送远程调用请求->Hessian客户端拦截器->封装远程调用请求->Hessian代理->通过HTTP协议发送远程请求代理到服务端. b.接收远程调用响应:

spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory

Hibernate 3.6以上版本在用junit测试时会提示错误: Unable to get the default Bean Validation factory spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory  ,是因为新版hibernate用到新的jar包造成的,默认会自动找验证包,吴国不需要这一步,可以在spring整合hibernate的配置节点中添加如下标红属性: <bean id="sessio

spring和springMVC整合注解版helloworld

整合的之前需要从官网上下载spring完整的jar包,我下载的是spring-framework-3.2.2.RELEASE.整合步骤如下: 1.在eclipse中新建一个web项目,将下载的spring的jar包拷入lib目录下,但是spring启动的时候依赖一个commons-logging-1.1.jar的jar包,你需要额外的下载. 2.编写web.xml,配置spring的分发器和spring配置文件的位置.具体内容如下: <servlet> <servlet-name>

九 spring和mybatis整合

1       spring和mybatis整合 1.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spring和mybatis整合自动完成) 持久层的mapper都需要由spring进行管理. 1.2     整合环境 创建一个新的java工程(接近实际开发的工程结构) jar包: mybatis3.2.7的jar包 spring3.2.

Spring与Hibernate整合,实现Hibernate事务管理

1.所需的jar包 连接池/数据库驱动包 Hibernate相关jar Spring 核心包(5个) Spring aop 包(4个) spring-orm-3.2.5.RELEASE.jar                 [spring对hibernate的支持] spring-tx-3.2.5.RELEASE.jar                     [事务相关] 2.配置文件 Product.hbm.xml <?xml version="1.0" encoding=

Spring与Struts2整合VS Spring与Spring MVC整合

Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <contex

spring,hibernate,struts整合

SSH整合: Spring与Hibernate整合 Spring与Struts整合 整合步骤:---------------------------------------------->本人使用的是struts2.3.4.1   hibernate3.6.0  spring3.2.5 1.导入jar文件 1)struts jar文件-->如何找? -->去源码包中struts-2.3.4.1\apps\struts-blank.war -->使用压缩文件打开struts-blan

Spring与Mybatis整合

1.mybatis-spring.jar简介 Spring与Mybatis整合需要引入一个mybatis-spring.jar文件包,该整合包由Mybatis提供,可以从Mybatis官网下载. mybatis-spring.jar提供了下面几个与整合相关的API SqlSessionFactoryBean 为整合应用提供SqlSession对象资源 MapperFactoryBean 根据指定Mapper接口生成Bean实例 MapperScannerConfigurer 根据指定包批量扫描M

spring与struts2整合出现错误HTTP Status 500 - Unable to instantiate Action

在进行spring和struts2整合的时候因为大意遇到了一个问题,费了半天神终于找到了问题所在,故分享出来望广大博友引以为戒!! 我们都知道在spring和struts2整合时,spring接管了action对象的创建,所以一定要在spring配置文件中配置action,这里需要注意的是配置<bean id="???">中的id时, 要与struts.xml配置action中的<action class="???">class一致,否则就会