<div class="iteye-blog-content-contain" style="font-size: 14px">
简单的Spring+hessian
Jar包:
dist\modules里面的 spring-webmvc.jar
lib\caucho 里面的hessian-3.1.3.jar
Server:
里面有个接口interface:
public interface IBasic {
public String helloWorld();
public User getUser();
}
接口的实现:
public class IBasicServer implements IBasic {
@Override
public String helloWorld() {
return "hello world!!";
}
@Override
public User getUser() {
User user =new User();
user.setName("nie");
user.setPassword("123");
return user;
}
}
建立一个model层:(实现Serializable接口)
public class User implements Serializable {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String name;
private String password;
}
Web.xml里面配置:
<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>
在WEB-INF下面创建一个remoting-servlet.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.5.xsd">
<bean id="accountService" class="nie.wei.qing.Server.implment.IBasicServer">
</bean>
<bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="nie.wei.qing.Server.IBasic"/>
</bean>
</beans>
客户端同样的要有interface IBasic 和model层 User(实现Serializable接口)
在src下面建立一个remote-client.xml
<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/Hissian-Server/remoting/AccountService"/>
<property name="serviceInterface" value="nie.wei.qing.Server.IBasic"/>
</bean>
使用JUnit来测试;测试代码:
try
{
ApplicationContext context = new ClassPathXmlApplicationContext("remote-client.xml");
IBasic basic =(IBasic)context.getBean("accountService");
System.out.println(basic.helloWorld());
}
catch (Exception e)
{
e.printStackTrace();
}</div>
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐