File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes spring constructor autowiring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "spring constructor autowiring" Watch "spring constructor autowiring" New topic
Author

spring constructor autowiring

Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

if there are multiple constructors in a bean and more than one qualify for autowiring, is Spring at liberty to pick one of them or should it throw an exception?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Please explain further with examples. Nevermind.

Are you creating the class with multiple constructors? My question would be why have multiple constructors. Why not have one constructor that takes only the mandatory parameters, and those parameters that are optional use setter methods?

A parameter to a constructor really says that that parameter is mandatory for the object, if it is not set, then the object won't work.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Anadi Misra
Ranch Hand

Joined: Jun 03, 2008
Posts: 69
hmm ... am not sure but more than one constructor qualifying for autowiring is rather unusual actually.
Wouldn't that mean that you have two same constructors in your class?


Anadi Mishra.
Anadi Misra
Ranch Hand

Joined: Jun 03, 2008
Posts: 69
aah !! saw your reply after I hit the add reply button Mark. thatnks for clearing the air out for me too
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

From the book Spring in Action (2nd Ed)
If a constructor has multiple constructors, any of which can be satisfied by autowiring, Spring will not attempt to guess which constructor to use.

I am not sure if the phrasing is as it was intended!

My question is from a What-if perspective. If I have an existing bean having constructors Bean(A), Bean(A, B), Bean (A, B, C), Bean (A, D) and A, B, C and D are all present in the context, which constructor will be used for wiring? Now, the quote above suggests this should not work but I didn't see any exceptions.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Abhinav Srivastava:
From the book Spring in Action (2nd Ed)

I am not sure if the phrasing is as it was intended!

My question is from a What-if perspective. If I have an existing bean having constructors Bean(A), Bean(A, B), Bean (A, B, C), Bean (A, D) and A, B, C and D are all present in the context, which constructor will be used for wiring? Now, the quote above suggests this should not work but I didn't see any exceptions.


Yes, there would be an exception because if Spring can't create the object when creating the ApplicationContext an exception is thrown.

Mark
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

No exceptions at all. I am using 2.5 . It looks like it tries to find the constructor with maximum number of parameters that it can wire.
[ October 03, 2008: Message edited by: Abhinav Srivastava ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I don't think that it is documented but Spring looks for constructors in the following order : public constructors first, with decreasing number of arguments, then non-public constructors, again with decreasing number of arguments. I find it dangerous to rely on that kind of behaviour.


[My Blog]
All roads lead to JavaRanch
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Christophe Verre:
I don't think that it is documented but Spring looks for constructors in the following order : public constructors first, with decreasing number of arguments, then non-public constructors, again with decreasing number of arguments. I find it dangerous to rely on that kind of behaviour.


Yeah, I think I would have liked to see an Exception thrown. But you need to have some kind of rules, and I am sure that rule is flexible that you can configure it to work differently. You can always create a BeanFactoryPostProcessor or BeanPostProcessor to overwrite that functionality, if need be.

Mark
 
 
subject: spring constructor autowiring
 
Similar Threads
Spring DI Vs Spring Autowiring
Is not autowire a another type of dependendcy injection?
Spring-Autowiiring
spring autowiring by name
Interface injection in Spring