Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mark Anthony Ranullo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring is able to identify that RmiProxyFactoryBean is a factory bean as it implements the FactoryBean interface.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Anthony Ranullo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic