如何使用Spring Annotations实现远程处理?

时间:2015-03-05 21:15:11

标签: spring service frameworks annotations remoting

我有一个基本的Spring框架Web应用程序设置,我开始从基于XML的配置切换到使用注释。我的服务器和webclient在不同的机器上,所以我一直在使用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中声明每一项服务?)

1 个答案:

答案 0 :(得分:3)

客户端:

@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;
  }
}