Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 725
Mike Minicki Avatar asked Sep 01 '11 09:09

Mike Minicki


People also ask

How can we achieve serialization in Java through HTTP using Spring?

Spring provides a special remoting strategy which allows for Java serialization via HTTP, supporting any Java interface (just like the RMI invoker). The corresponding support classes are HttpInvokerProxyFactoryBean and HttpInvokerServiceExporter. Hessian.

Can we have two beans with same name in Spring?

Spring beans are identified by their names within an ApplicationContext. Therefore, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext that has the same name as another bean. It works by simply replacing the former bean in case of a name conflict.

What is HTTP invoker?

HttpInvoker is a Spring-specific remoting option that essentially enables Remote Procedure Calls (RPC) over HTTP. In order to accomplish this, an outbound representation of a method invocation is serialized using standard Java serialization and then passed within an HTTP POST request.


1 Answers

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?

like image 63
Sean Patrick Floyd Avatar answered Oct 13 '22 02:10

Sean Patrick Floyd