博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中HttpInvoker远程调用使用实例
阅读量:6952 次
发布时间:2019-06-27

本文共 3004 字,大约阅读时间需要 10 分钟。

hot3.png

代码结构图如下:

09153752_tsOy.jpg

客户端通过Spring的HttpInvoker,完成对远程函数的调用。涉及的类有:

09153752_jxQI.jpg
客户端调用User类的服务UserService,完成对实现类UserServiceImpl的addUser(User u)方法调用。其中User类为普通Pojo对象,UserService为接口,UserServiceImpl为UserService的具体实现。代码如下:
public interface UserService {
    void addUser(User u);
}

public class UserServiceImpl implements UserService {

    public void addUser(User u) {
        System.out.println("add user ["+u.getUsername()+ "] ok !!!");
    }
}

客户端调用时,主方法代码为:

public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] {"ApplicationContext.xml" });
    
        UserService us = (UserService)ctx.getBean("ServletProxy");
        us.addUser(new User("Hook1"));
        
        UserService us2 = (UserService)ctx.getBean("UrlPathProxy");
        us2.addUser(new User("Hook2"));
    }

其调用流程用时序图可表示为:

09153752_zlRr.jpg
图中粉红色表示基于Url映射方式的配置时程序的处理流程,红色表示基于Servlet配置时的处理流程。
当以示基于Url映射方式的配置时,远程系统处理请求的方式同SpringMVC的controller类似,所有的请求通过在web.xml中的org.springframework.web.servlet.DispatcherServlet统一处理,根据url映射,去对应的【servlet名称-servlet.xml】文件中,查询跟请求的url匹配的bean配置;而基于Servlet配置时,由org.springframework.web.context.support.HttpRequestHandlerServlet去拦截url-pattern匹配的请求,如果匹配成功,去ApplicationContext中查找name与servlet-name一致的bean,完成远程方法调用。

当使用URL映射配置时,实力配置如下(application-servlet.xml):

<bean name="/userHttpInvokerService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="userService"/>
        <property name="serviceInterface" value="com.handou.httpinvoker.service.UserService"/>
    </bean>

web.xml文件配置:

 <servlet>
        <servlet-name>application</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
     
    <servlet-mapping>
        <servlet-name>application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

 如果使用基于Servlet的配置,web.xml文件配置如下:

    <!-- 基于servlet配置时使用 ,根据请求的url匹配url-pattern,如果匹配成功,去ApplicationContext
    中查找name与servlet-name一致的bean-->
    <servlet>
        <servlet-name>userHttpInvokerService</servlet-name>
        <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>userHttpInvokerService</servlet-name>
        <url-pattern>/UserHttpInvokerService</url-pattern>
    </servlet-mapping>

applicationContext.xml文件中配置如下:

<bean id="userService" class="com.handou.httpinvoker.service.UserServiceImpl" />
    <!--第二种配置方式 --> 
    <bean name="userHttpInvokerService" 
        class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="userService"/>
        <property name="serviceInterface" value="com.handou.httpinvoker.service.UserService"/>
    </bean>

 

两种方式,客户端配置均相同:

<bean id="ServletProxy" 
     class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
     <property name="serviceUrl">  
           <value>http://localhost:8080/HttpInvoke/UserHttpInvokerService</value>  
       </property>  
       <property name="serviceInterface">  
            <value>com.handou.httpinvoker.service.UserService</value>  
        </property>  
   </bean>

具体可参考源码 :

转载于:https://my.oschina.net/fangshaowei/blog/167437

你可能感兴趣的文章
算法题!大家可以贡献答案哦!
查看>>
此文是2013年应届生实习时,集中培训班的最后,个人写给大家的话
查看>>
JVM致命错误日志(hs_err_pid.log)解读
查看>>
一个老司机工程师整理的自动化测试资料
查看>>
单机环境搭建Postgres-XC开发测试环境
查看>>
三: 推荐系统
查看>>
PHP文件上传-单文件上传函数
查看>>
jvmtop 监控
查看>>
使用JMH进行并发测试
查看>>
关于服务器 SAN 和 SDS
查看>>
ASP.NET 如何做出简单的验证码
查看>>
我的友情链接
查看>>
Spring 转换 model 为 json 时增加属性
查看>>
最新在线软件测试模拟题,做完答案立显,自我检测好机会!
查看>>
论坛PC端模板
查看>>
域名解析
查看>>
通过SNMP获取接口速率 32位与64位的区别
查看>>
Windows上用gcc编译SQLite3
查看>>
bash位置参数的简介
查看>>
VirtualBox导入其他虚拟机后网络问题
查看>>