spring hessian搭建

1.jar包:

hessian-4.0.37.jar  

spring-web-3.2.4 .jar

服务器端:

1.修改web.xml文件,添加servlet监听、

web.xml

  <servlet>
  	<servlet-name>ChickenWebApp</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
	  	<param-name>contextConfigLocation</param-name>
	  	<param-value>classpath:spring.xml</param-value>
 	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>ChickenWebApp</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 控制hessian远程访问 -->
  <servlet-mapping>
  	<servlet-name>ChickenWebApp</servlet-name>
  	<url-pattern>*.service</url-pattern>
  </servlet-mapping>

 

2.编写远程接口

IRmiService.java

package service;

public interface IRmiService 
{
	public String sayHello();
	public void pringHello();
}

3.接口实现类

RmiServiceImp.java

package serviceimp;

import service.IRmiService;

public class RmiServiceImp implements IRmiService 
{

	public String sayHello() 
	{
		return "sayHello...";
	}

	public void pringHello() 
	{
		System.out.println("printHello....");
	}

}

4.在spring配置文件中,配置hessian导出器

spring.xml

    <bean id="iRmiService" class="serviceimp.RmiServiceImp"></bean>
    
    <!-- 利用RmiServiceExporter发布iRmiService为RMI -->
    <bean id="test" class="org.springframework.remoting.caucho.HessianServiceExporter">
     <!-- RMI服务 -->
     <property name="service" ref="iRmiService"></property>
     <!-- RMI服务名称 -->
<!--      <property name="serviceName" value="test"></property> -->
     <!-- RMI服务接口 -->
     <property name="serviceInterface" value="service.IRmiService"></property>
     <!-- 绑定端口 -->
<!--      <property name="registryPort" value="8888"></property> -->
    </bean>
    
    <bean name="/test" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
      <value>
       /ChickenWebApp.service = test
      </value>
     </property>
    </bean>

 

本地配置了一个url映射来确保DispatcherServlet把请求转给hessian。

还有一种方法是,直接设置bean的name属性。如下:

 

<bean name="test" class="org.springframework.remoting.caucho.HessianServiceExporter">
    	<!-- RMI服务 -->
    	<property name="service" ref="iRmiService"></property>
    	<!-- RMI服务名称 -->
<!--     	<property name="serviceName" value="test"></property> -->
    	<!-- RMI服务接口 -->
    	<property name="serviceInterface" value="service.IRmiService"></property>
    	<!-- 绑定端口 -->
<!--     	<property name="registryPort" value="8888"></property> -->
    </bean>


 

服务器端:

1.配置spring配置文件

	<bean id="iRmiService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl" value="http://localhost:8080/ChickenWebApp/ChickenWebApp.service"></property>
		<property name="serviceInterface" value="service.IRmiService"></property>
	</bean>


2.测试

public static void main(String[] args) 
	{
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		IRmiService iRmiService = (IRmiService) ctx.getBean("iRmiService");
		iRmiService.pringHello();
		System.out.println(iRmiService.sayHello());
	}


测试结果:

 

2015-1-9 17:00:53 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1608e05: startup date [Fri Jan 09 17:00:53 CST 2015]; root of context hierarchy
2015-1-9 17:00:53 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
sayHello...


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值