Spring 中的 RMIPRoxyFactoryBean 工厂?
我目前正在使用 Spring RmiProxyFactoryBean
来访问远程服务。由于需求发生了变化,我需要在运行时指定不同的主机 - 可以有很多 - ,但 remoteServiceInterface
和 remoteServiceUrl
的非主机组件仍然存在相同。
从概念上讲,我会看到一个类似于以下内容的 bean 定义:
<bean class="org.springframework.remoting.rmi.RmiProxyFactoryBeanFactory">
<property name="serviceInterface" value="xxx"/>
<property name="serviceUrl" value="rmi://#{HOST}:1099/ServiceUrl"/>
</bean>
它公开了
Object getServiceFor(String hostName);
Spring 是否有这样的服务可用?或者,您是否看到另一种方法?
请注意,主机列表在编译或启动时不是未知的,因此我无法在 xml 文件中生成它。
I'm currently using a Spring RmiProxyFactoryBean
to access remote services. Since requirements have changed, I need to specify at runtime a different host - there can be many of them - , but the remoteServiceInterface
and the non-host components of the remoteServiceUrl
remain the same.
Conceptually speaking, I'd see a bean definition similar to:
<bean class="org.springframework.remoting.rmi.RmiProxyFactoryBeanFactory">
<property name="serviceInterface" value="xxx"/>
<property name="serviceUrl" value="rmi://#{HOST}:1099/ServiceUrl"/>
</bean>
which exposes a
Object getServiceFor(String hostName);
Is there such a service available with Spring? Alternatively, do you see another way of doing this?
Please note that the host list will not be known at compile or startup time, so I can't generate it in the xml file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看 RmiProxyFactoryBean 的源代码,您会发现它是 RmiClientInterceptor 的一个非常薄的子类,而 RmiClientInterceptor 只是一个标准的 AOP MethodInterceptor。这对我来说意味着您可以编写一个自定义类来实现您想要的 getServiceFor(hostname) 方法,并且该方法可以以与 RmiProxyFactoryBean 类似的方式使用 Spring ProxyFactory 来生成运行时代理对于您的特定主机。
例如:
其中
rmiServiceInterface
和proxyInterface
是您定义的类型。If you look at the source for RmiProxyFactoryBean, you can see that it's a very thin subclass of RmiClientInterceptor, which is just a standard AOP MethodInterceptor. This suggests to me that you could write a custom class which implements your desired
getServiceFor(hostname)
method, and this method could use a Spring ProxyFactory in a similar way to RmiProxyFactoryBean, to generate a run-time proxy for your specific host.For example:
Where
rmiServiceInterface
andproxyInterface
are types defined by you.我最终实现了类似的东西:
当然,涉及一些健全性检查和缓存,但我忽略了它们。
I ended up implemeting something similar to:
Of course, there is some sanity checking and caching involved, but I've ommited them.