一. hession服务端
1.导包(spring核心包+commons-logging.jar,log4j.jar和hession.jar包)
2.接口IHello
package com.hession.test.inteface;
public interface IHello {
String sayHello();
}
3.实现接口类Hello
package com.hession.test.imp;
import com.caucho.hessian.server.HessianServlet;
import com.hession.test.inteface.IHello;
public class Hello extends HessianServlet implements IHello {
@Override
public String sayHello() {
return "Hello world";
}
}
4.配置web.xml(配置spring,spring就是一个特殊的serlvet,请看下面配置)
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:remoting-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
5. 在src目录下创建一个remoting-servlet.xml的spring配置文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="basicService" class="com.hession.test.imp.Hello"/>
<bean name="/HessianRemote" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="serviceInterface" value="com.hession.test.inteface.IHello"/>
<property name="service" ref="basicService"/>
</bean>
</beans>
到此hessian和spring的集成就结束了(提供hessian服务)。
下面配置客户端调用:
这里是把客户端配置在了服务端的工程里。
1.在src目录下配置一个remoting-client.xml(也是spring的配置文件,目的就是要用spring来管理实例)文件。
<bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://220.114.99.62:8080/项目名称/web.xml配置的拦截这里是(remote)/服务器端spring文件里配置的spring hessian的bean实例id这里是(HessianRemote)</value>
</property>
<property name="serviceInterface">
<value>com.hession.test.inteface.IHello</value>
</property> </bean>
2.写个测试的main方法试试:
try
{
ApplicationContext context = new ClassPathXmlApplicationContext("remoting-client.xml");
IHello hello =(IHello)context.getBean("myServiceClient");
System.out.println(hello.sayHello());
} catch (Exception e)
{ e.printStackTrace();
}