Spring让Hessian变得不但强大而且易用但是易用背后却有不少陷阱!
这个例子很简单但实际上的确花费了我超过一小时的时间排除了种种问题最后问题终于水落石出
整合以上篇Hello Hessian为基础加入Spring框架进行改进
一环境
jdk
mirror/tomcat/tomcat/v/bin/apachetomcatzip
frameworkSECwithdependencieszip
顺便说下如果不说环境版本很难保证你的程序在别的版本下能运行
二整合
写Spring的发布Hessian服务的配置文件
hessianservletxml
<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
<bean id=defaultHandlerMapping class=orgspringframeworkwebservlethandlerBeanNameUrlHandlerMapping/>
<bean id=helloService class=lavasoftsuthsserviceHelloService/>
<bean name=/hello class=orgspringframeworkremotingcauchoHessianServiceExporter>
<property name=service ref=helloService/>
<property name=serviceInterface value=lavasoftsuthsserviceHello/>
</bean>
</beans>
配置webxml
<?xml version= encoding=UTF?>
<webapp xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd
version=>
<contextparam>
<paramname>contextConfigLocation</paramname>
<paramvalue>
/WEBINF/hessianservletxml
</paramvalue>
</contextparam>
<servlet>
<servletname>hessian</servletname>
<servletclass>orgspringframeworkwebservletDispatcherServlet</servletclass>
<loadonstartup></loadonstartup>
</servlet>
<servletmapping>
<servletname>hessian</servletname>
<urlpattern>/hessian/*</urlpattern>
</servletmapping>
</webapp>
陷阱
a)hessianservletxml的文件名必须以<servletname>hessian</servletname>名字开头并且加上servletxml一段组成完整的文件名
b)hessianservletxml的文件名格式必须是[servletname]servletxml格式否则出错
三部署应用
因为涉及到类加载顺序问题好用IDEA启动Tomcat测试老失败不知道为啥!这次不用IDEA嵌入式启动Tomcat了直接自己部署测试
部署后启动界面如下
三写测试
这次测试可以在上个例子的基础上进行修改来测试根据上面的配置那么请求HelloService的URL应该是//localhost/hessianapp/hessian/hello
package lavasoftsuthsserviceclient;
import comcauchohessianclientHessianProxyFactory;
import lavasoftsuthsserviceHello;
import MalformedURLException;
/**
* 客户端调用(会依赖服务接口)
*
* @author leizhimin ::
*/
public class Client {
public static void main(String[] args) throws MalformedURLException {
String url = //localhost:/hessianapp/hessian/hello;
HessianProxyFactory factory = new HessianProxyFactory();
Hello hello = (Hello) factorycreate(Helloclass url);
Systemoutprintln(hellosayHello(Hessian));
}
}
运行结果
Hello Hessian!
Process finished with exit code
还有一种测试方法就是在客户端也使用Spring需要做个配置remotingclientxml
<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
<bean id=helloServiceClient class=orgspringframeworkremotingcauchoHessianProxyFactoryBean>
<property name=serviceUrl value=//localhost:/hessianapp/hessian/hello/>
<property name=serviceInterface value=lavasoftsuthsserviceHello/>
</bean>
</beans>
然后写个测试类
package lavasoftsuthsserviceclient;
import lavasoftsuthsserviceHello;
import orgntextApplicationContext;
import orgntextsupportClassPathXmlApplicationContext;
/**
* Spring整合Hessian客户端测试
*
* @author leizhimin ::
*/
public class TestClient {
public static void main(String[] args) {
try {
ApplicationContext context = new ClassPathXmlApplicationContext(/remotingclientxml);
Hello hello = (Hello) contextgetBean(helloServiceClient);
Systemoutprintln(hellosayHello(Spring Hession));
}
catch (Exception e) {
eprintStackTrace();
}
}
}
运行结果
Hello Spring Hession!
Process finished with exit code
陷阱实际上看着代码好好程序有时候还是不能跑原因是Hessian的版本问题这里推荐使用Spring自带的版本就不会有问题了
整个工程所以依赖的包
logjjar
springaopjar
springbeansjar
springcontextjar
springcontextsupportjar
springcorejar
springjdbcjar
springjmsjar
springormjar
springtestjar
springtxjar
springwebjar
springwebmvcjar
springwebmvcportletjar
springwebmvcstrutsjar
hessianjar
aopalliancejar
commonsloggingjar