This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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?
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.
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
posted
0
aah !! saw your reply after I hit the add reply button Mark. thatnks for clearing the air out for me too
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.
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.
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 ]
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.
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.