zoukankan      html  css  js  c++  java
  • spring 的延迟加载

    今天用spring的rmi,启动似乎不注册rmi服务。最后手工的加载bean,成了。

     

        晕,在配置头的时候设置default-lazy-init="true",延迟加载的说。

     

        顺便贴代码

        服务器端配置文件

    <!-- 服务 -->
        <bean id="myService" class="org.spring.rmi.yoara.MyServiceImpl"/>
        
        <!-- RmiServiceExporter 此类封装了生成stub的方法,大大简化代码 -->
        <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
            <!-- 服务 -->
            <property name="service" ref="myService"/>
            <!-- 服务名 -->
            <property name="serviceName" value="MyService"/>
            <!-- 绑定服务的路径 -->
            <property name="serviceInterface" value="org.spring.rmi.yoara.MyService"/>
            <!-- 服务端口 -->
            <property name="registryPort" value="1199"/>

        </bean> 

    服务pojo随便写就成了。

     

        客户端配置文件

    <bean id="goService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
            <!-- 服务URL -->
            <property name="serviceUrl" value="rmi://127.0.0.1:1199/MyService"/>
            <!-- 本地接口路径 -->
            <property name="serviceInterface" value="org.spring.rmi.yoara.MyService"/>

        </bean> 

     客户端调用代码

    测试连接:
    //        try {
    //            System.out.println(Naming.lookup("rmi://localhost:1199/MyService"));
    //        } catch (Exception e) {
    //            e.printStackTrace();
            }


    第一种:

            ClassPathXmlApplicationContext cxc = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            MyService client = (MyService)cxc.getBean("goService");
            client.showInfo();

    第二种:

    //        RmiProxyFactoryBean proxy=new RmiProxyFactoryBean();            
    //        proxy.setServiceInterface(MyService.class);
    //        proxy.setServiceUrl("rmi://localhost:1199/MyService");
    //        proxy.afterPropertiesSet();
    //        MyService client=(MyService)proxy.getObject();    

    //        client.showInfo(); 

  • 相关阅读:
    Spark集群下的K-Means算法
    牛客:数据库SQL实战(一)查询入职最晚的员工的所有信息
    吴军《硅谷之谜》
    IntelliJ IDEA 中 右键新建(new)时,选项没有scala(java) class的解决方法和具体解释
    Gitlab跨版本升级
    Kubernetes 0-1 Pod中的livenessProbe和readinessProbe解读
    Kubernetes 0-1 了解Pod
    Kubernetes 0-1 K8S自建LoadBalancer
    Kubernetes 0-1 K8S部署coredns
    Kubernetes 0-1 二进制搭建K8S(四)部署Node
  • 原文地址:https://www.cnblogs.com/chenying99/p/2555578.html
Copyright © 2011-2022 走看看