aspose file tools*
The moose likes Spring and the fly likes How does the Spring know where to get the proxy object needs to get injected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How does the Spring know where to get the proxy object needs to get injected?" Watch "How does the Spring know where to get the proxy object needs to get injected?" New topic
Author

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

Mark Anthony Ranullo
Greenhorn

Joined: Dec 21, 2010
Posts: 6
Hi everyone,

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






Yes, Spring supports autowiring by name, type, etc. But in the given code, the "RmiProxyFactoryBean" itself is being injected as "AccountService" type to SimpleObject"'s accountService property, which of course is not of the same type as the target of injection. I don't understand how could Spring possibly know that it's a factory and that it need not assign the factory itself but get a proxy instance from the factory and inject it. It really looks counter intuitive to me. It looks like it's trying to assign the factory itself and not the instance coming from the factory.

Thanks

Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

Spring is able to identify that RmiProxyFactoryBean is a factory bean as it implements the FactoryBean interface.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Rishi Shehrawat wrote:Spring is able to identify that RmiProxyFactoryBean is a factory bean as it implements the FactoryBean interface.


Right and if you look at the JavaDoc for a FactoryBean it would be clear how this works,

Interface to be implemented by objects used within a BeanFactory which are themselves factories. If a bean implements this interface, it is used as a factory for an object to expose, not directly as a bean instance that will be exposed itself.

NB: A bean that implements this interface cannot be used as a normal bean. A FactoryBean is defined in a bean style, but the object exposed for bean references (getObject() is always the object that it creates.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Mark Anthony Ranullo
Greenhorn

Joined: Dec 21, 2010
Posts: 6
Sam Mercs wrote:
Rishi Shehrawat wrote:Spring is able to identify that RmiProxyFactoryBean is a factory bean as it implements the FactoryBean interface.


Right and if you look at the JavaDoc for a FactoryBean it would be clear how this works,

Interface to be implemented by objects used within a BeanFactory which are themselves factories. If a bean implements this interface, it is used as a factory for an object to expose, not directly as a bean instance that will be exposed itself.

NB: A bean that implements this interface cannot be used as a normal bean. A FactoryBean is defined in a bean style, but the object exposed for bean references (getObject() is always the object that it creates.


Perfect, exactly what I'm looking for. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does the Spring know where to get the proxy object needs to get injected?