【问题标题】:How do I implement Remoting with Spring Annotations?如何使用 Spring Annotations 实现远程处理?
【发布时间】:2023-11-18 13:47:01
【问题描述】:

我已经设置了一个基本的 Spring 框架 Web 应用程序,并且我开始从基于 XML 的配置切换到使用注释。我的服务器和 web 客户端在不同的机器上,所以我一直在使用 Spring HttpInvokerServiceExporter 来启用它们之间的远程处理。

客户:

<bean id="accountService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
    <property name="serviceUrl" value="${server.url}/AccountService"/>
    <property name="serviceInterface" value="com.equinitixanite.knowledgebase.common.service.AccountService"/>
</bean>

服务器:

<bean name="accountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>

我的问题是,我如何用注释做同样的结果? (我的意思是,我怎样才能避免在 XML 中声明每一个服务?)

【问题讨论】:

    标签: spring service frameworks annotations remoting


    【解决方案1】:

    客户:

    @Configuration
    public class ClientConfiguration {
      @Value("${server.url}") 
      private String serverUrl;
    
      @Bean
      public HttpInvokerProxyFactoryBean httpInvokerProxy() {
        HttpInvokerProxyFactoryBean httpInvoker = new HttpInvokerProxyFactoryBean();
        httpInvoker.setServiceUrl(serverUrl + "/AccountService");
        httpInvoker.setServiceInterface(AccountService.class);
        return httpInvoker;
      }
    }
    

    服务器:

    @Configuration
    @ComponentScan
    public class ServerConfiguration {
      @Bean
      public HttpInvokerServiceExporter accountServiceExporter(AccountService accountService) {
        HttpInvokerServiceExporter httpInvokerServiceExporter =
            new HttpInvokerServiceExporter();
        httpInvokerServiceExporter.setService(accountService);
        httpInvokerServiceExporter.setServiceInterface(AccountService.class);
        return httpInvokerServiceExporter;
      }
    }
    

    【讨论】:

      最近更新 更多