使用 RMI 进行 Spring 依赖注入

发布于 2024-11-19 01:19:37 字数 775 浏览 1 评论 0原文

假设我们有 2 个项目 ProjectA(前端)和 ProjectB(后端),以及 ProjectA 中的 2 个类 ClassA 和 ProjectB 中的 ClassB。 现在我需要获取ClassA中ClassB的实例。 问题是如何使用Spring和RMI组织ClassB到ClassA的注入? spring.xml 中需要添加什么? 我是java新手,所以如果你能提供带有示例的答案。 提前致谢!!

public void initializeManager() {
  InitialContext context = null;
  if (manager == null) {
    try {
      Properties props = TaxFormsConfiguration.getInstance().getProperties();
      context = new InitialContext(props);
      manager = (EFormsManager) context.lookup("taxsystem/EFormsManagerFacade/remote");
      if (manager == null) {
        throw new RuntimeException("EFormsManager is null.");
      }
    } catch (Exception e) {
      logger.error("error in EFormsActionManager.initializeManager: " + e.getMessage());
    }
  }
}

suppose we have 2 projects ProjectA(front end) and ProjectB(back end) and 2 classes ClassA in ProjectA and ClassB in ProjectB.
Now I need to get the instance of ClassB in ClassA.
The question is how to organize injection of ClassB into ClassA by using Spring and RMI? What to add into spring.xml?
I'm new in java so if you can provide the answer with exampls please.
Thanks in advance!!

public void initializeManager() {
  InitialContext context = null;
  if (manager == null) {
    try {
      Properties props = TaxFormsConfiguration.getInstance().getProperties();
      context = new InitialContext(props);
      manager = (EFormsManager) context.lookup("taxsystem/EFormsManagerFacade/remote");
      if (manager == null) {
        throw new RuntimeException("EFormsManager is null.");
      }
    } catch (Exception e) {
      logger.error("error in EFormsActionManager.initializeManager: " + e.getMessage());
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2024-11-26 01:19:37

检查用于远程处理的 Spring 参考。 Yuu 可以在那里找到一个示例(第 19.2 章使用 RMI 公开服务),

您将在 XML 中声明您的 bean 并通过 Spring 应用程序上下文获取它们,而不是上下文查找,例如:

ApplicationContext context = new ClassPathXmlApplicationContext(yourxmlname);
EFormsManager eFormsManager= (EFormsManager)context.getBean("eFormsManager");

RMI 服务器端的 XML:

<bean id="eFormsManager" class="example.EFormsManagerImpl">
    <!-- 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="EFormsManagerService"/>
    <property name="service" ref="eFormsManager"/>
    <property name="serviceInterface" value="example.EFormsManager"/>
    <!-- defaults to 1099 -->
    <property name="registryPort" value="1199"/>
</bean>

RMI 客户端的 XML:

<bean id="eFormsManager" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceUrl" value="rmi://HOST:1199/EFormsManagerService"/>
    <property name="serviceInterface" value="example.EFormsManager"/>
</bean>

Check Spring reference for remoting. Yuu can find an example there (chapter 19.2 Exposing services using RMI)

Instead of context lookup you will declare your beans in XML and get them via Spring application context, for example:

ApplicationContext context = new ClassPathXmlApplicationContext(yourxmlname);
EFormsManager eFormsManager= (EFormsManager)context.getBean("eFormsManager");

XML on RMI server side:

<bean id="eFormsManager" class="example.EFormsManagerImpl">
    <!-- 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="EFormsManagerService"/>
    <property name="service" ref="eFormsManager"/>
    <property name="serviceInterface" value="example.EFormsManager"/>
    <!-- defaults to 1099 -->
    <property name="registryPort" value="1199"/>
</bean>

XML on RMI client side:

<bean id="eFormsManager" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceUrl" value="rmi://HOST:1199/EFormsManagerService"/>
    <property name="serviceInterface" value="example.EFormsManager"/>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文