Announcement Announcement Module
No announcement yet.
This an Autowire question Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

    Any thoughts.

  • #2
    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.


    • #3
      There is the thought as you have mentioned that the Constructor may have work to perform and the injection at the property wouldn't.


      • #4
        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.


        • #5
          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.


          • #6
            Good luck to you on that....

            and Happy POSTing..