<!-- Hessian服务的接口-->
<property name="serviceInterface" value="com.demo.api.server.DemoService" />
</bean>
在该配置下,Hessian服务的url是
http://IP:port/**/remote/HessianService,其中**是该应用的url。Spring使用DispatcherServlet拦截到匹配/remoting/*的请求,然后将该请求转发到对应的bean,该bean在dispatcher-servlet.xml文件中以HessianServiceExporter定义。
二.客户端
同样我们创建工程和代码
Web project&code
public interface DemoService {
List getUsers();
}
下面注意了:
创建可序列化对象,就是Server端的那个syncobject,而且要implements Serializable。这个也是我们要远程对象调用的domain类。
public class SyncObject implements Serializable {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
private String username="";
private String password="";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
配置文件config.xml
<bean id="hessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!--服务器端发布的地址URL-->
<property name="serviceUrl" value="http://ip:port/**/remote/HessianService" />
<!--本地接口à
<property name="serviceInterface" value="com.demo.api.client.DemoService"/>
</bean>
最后我们写个测试方法:
public static void main(String[] args) {
final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
DemoService service = (DemoService)context.getBean("hessianService");
try {
List<SyncObject> userlist= service.getUsers();
for (SyncObject user : userlist) {
System.out.println(user.getUsername());
}
}
catch(RemoteException e) {
e.printStackTrace();
}
}
Run一把试试,(确保服务器端runing状态哦)
下面是成功后的信息,结果是从服务器端取到了2个用户的username。
2008-12-31 10:31:15 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
2008-12-31 10:31:16 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
2008-12-31 10:31:16 org.springframework.context.support.AbstractRefreshableApplicationContext refreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=11988197]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [hessianService]; root of BeanFactory hierarchy
2008-12-31 10:31:16 org.springframework.context.support.AbstractApplicationContext refresh
信息: 1 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=11988197]
2008-12-31 10:31:16 org.springframework.context.support.AbstractApplicationContext initMessageSource
信息: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@b8f82d]
2008-12-31 10:31:16 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
信息: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@b1b4c3]
2008-12-31 10:31:16 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [hessianService]; root of BeanFactory hierarchy]
2008-12-31 10:31:16 org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
信息: CGLIB2 not available: proxyTargetClass feature disabled
username1 from server
username2 from server