我正在尝试将 Spring 从 XmlApplicationContext
迁移到 AnnotationConfigApplicationContext
(更多信息:Java-based container configuration)。
一切正常,但我不知道如何创建 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 配置应该是什么样子的?我还需要这个 Factory 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();
}
最佳答案
实际上,正确的(和等效的)版本会更加尴尬:
public @Bean HttpInvokerProxyFactoryBean httpInvokerProxy() {
HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
proxy.setServiceInterface(AccountService.class);
proxy.setServiceUrl("http://remotehost:8080/remoting/AccountService");
return proxy;
}
(毕竟您通常希望 FactoryBean 由 Spring 管理,而不是它返回的 Bean)
请参阅这篇最近的文章以供引用:
关于java - 作为 Java 配置 Bean 的 Spring HttpRemoting 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7268151/