众所周之 ejb中的远程用是相当的不错,但是ejb毕竟是一个重型机器,而spring就提供了一辆轻型单车,
很多人都能骑,不是所有人都能去开。今天我学习了一下spring中结合hessian的一个远程调用的例子,希望给
刚学习的人作一下参考,所以我在这里把我的学习经历和大家分享一下,希望能帮助到刚学习的人。在这里我尽
量的详细一点,不至于能得头昏眼花。
我用的开发工具是eclipse,有myeclipse更好,服务器我用了tomcat6.0。
上网下载hessian的jar包,一定要和spring现有的版本所支持,就是说hessian的版本不能比spring的新,要不
然在编译时会出错,所以要注意,下面就来建工程了,首先是建立服务器端的工程,在eclipse中new一个web
project,把spring的一些基本的包引进项目中最重要的是要部romote的包引进来,还有下载的hessian包,基本
的环境搭建好了后就开始重要的事情了:
新建一个bean对象并序列化,用于数据传输的,主要是被客户端调用的一个实体类里面有get和set方法
package com;
import java.io.Serializable;
public class Dog implements Serializable {
private Integer id;
private String name;
public Dog (){
}
public Dog (Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
建立一个接口,这个接口主要是用于暴露给客户端,客户端获取这个接口并调用这个接口中相应的方法
package com;
public intterface IregisterService(){
public void register(Dog dog);
}
package com;
public class RegisterService implements IregisterService(){
public void register(Dog dog){
Dog d=new Dog();
d.set(dog.getId());
d.set(dog.getName());
}
}
配置spring文件,把实现远程接口的实现类注入到spring的applicationcontext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="RegisterService" class="com.RegisterService"></bean>
</beans>
在web-info目录下的web.xml配置spring的配置和监听和servlet名等,如下:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>Hession.root</param-value>
</context-param>
<context-param>
<description>Spring配置文件位置</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<context-param>
<description>Spring日志文件位置</description>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<description>Spring配置加载器</description>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
还要在此目录下增加一个remoting-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">
<!--
- Dispatcher servlet for HTTP remoting via Hessian, Burlap, and Spring's
- HTTP invoker (see remoting-servlet.xml for the controllers).
-->
<beans>
<bean name="/registerService-hessian"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="orderService"/>
<property name="serviceInterface"
value="com.RegisterService"/>
</bean>
</beans>
服务器端的配置完了,下面就到clent端的编写了,客户端主要是以测试问主,主要是写一个测试类,看一下它
是怎么样连接上服务器和传输数据的,在客户端也要配置spring的,把spring的包引进来,下面就开始了
先配置spring的文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="registerService"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"
value="http://127.0.0.1:8989/springServer/remoting/registerService-hessian"/>
<property name="serviceInterface"
value="com.RegisterService"/>
</bean>
</beans>
在这里我先解释一下上面的内容的由来,因为开始时我也是被上面的内容搞到头晕
serviceUrl这个属性的值http://127.0.0.1:8989/springServer/remoting/registerService-hessian,首先是
127.0.0.1:8989中127.0.0.1是服务器的ip地址,这里的ip是主机的ip,8989是http端口这个要在使用的服务器
中定义了,tomcat默认的是8080端口,springServer是定义的web工程的名称,remoting是在web中定义的
servlet名,registerService-hessian是在remoting-servlet.xml中定义的名称。
写一个test类用来连接服务器
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestOrder {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void testOrder(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");
IregisterService registerService=(IOrderService) ctx.getBean("orderService");
Dog dog=new Dog();
dog=new .setId(5);
dog=new .setName("shit");
registerService.register(dog);
System.out.println("successful");
}
}
在这里就有疑问了,客户端的IregisterService这个接口哪里得到?很简单你可以直接从服务器端拷贝到客户端
,或者在eclipse中建立工程引用就ok了,至于怎么建立引用点工程右击-properties-projects-Add把引用的工
程加进去就可以引用到这个工程的类了。这里是使用到junit进行测试,只用把junit包倒进去就好了。这是我第
一次在此写文章,写得比较急促,请各位指教!