spring集成hession使用了好久了,便是框架这种东西搭完也就放哪了,再重新搭一遍的时候之前做过的内容,还要慢慢回忆,在这里记录一下吧。 首先不管是直接复制jar包还是使用maven管理总之包不能少(这个不用多说) 先添加web.xml配置:
<servlet>
<servlet-name>hessianServices</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-hessian.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessianServices</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
设置 拦截servlet,集成hession到spring,引入spring-hession.xml配置文件 spring-hession.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="false">
<bean name="/helloService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="hello" />
<property name="serviceInterface" value="com.xx.Hello" />
</bean>
</beans>
hello 是配置的spring配置文件(或者注解形式)的bean(是Hello接口的实现方法) com.xx.Hello是要对外提供方法的接口 服务端基本ok了 调用方法:
String url = "http://127.0.0.1:8080/App/hessian/helloService";
HessianProxyFactory hpf = new HessianProxyFactory();
try {
Hellomss = (Hello) hpf.create(Hello.class, url);
int i = mss.find("xxx");
System.out.println("xxx:"+i);
} catch (MalformedURLException e) {
e.printStackTrace();
}
如果返回值类型为自定义的bean要保证bean实现Serializable接口,定义serialVersionUID属性值