频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
Spring配置Hessian
2013-01-08 11:21:20      个评论      
收藏   我要投稿
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="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

点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇:Hessian
下一篇:java.lang.IllegalStateException: Web app root system property already set to different value
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站