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...