当前位置:编程学习 > JAVA >>

Spring配置Hessian

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"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://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="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.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="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.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");  
    &nbs
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,