创建web工程并加载springhessian框架
创建service:
[java]
public interface BasicService {
public void setServiceName(String serverName)
public String getServiceName()
public User createUser()
}
创建service实现
public class BasicServiceImpl implements BasicService {
private String serviceName;
@Override
public void setServiceName(String serverName) {
thisserviceName = serverName;
}
@Override
public String getServiceName() {
return thisserviceName;
}
@Override
public User createUser() {
return new User(zhangsan )
}
}
创建需要传递的对象
[java]
public class User implements Serializable {
private static final long serialVersionUID = L;
private String username;
private String password;
public User(String username String password) {
thisusername = username;
thispassword = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
}
配置webxml利用dispatchServlet处理请求
[html]
<?xml version= encoding=UTF?>
<webapp xmlns:xsi=//XMLSchemainstance xmlns=/xml/ns/javaee xmlns:web=/xml/ns/javaee/webapp__xsd xsi:schemaLocation=/xml/ns/javaee /xml/ns/javaee/webapp__xsd id=WebApp_ID version=>
<displayname>HessianSpringServer</displayname>
<servlet>
<servletname>remote</servletname>
<servletclass>orgspringframeworkwebservletDispatcherServlet</servletclass>
<initparam>
<paramname>contextConfigLocation</paramname>
<paramvalue>classpath:com/loujinhe/config/remoteservletxml</paramvalue>
</initparam>
<loadonstartup></loadonstartup>
</servlet>
<servletmapping>
<servletname>remote</servletname>
<urlpattern>/remote/*</urlpattern>
</servletmapping>
</webapp>
配置remoteservletxml:
[html]
<?xml version= encoding=UTF?>
<beans xmlns=/schema/beans
xmlns:xsi=//XMLSchemainstance
xsi:schemaLocation=/schema/beans
/schema/beans/springbeansxsd>
<bean id=basicService class=comloujinheserviceimplBasicServiceImpl/>
<bean id=hessianRemote name=/HessianRemote class=orgspringframeworkremotingcauchoHessianServiceExporter>
<property name=serviceInterface value=comloujinheserviceBasicService/>
<property name=service ref=basicService/>
</bean>
</beans>
创建客户端调用工程并加载springhessian框架
创建service和普通需要传递的对象
配置remoteclientxml
[html]
<?xml version= encoding=UTF?>
<beans xmlns=/schema/beans
xmlns:xsi=//XMLSchemainstance
xsi:schemaLocation=/schema/beans
/schema/beans/springbeansxsd>
<bean id=basicService class=comloujinheserviceimplBasicServiceImpl/>
<bean id=hessianRemote name=/HessianRemote class=orgspringframeworkremotingcauchoHessianServiceExporter>
<property name=serviceInterface value=comloujinheserviceBasicService/>
<property name=service ref=basicService/>
</bean>
</beans>
创建客户端测试程序
[java]
public class RemoteTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(com/loujinhe/config/remoteclientxml)
BasicService basicService = (BasicService) contextgetBean(hessianRemoteCall)
basicServicesetServiceName(hello service)
Systemoutprintln(basicServicegetServiceName())
Systemoutprintln(basicServicecreateUser()getUsername())
Systemoutprintln(basicServicecreateUser()getPassword())
}
}
启动服务器执行客户端测试程序结果如下
hello service
zhangsan