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"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xmlns:web="https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HessianSpringServer</display-name>
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/loujinhe/config/remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
</web-app>
5.配置remote-servlet.xml:
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="basicService" class="com.loujinhe.service.impl.BasicServiceImpl"/>
<bean id="hessianRemote" name="/HessianRemote" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="serviceInterface" value="com.loujinhe.service.BasicService"/>
<property name="service" ref="basicService"/>
</bean>
</beans>
6.创建客户端调用工程,并加载spring、hessian框架
7.创建service和普通需要传递的对象
8.配置remote-client.xml
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="basicService" class="com.loujinhe.service.impl.BasicServiceImpl"/>
<bean id="hessianRemote" name="/HessianRemote" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="serviceInterface" value="com.loujinhe.service.BasicService"/>
<property name="service" ref="basicService"/>
</bean>
</beans>
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