Spring配置Hessian

2014-11-24 11:07:24 · 作者: · 浏览: 2
1.创建web工程,并加载spring、hessian框架
2.创建service:
[java]
public interface BasicService {
public void setServiceName(String serverName);
public String getServiceName();
public User createUser();
}
2.创建service实现:
public class BasicServiceImpl implements BasicService {
private String serviceName;
@Override
public void setServiceName(String serverName) {
this.serviceName = serverName;
}
@Override
public String getServiceName() {
return this.serviceName;
}
@Override
public User createUser() {
return new User("zhangsan", "123456");
}
}
3.创建需要传递的对象:
[java]
public class User implements Serializable {
private static final long serialVersionUID = 5792818254468116836L;
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = 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;
}
}
4.配置web.xml,利用dispatchServlet处理请求:
[ html]
< xml version="1.0" encoding="UTF-8" >
HessianSpringServer
remote
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:com/loujinhe/config/remote-servlet.xml
1
remote
/remote/*
5.配置remote-servlet.xml:
[html]
< xml version="1.0" encoding="UTF-8" >
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.xsd">
6.创建客户端调用工程,并加载spring、hessian框架
7.创建service和普通需要传递的对象
8.配置remote-client.xml
[html]
< xml version="1.0" encoding="UTF-8" >
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.xsd">
9.创建客户端测试程序:
[java]
public class RemoteTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/loujinhe/config/remote-client.xml");
BasicService basicService = (BasicService) context.getBean("hessianRemoteCall");
basicService.setServiceName("hello service");
System.out.println(basicService.getServiceName());
System.out.println(basicService.createUser().getUsername());
System.out.println(basicService.createUser().getPassword());
}
}
10.启动服务器,执行客户端测试程序,结果如下:
hello service
zhangsan
123456