Dec 19th, 2012, 02:01 AM
This an Autowire question
On my team a question came up>
Which is most efficient to Autowire through the Constructor or Property?
So when the class gets created the Constructor is called and any beans to be injected are handled.
Spring will Autowire the bean after the class is initialized.
Why care well my team is trying to settle on one or the other some have settled on the Constructor others the Property.
Dec 19th, 2012, 08:23 AM
Well, to begin with, none of your teams knows spring too well. First, let me claim that it doesn't matter in 98% cases. Spring is a container which manages some/more/most of the boiler-plate code for you and nothing beyond that. When using any type of injection, when the bean its being created, all its dependencies would have to be pre-created. and that means, the lazy attribute doesn't help much as far as constructor/setter based injection is concerned. What matters most is whats written in constructor. If there's some time-consuming operation in constructor, any type of injection, will look similar. How much time it takes to create a normal object on a quad-core machine with 4/6/8GB of RAM, it is less than couple of milliseconds. On a typical production app, this time is minimal of the whole application life. So doesn't matter. Imagine a web application, which is expected to run 24*7 having initialization time of either 45 or 60 seconds. I don't think it matters in either case.
Now, why I said none of your teams know spring well, cause this is probably the most TRIVIAL of the issues to discuss when different teams are involved. When the WORK actually starts, there're better things to discuss and IDEALLY, teams should be looking forward to that rather.
This is only personal opinion and nothing official about it.
Dec 19th, 2012, 09:13 AM
There is the thought as you have mentioned that the Constructor may have work to perform and the injection at the property wouldn't.
Dec 19th, 2012, 09:45 AM
Sorry, misunderstood I am, no way a constructor loaded with work can be better in any way whether or not constructor based inject is used.......the point is.....if constructor does minimal work then setter-based/constructor-based is equivalent and if it doesn't, even then it is equivalent, cause the constructor will take whatever time it requires......the point is to move ahead and try to create the other magic and/or pleasure using spring rather than getting stuck on what injection to prefer cause its anyway going to be less than 99.99% time of the overall application life.
Dec 19th, 2012, 10:00 AM
Well we are .....Looking toward the magic... it's just that for a moment we said to our selves Hmmm which is better, is Spring doing something we have overlooked while focusing on the "magic". With all the Boiler Work going on its easy to overlook something which is why typos get into code.
Dec 19th, 2012, 12:24 PM
Good luck to you on that....
and Happy POSTing..