开发者

Spring HttpRemoting client as a Java Configuration Bean

开发者 https://www.devze.com 2023-04-01 00:49 出处:网络
I\'m trying to migrate Spring from XmlApplicationContext to AnnotationConfigApplicationContext (more info: Java-based container configuration).

I'm trying to migrate Spring from XmlApplicationContext to AnnotationConfigApplicationContext (more info: Java-based container configuration).

Everything works perfectly but I don't know how to create a HttpInvoker client. The XML configuration is a开发者_C百科s follows:

<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>

How should the Java Configuration look? Do I still need this Factory Bean? I think one should be able to instantiate the client without this wrapper with this configuration method.

This (somehow) feels bad to me:

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();
}


Actually, the correct (and equivalent) version would be the even more awkward:

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

(After all you usually want the FactoryBean to be managed by Spring, not the Bean it returns)

See this recent article for reference:

What's a FactoryBean?

0

精彩评论

暂无评论...
验证码 换一张
取 消