2011-09-01 45 views
8

我試圖將Spring從XmlApplicationContext遷移到AnnotationConfigApplicationContext(更多信息:Java-based container configuration)。作爲Java配置Bean的Spring HttpRemoting客戶端

一切正常,但我不知道如何創建一個HttpInvoker客戶端。 XML配置如下:

<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> 
    <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/> 
    <property name="serviceInterface" value="example.AccountService"/> 
</bean> 

Java配置應該如何查看?我還需要這個工廠Bean嗎?我認爲應該能夠在沒有這個包裝器的情況下通過這種配置方法實例化客戶端。

這(某種程度上)的感覺對我不好:

public @Bean AccountService httpInvokerProxy() { 
    HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean(); 
    proxy.setServiceInterface(AccountService.class); 
    proxy.setServiceUrl("http://remotehost:8080/remoting/AccountService"); 
    proxy.afterPropertiesSet(); 
    return (AccountService) proxy.getObject(); 
} 
+0

相關閱讀:http://blog.springsource.com/2011/08/10/beyond-the-factorybean –

回答

8

其實,正確的(和等效)版本將是更加尷尬:

public @Bean HttpInvokerProxyFactoryBean httpInvokerProxy() { 
    HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean(); 
    proxy.setServiceInterface(AccountService.class); 
    proxy.setServiceUrl("http://remotehost:8080/remoting/AccountService"); 
    return proxy; 
} 

(畢竟通常希望FactoryBean由Spring管理,不是Bean返回)

查看此最新文章以供參考:

What's a FactoryBean?