二、hessian与spring集成
spring提供了与hessian的集成,这样就会大大减少代码的嵌入和编写,让我们让关注与自身的业务,spring的org.springframework.remoting.caucho.HessianServiceExporter类提供了hessian的代理,下面开始hessian于spring的简单集成。
1、准备spring和hessian的相关依赖包
spring的依赖包
hessian依赖包 hessian-4.0.7.jar(可以自行去hessian官网下载)
2、编写服务接口api和实现
api类
api实现
3、编写hessian与spring集成的xml
通过org.springframework.remoting.caucho.HessianServiceExporter代理hessian,我命名为hessian-remote-servlet.xml(注意命名和web.xml中servlet配置有关系,否则找不到xml配置文件)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="basicService" class="com.jimi.hessian.test.impl.BasicService"/>
<bean name="/helloHessian" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="basicService"/>
<property name="serviceInterface" value="com.jimi.hessian.test.api.Basic"/>
</bean>
</beans>
4、配置web.xml文件,加载hessian-remote-servlet.xml文件
<!-- hessian与servlet集成 -->
<servlet>
<servlet-name>hessian-remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian-remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
5、编写与spring集成的hessian客户端
在res下建立remote-client.xml文件
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/hessian/remote/helloHessian"/>
<property name="serviceInterface" value="com.jimi.hessian.test.api.Basic"/>
</bean>
</beans>
结构图如下:
6、编写java客户端代码
使用ClassPathXmlApplicationContext加载remote-client.xml文件
public class HessianClient {
final static String url = "http://localhost:8080/hessian/remote/helloHessian";
public static void main(String[] args) throws Exception {
//hessian工程代理
// HessianProxyFactory factory = new HessianProxyFactory();
// Basic basic = (Basic) factory.create(url);
// System.out.println(basic.hello("jimi"));
//spring集成
ApplicationContext context = new ClassPathXmlApplicationContext("remote-client.xml");
//从spring容器中获取hessian代理
Basic basic = (Basic) context.getBean("clientSpring");
System.out.println(basic.hello("good boy"));
}
}
输出结果如下 hello good boy
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)