Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Spring know where to get the proxy object needs to get injected?

Tags:

java

spring

To my impression, RmiProxyFactoryBean is supposed to produce a proxy object instance that is of type AccountService that will be injected to accountService property of SimpleObject instance in the following code.

What I do not understand is why does the XML file seem to instruct Spring to inject an object of RmiProxyFactoryBean type to accountService property of SimpleObject object instance? I'm expecting an explicit instruction from the XML file that tells the Spring how to get an AccountService instance from RmiProxyFactoryBean instance instead of injecting an RmiProxyFactoryBean instance. I find this confusing.

public class SimpleObject {

    private AccountService accountService;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }
}


<bean class="example.SimpleObject">
    <property name="accountService" ref="accountService"/>
</bean>

<bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceUrl" value="rmi://HOST:1199/AccountService"/>
    <property name="serviceInterface" value="example.AccountService"/>
</bean>

Source: http://static.springsource.org/spring/docs/2.5.x/reference/remoting.html (see 17.2.2.)

Thanks

like image 475
supertonsky Avatar asked Sep 30 '11 02:09

supertonsky


People also ask

How does proxy work in Spring?

Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice). If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used.

How proxy design is implemented in Spring?

Spring uses JDK Dynamic proxy or CGLIB to generate proxy, based on the target class and configuration. JDK Dynamic proxy uses interface implementation for creating a proxy. While CGLIB creates proxy by creating a subclass of the target class. The full source code can be found on Github.

Where Proxy pattern is used in Spring?

Proxy design pattern provides an object of class that has the functionality of another class. This pattern comes under the structural design pattern of GOF design patterns. According to GOF pattern, Provide a surrogate or placeholder for another object to control access to it.

What is Spring proxy object?

A proxy is an object that wraps another object maintaining its interface and optionally providing additional features. Proxies usually delegate behavior on the real object they are proxying but can execute code around the call to the wrapped object.


2 Answers

Because Proxy Beans wrap the object in question, they pretend to be the interface that is being called (and then later actually call that interface).

The RmiProxyFactoryBean returns a proxy object so that when your code thinks it is calling a method on your example.AccountService class, it's actually being called on a bean with the same interface as your example.AccountService interface as specified here:

<bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    ...
    <property name="serviceInterface" value="example.AccountService"/>
</bean>

Say there was a method on the interface called example.AccountService.reconcile(Long amount) (just for sake of example)...

If you called this normally the method calling it would just push it into the stack. But if the example.AccountService object is returned from the RmiProxyFactoryBean as it is above, it will be wrapped in a proxy bean. This proxy bean has a method inside of it that is also named reconcile and also has an argument of Long amount, which makes it indistinguishable from the original example.AccountService bean which it wraps. This way the proxy bean can have code that it runs, before, after, or instead of the code that is in the actual example.AccountService.reconcile(Long amount) method.

Proxy beans come from a whole different way of programming called Aspect Oriented Programming (AOP) which deals with cross-cutting-concerns; i.e. code that doesn't clearly seem to fit within the confines of normal OOP separation of concerns. Some of these concerns are For instance, transaction demarcation, security, and logging.

It appears that you are doing this rather manually above, but it's sometimes easier to weave these in at runtime using SpringAOP which has a language for selecting and applying proxy beans to existing code.

However, take note to program to interfaces rather than to program to just classes.

like image 113
leeand00 Avatar answered Sep 25 '22 20:09

leeand00


A more succinct way of putting it is that the RmiProxyFactoryBean implements the FactoryBean interface. Implementing that interface instructs the Spring bean factory that this class is a factory itself, causing Spring to invoke the getObject() method on that class and return the result as the bean, rather than creating an instance of the class itself and returning.

So, short answer, it's a built-in mechanic of Spring.

like image 20
pap Avatar answered Sep 23 '22 20:09

pap