欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用hessian简单使用【续】- 与spring结合使用

程序员文章站 2022-05-28 16:26:57
...

hessian与spring结合使用

依旧使用前面用到的两个项目servicepro与clientpro,导入相应的jar包,之前已经导入了hessian,此处只导入spring用到的jar包与aopalliance.jar,注:导入工程的jar有些可能不需要。

一、配置服务器端(servicepro工程)

1.在web.xml中需要进行如下配置

<servlet>
	<servlet-name>remoting</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param><!--指定spring加载的文件具体位置,可以任意定义,默认为WEB-INF下,默认名称为${servlet-name}-servlet.xml (即remoting-servlet.xml)-->
        	<param-name>contextConfigLocation</param-name>
        	<param-value>classpath:remote-service.xml</param-value>
    	</init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>remoting</servlet-name>
	<url-pattern>/service/*</url-pattern>
</servlet-mapping>

  Spring的DispatcherServlet可以将请求转发到Hessian服务

 

2.创建相应的接口与接口实现类

a.接口HessianHelloWorld

package com.remote;

public interface HessianHelloWorld {
	String target();
}

 b.接口实现类

package com.remote.impl;

import com.caucho.hessian.server.HessianServlet;
import com.remote.HessianHelloWorld;

public class HessianHelloWorldImpl extends HessianServlet implements
		HessianHelloWorld {
	private static final long serialVersionUID = -489831024851039867L;

	@Override
	public String target() {
		return "target:one piece";
	}

}

 

3.在工程的源代码根目录下面创建remote-service.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
	default-lazy-init="true">

	<!-- 声明具体实现的累 -->
	<bean id="helloWorld" class="com.remote.impl.HessianHelloWorldImpl" />
	<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务-->
	<bean name="/remote"<!--客户端访问时的路径-->
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="helloWorld" /><!-- 需要导出的目标bean--> 
		<property name="serviceInterface" value="com.remote.HessianHelloWorld" /><!-- Hessian服务的接口--> 
	</bean> 

</beans>

 

 通过Tomcat启动servicepro工程

项目整体结构如附件中图片:service-2.jpg

 

二、配置客户端(clientpro工程)

此处用到的HessianHelloWorld通过服务器端(servicepro工程)创建并已jar文件的形式导出,最后添加到客户端(clientpro工程)中

1.在spring文件中配置远程bean

a.在工程的源代码根目录下面创建remote-bean.xml文件

b.添加相应的配置,沿用之前的接口HessianHelloWorld

<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
	default-lazy-init="true">
	<bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl">
			<value>http://localhost:8080/servicepro/service/remote</value>
		</property>
		<property name="serviceInterface">
			<value>com.remote.HessianHelloWorld</value>
		</property>
	</bean>
</beans>

 

2.创建测试用例ClientBySpringTest

package com.client.remote.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.remote.HessianHelloWorld;

public class ClientBySpringTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("remote-bean.xml"); 
		HessianHelloWorld hello = (HessianHelloWorld)context.getBean("myService");
        System.out.println(hello.target());
	}
}

通过上下文类加载配置文件remote-bean.xml中的内容,获取的bean的名称“myService”是在配置文件中定义的bean的id

项目整体结构如附件中图片:clientpro-2.jpg

 

当服务器端通过spring配置,客户端若不需要使用spring时,同样可以使用纯hessian,也能够正常访问。反过来也是一样的

 

其他参考资料

http://www.iteye.com/topic/14887

http://www.blogjava.net/freeman1984/archive/2010/01/27/310999.html