目录
[TOC]
不使用Spring
所需jar包:
hessian-4.0.7.jar
步骤
- 服务端编写接口和实现类
- 服务端编写web.xml
将接口所在包打包导出并发布服务
客户端导入接口对应jar包
- 客户端编写测试类
服务端编写接口和实现类
接口:
public interface HessianTest {
public String sayHello(String name);
}
实现类:
import com.xxx.hessiantest.HessianTest;
public class HessianTestImpl implements HessianTest {
public String sayHello(String name) {
return "hello " + name;
}
}
服务端编写web.xml
HessianServlet以及对应的初始化参数home-class,home-api写法固定。其中,home-class对应实现类全包名,home-api对应接口全报名。
<servlet>
<servlet-name>hessiantest</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>com.xxx.hessiantestimpl.HessianTestImpl</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>com.xxx.hessiantest.HessianTest</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessiantest</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
打成jar包,发布服务
只需要将接口所在的包打成jar包即可。
客户端导入jar包客户端编写测试类
import com.caucho.hessian.client.HessianProxyFactory;
import com.xxx.hessiantest.HessianTest;
public class TestHessianInterface {
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/Hessian_Server/hessian";
HessianProxyFactory factory = new HessianProxyFactory();
HessianTest hessianTest = (HessianTest) factory.create(HessianTest.class, url);
String hello = hessianTest.sayHello("阿紫");
System.out.println(hello);
}
}
打印输出结果
hello 阿紫
使用Spring
所需要jar包
aopalliance-1.0.jar
commons-logging-1.1.1.jar
hessian-4.0.7.jar
junit-4.10.jar
spring-aop-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-orm-3.2.5.RELEASE.jar
spring-test-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE.jar
spring-webmvc-3.2.5.RELEASE.jar
步骤
服务端编写接口和实现类
服务端编写web.xml
服务端编写spring的配置文件hessian-server.xml
服务端将接口所在的包打包发布服务
服务端编写接口和实现类
服务端接口
public interface HessianTest {
public String sayHello(String name);
}
服务端实现类
import com.xxx.hessiantest.HessianTest;
public class HessianTestImpl implements HessianTest {
public String sayHello(String name) {
return "hello " + name;
}
}
服务端编写web.xml
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-server.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
服务端编写hessian-server.xml文件
其中bean的name与web.xml中的URL拼接成hessian接口的路径地址。
property中name属性固定
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="hessianTestImpl" class="com.xxx.hessiantestimpl.HessianTestImpl"></bean>
<bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="hessianTestImpl"></property>
<property name="serviceInterface" value="com.xxx.hessiantest.HessianTest"></property>
</bean>
</beans>
打成jar包并发布服务
客户端编写测试代码
不带spring
import com.caucho.hessian.client.HessianProxyFactory;
import com.xxx.hessiantest.HessianTest;
public class TestHessianInterface {
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/Hessian_Server/hessian/hello";
HessianProxyFactory factory = new HessianProxyFactory();
HessianTest hessianTest = (HessianTest) factory.create(HessianTest.class, url);
String hello = hessianTest.sayHello("阿紫");
System.out.println(hello);
}
}
带Spring
客户端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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="testHessian" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/Hessian_Server/hessian/hello"></property>
<property name="serviceInterface" value="com.xxx.hessiantest.HessianTest"></property>
</bean>
</beans>
测试类:
@Test
public void testHessian(){
ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml");
HessianTest bean = (HessianTest) context.getBean("testHessian");
String sayHello = bean.sayHello("阿紫");
System.out.println(sayHello);
}
输出结果
hello 阿紫