zoukankan      html  css  js  c++  java
  • Hessian与Spring整合

    1.服务端与Spring的整合

    1.1:web.xml中配置控制器

        <servlet>
            <servlet-name>hessian</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            
            <!-- Spring的配置 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>hessian</servlet-name>
            <url-pattern>/hessian/*</url-pattern>
        </servlet-mapping>

    1.2:编写接口和实现类

    接口:

    package com.hessian.service;
    
    import com.hessian.domain.User;
    
    public interface HessianFunc {
            
        public String getAddressByMobille(String phone);
        
        public void saveUser(User user);
        
    }

    实现类:

    package com.hessian.service;
    
    import com.hessian.domain.User;
    
    public class HsessianFuncImpl implements HessianFunc {
    
        @Override
        public String getAddressByMobille(String phone) {
            
        String result="手机号"+phone+"的归属地是上海.....";
            
        return result;
        }
    
        @Override
        public void saveUser(User user) {
            
            System.out.println(user.getName()+"---"+user.getAge());
        }
    }

    需要一个实体类:

    package com.hessian.domain;
    
    import java.io.Serializable;
    
    public class User implements Serializable{
    
        /**
         * 一定要实例化
         */
        private static final long serialVersionUID = 1L;
        private Integer id;
        private String name;
        private Integer age;
        
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
        }
    }

    1.3:编写applicationContext.xml  交给Spring管理

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <bean id="/mobile" class="org.springframework.remoting.caucho.HessianServiceExporter">
            <!-- 接口类型 -->
            <property name="serviceInterface" value="com.hessian.service.HessianFunc"></property>
            <!-- 接口对象 -->
            <property name="service" ref="HsessianFuncImpl"></property>
        </bean>
        
        <bean id="HsessianFuncImpl" class="com.hessian.service.HsessianFuncImpl"></bean>
        
    </beans>

    访问地址:http://localhost:8080/HessionSpringServer/hessian/mobile

    2.客户端与Spring整合:

    2.1:因为要用到User和接口,这里包装成jar包即可 

    2.2:配置applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!--创建代理工厂的核心对象-->
        <bean id="hessianProxy" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!--服务接口--> <property name="serviceInterface" value="com.hessian.service.HessianFunc"></property>
        <!--服务地址--> <property name="serviceUrl" value="http://localhost:8080/HessionSpringServer/hessian/mobile"></property> </bean> </beans>

    2.3:编写测试代码:

    package com.hessian.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.hessian.domain.User;
    import com.hessian.service.HessianFunc;
    
    public class TestHessian {
        public static void main(String[] args) {
            
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            
            HessianFunc func = (HessianFunc) ac.getBean("hessianProxy");
            
            String mobille = func.getAddressByMobille("1888888888");
            
            System.out.println(mobille);
            
            User user = new User();
            user.setAge(12);
            user.setName("jack");
            
            func.saveUser(user);
        }
    }
  • 相关阅读:
    FusionCharts数据展示成饼状图、柱状图和折线图
    Js获取request中的对象的属相值
    在grid结果集中实现全选或全不选某些特定的行
    JQuery的一些基础知识
    查询的数据插入不到临时表中的原因
    Javascript获取页面表格中的数据
    ajax实现菜单联动显示信息(当选择单位的时候,动态关联出人员信息)
    form表单只提交数据而不进行页面跳转的解决方案
    NotSupportedException-无法将类型“System.DateTime”强制转换为类型“System.Object”
    LINQ to SQL语句
  • 原文地址:https://www.cnblogs.com/lichangyun/p/8687518.html
Copyright © 2011-2022 走看看