1、首先把hessian的jar包下载下来,该处用的maven管理jar包
<dependency>
<groupId>com.sptsm.unicom.tsm.hessian</groupId>
<artifactId>sptsm-unicom-tsm</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
2、web.xml配置如下:
<!-- 配置Spring监听hessian远程访问,即对外暴露Hessian接口 -->
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
3、接口类和实现类:
package com.gstd.service.business;
public interface ILoginBusiness {
public String sayHello();
}
package com.gstd.service.business.impl;
import org.springframework.stereotype.Component;
import com.gstd.service.business.ILoginBusiness;
@Component("loginBusiness")
public class LoginBusinessImpl implements ILoginBusiness {
@Override
public String sayHello(String arg) {
System.out.println("完成调用----");
return "hello:"+arg;
}}
4、hessian-servlet.xml的配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 以Hessian方式向外暴露的接口 -->
<bean name="/loginBusiness" //name这个值是在调用时用到
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="loginBusiness" />//ref这个值是通过注解后纳入容器的bean的一个ID
<property name="serviceInterface"
value="com.gstd.service.business.ILoginBusiness" />//value这个值是实现ref对应的实现类的接口地址
</bean>
</beans>
5、之后可以在main方法中直接验证是否可成功调用:
package com.gstd.service;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.gstd.service.business.ILoginBusiness;
public class A {
/**
* @param args
*/
public static void main(String[] args) {
String url = "http://localhost:8080/hessian/loginBusiness"; //hessian是在web.xml中<url-pattern>/hessian/*</url-pattern>的配置的
//而loginBusiness则是在hessian-servlet.xml中配置bean的id的值。
HessianProxyFactory factory = new HessianProxyFactory();
ILoginBusiness hello = null;
try {
// hello.sayHello("黄晓明");
System.out.println(hello.sayHello("黄晓明"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
hello.sayHello("黄晓明");//在eclipse的Console中会打印出 hello:黄晓明
}
}
完事!简单、强悍、直观。。。之后会说在spring中怎么调用hessian接口