Java - 动态接口实现

时间:2015-06-09 23:45:49

标签: java interface rmi

我有很多接口。然后,每个定义一组模块的动作。 我需要将这些接口用作远程RMI接口,但我不想在原始接口定义中指定它。

我可以手动(对于每个动作界面),创建一个新的接口,扩展' java.rmi.Remote'和我的动作界面。 有没有办法以动态的方式做到这一点?

我不知道如果我正确地解释自己。随意提出任何问题。

1 个答案:

答案 0 :(得分:1)

这样做的一种方法是使用Spring的RMI远程处理功能。它将允许您通过他们称之为RMI调用者的方式公开不从java.rmi.Remote下降的java接口。

所有文档都在这里:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/remoting.html#remoting-rmi-server

您只需在spring中注册接口实现,并使用org.springframework.remoting.rmi.RmiServiceExporter通过RMI公开您的接口实现。

<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>

完全没有侵入性,但你必须喝春天的kool-aid。这是件坏事吗?

对于真正动态的解决方案,您可以使用java或groovy在代码中构建应用程序上下文。您可以遍历您的接口,然后在代码中构造正确的RmiServiceExporter实例。

这些方法中的任何一种都可以处理:

http://static.springframework.org/spring-javaconfig/docs/1.0.0.M4/reference/html/

http://grails.org/Spring%2BBean%2BBuilder