我有很多接口(interface)。其中每个都定义了模块的一组操作。 我需要将这些接口(interface)用作远程 RMI 接口(interface),但我不想在原始接口(interface)定义中指定这一点。
我可以手动(对于每个操作接口(interface))创建一个扩展“java.rmi.Remote”和我的操作接口(interface)的新接口(interface)。 有没有办法以动态的方式做到这一点?
我不知道我的解释是否正确。如有任何问题,请随时提出。
最佳答案
实现此目的的一种方法是使用 Spring 的 RMI 远程处理功能。它将让您公开一个 java 接口(interface),这些接口(interface)不是通过所谓的 RMI 调用程序从 java.rmi.Remote 派生的。
所有文档都在这里:
您只需在 spring 中注册您的接口(interface)实现,并使用 org.springframework.remoting.rmi.RmiServiceExporter 通过 RMI 公开您的接口(interface)实现。
<bean id="accountService" class="example.AccountServiceImpl">
<!-- any additional properties, maybe a DAO? -->
</bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- does not necessarily have to be the same name as the bean to be exported -->
<property name="serviceName" value="AccountService"/>
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
<!-- defaults to 1099 -->
<property name="registryPort" value="1199"/>
</bean>
完全不打扰,但你必须喝 Spring 的酷爱饮料。但这是一件坏事吗?
对于真正的动态解决方案,您可以使用 java 或 groovy 在代码中构建应用程序上下文。您可以迭代您的接口(interface),然后在代码中构造适当的 RmiServiceExporter 实例。
以下任一方法都可以处理此问题:
http://static.springframework.org/spring-javaconfig/docs/1.0.0.M4/reference/html/
关于Java - 动态接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30744781/