Spring HttpRemoting client as a Java Configuration Bean
Asked Answered
T

2

8

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 as 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();
}
Turnspit answered 1/9, 2011 at 9:2 Comment(1)
Relevant reading: blog.springsource.com/2011/08/10/beyond-the-factorybeanSubdue
S
8

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?

Subdue answered 1/9, 2011 at 10:52 Comment(0)
V
0

Setup up an HttpInvoker client for AccountService without the need for XML. The httpInvokerClient() method creates the client, configuring it with the service interface and URL.

The accountService() method retrieves the actual AccountService proxy object from the HttpInvokerProxyFactoryBean. This way, you can use the AccountService bean throughout your application.

By using this configuration, you can easily inject and use the AccountService in your code without XML-based setup. Feel free to ask if you have any further questions!

@Configuration
public class AppConfig {

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

    @Bean
    public AccountService accountService() {
        return (AccountService) httpInvokerClient().getObject();
    }
}
Vanzandt answered 6/11, 2023 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.