Hessian集成Spring使用案例

来源:互联网 发布:淘宝swatch是真的吗 编辑:程序博客网 时间:2024/06/05 07:27

       演示例子分为三部分:Client  、Server、Skeleton; 其中,Client 是通过Hessian调用远程服务的客户端;Server是通过Hessian对外提供服务的服务端;Skeleton 是接口桥梁。

       一、Client 配置

            1. 新增文件 hessian.properties  ,配置内容如下:
                 business.log.url=http://192.168.0.125:8087/server/hessian/logBusiness
           2. Pom 增加配置信息,如下:
                  <dependency>
    <groupId>hessian</groupId>
<artifactId>skeleton</artifactId>
<version>1.0</version>  
</dependency>  
               <dependency>
           <groupId>com.caucho</groupId>
           <artifactId>hessian</artifactId>
          <version>4.0.51</version>
     </dependency>
         3. Spring 配置文件增加信息如下:
  <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">  
       <list>  
          <value>classpath:jdbc.properties</value>
          <value>classpath:hessian.properties</value>
        </list>  
    </property>  
</bean>
<bean id="logRemoteBusiness" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="${business.log.url}" />
    <property name="serviceInterface" value="com.kevin.service.ILogRemoteBusiness" />
</bean>
       4. 客户端通过Hessian调用远程服务
@Controller
@RequestMapping("remote")
public class HessianTest {

@Resource
    private ILogRemoteBusiness logRemoteBusiness;

@RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String test(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("ok!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        logRemoteBusiness.sayHello();
        return "true";
    }
}

二、Server 配置

1. Pom 配置同上
2. 增加 spring-hessian-server.xml 文件, 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<!-- 暴露远程服务 -->
<bean name="/logBusiness" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="logRemoteBusinessImpl" />
    <property name="serviceInterface" value="com.kevin.service.ILogRemoteBusiness" />
</bean>


</beans>
3. Web.xml 配置过滤器,如下:
<servlet>
       <servlet-name>hessian-servlet</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring/spring-hessian-server.xml</param-value>
       </init-param>
       <load-on-startup>2</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>hessian-servlet</servlet-name>
       <url-pattern>/hessian/*</url-pattern>
   </servlet-mapping>
4. 实现接口,提供服务
@Service
public class LogRemoteBusinessImpl implements ILogRemoteBusiness {


public void sayHello() {
System.out.println("===============This is server say hello==============");
}

}

三、skeleton(接口)配置

1. 提供接口
public interface ILogRemoteBusiness {
public void sayHello();
}
2. 将 skeleton.jar 发布到远程仓库
具体参考文档请看:http://blog.csdn.net/likunwen_001/article/details/78109595

四、完整代码下载地址:http://download.csdn.net/download/likunwen_001/9999145

原创粉丝点击