首先贴出客户端方面的BEAN配置信息:
         <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
<property name="serviceUrl" value="rmi://192.168.0.109:1099/helloService"/> 
<property name="serviceInterface" value="com.rmiClient.HelloWorldInterface"/> 
<!-- setting refresh connect -->  
     <property name="refreshStubOnConnectFailure" value="true"></property>  
        <property name="lookupStubOnStartup" value="false"></property>  
</bean> 
    我想做的是,动态修改serviceUrl的地址,也就是存在多个服务的情况下,我必须修改地址,然后调用其相应的远程服务。在测试例子中,这样做是可以的:
    ApplicationContext context = new ClassPathXmlApplicationContext("xpring.xml");
RmiProxyFactoryBean helloService = (RmiProxyFactoryBean)context.getBean("&helloService");
这样可以获得其代理对象,然后setServiceUrl()改变调用RMI地址。    但如果我是注入在业务BEAN里面,该怎么获取呢?例如:
    public class ABusinessService {

@Autowired(required=true)
private RmiProxyFactoryBean helloService;

public String execute(){
return helloService.getServiceUrl();
}
} 然后尝试ApplicationContext context = new ClassPathXmlApplicationContext("xpring.xml");
ABusinessService a = (ABusinessService)context.getBean("businessService");
System.out.println(a.execute());
执行到execute()后,报空指针了。很显然,在业务类中注入的helloService没有被实例化或者干脆不存在。而在实际项目中,请问应该怎样进行注入才能有(RmiProxyFactoryBean)context.getBean("&helloService");等同这样的效果。 希望我说得大家能明白。

解决方案 »

  1.   

    <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 修改里面的class啊
      

  2.   

    既然是注入了,就不用执行下面的来获取
    ApplicationContext context = new ClassPathXmlApplicationContext("xpring.xml");
    ABusinessService a = (ABusinessService)context.getBean("businessService");
            System.out.println(a.execute());直接用helloService就是了
      

  3.   

    我上面的看错了,是不是没有get/set方法
      

  4.   

    public class ABusinessService {
        
        @Autowired(required=true)
        private RmiProxyFactoryBean helloService;//这里不对,要写接口
        
        public String execute(){
            return helloService.getServiceUrl();
        }
    }
      

  5.   

    businessService是个业务类,类似于MVC中封装的一些后台操作,后台操作中里面注入了一个RMI客户端调用对象,这个对象,因为采用代理模式,无法正常地获取。我通过API的方法里在getBean()的时候,参数里面要加&这个符号,可以获取代理对象。但是实际项目过程中,我用的是注入的方式,    @Autowired(required=true)
        private RmiProxyFactoryBean helloService;
    这个时候,我发现在调用到此业务BEAN时,helloService代理对象里获取不到serviceUrl,我如果要调用setServiceUrl()方法同样也没用。所以,我想做成在注入的时候,能达到这样执行的效果:(RmiProxyFactoryBean)context.getBean("&helloService");
      

  6.   


    那你知道,RMI 客户端生成所用的代理类的接口是什么吗
      

  7.   

    好吧,结贴吧,主动建议的都有分数。官方只提供了通过API解决的方法,想打算用SPRING 表达式,发现不支持。