• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

spring constructor autowiring

 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anadi Misra
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Misra
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic