首先贴出客户端方面的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");等同这样的效果。 希望我说得大家能明白。
<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");等同这样的效果。 希望我说得大家能明白。
ApplicationContext context = new ClassPathXmlApplicationContext("xpring.xml");
ABusinessService a = (ABusinessService)context.getBean("businessService");
System.out.println(a.execute());直接用helloService就是了
@Autowired(required=true)
private RmiProxyFactoryBean helloService;//这里不对,要写接口
public String execute(){
return helloService.getServiceUrl();
}
}
private RmiProxyFactoryBean helloService;
这个时候,我发现在调用到此业务BEAN时,helloService代理对象里获取不到serviceUrl,我如果要调用setServiceUrl()方法同样也没用。所以,我想做成在注入的时候,能达到这样执行的效果:(RmiProxyFactoryBean)context.getBean("&helloService");
那你知道,RMI 客户端生成所用的代理类的接口是什么吗