Mike Minicki
Mike Minicki

Reputation: 8456

Spring HttpRemoting client as a Java Configuration Bean

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

Upvotes: 8

Views: 3914

Answers (2)

Dipak
Dipak

Reputation: 29

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

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

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?

Upvotes: 8

Related Questions