Constructor Injection vs Property Setter Injection ??

We need to set the record straight on this issue. There is ACTUALLY on such thing as Constructor Injection in Spring IOC framework  It is surprising that even Spring themselves use such anomalous terminology.

You have to admit that semantic precision dictates that the mechanism of declaring a component within a dependency context, by itself is not yet "injection". An injection occurs only when the contextual component is @Inject''ed (or the deprecated term @Autowired) into various sections of the code.

To be more precise, all the @Inject-able components need to have gone thro constructor enabled instantiation. Therefore, all cases are indeed constructor-instantiated-component injection.

The more precise phrasing is, component instantiation, where either
  • Component instantiation thro constructor argument.
  • Component instantiation thro null-argument constructor, in conjunction with property setter(s).
  • Combination of both - Component instantiation thro constructor argument, in conjunction with property setter(s).
We could declare the scope of the component either as
  • singleton
  • prototype
And additional scopes for web/mvc
  • request
  • session 
  • globalSession
We can then perform cascaded component declaration by using the ref keyword in place of values, to reference existing component declaration within the same context.

Injection then can be done thro two strategies
  • @Inject annotated on setter method
  • @Inject annotated directly on a field declaration.
The above clarification is demonstrated in a previous page that explains XML-based component declaration and injection .

But this argument becomes moot when we use Spring component annotation directly on the Java code of the bean itself.

Also, be reminded that using the @Autowired annotation should be avoided, but should use the Java standard @Inject annotation. They do the same thing, where @Autowired is Spring's terminology, whereas, @Inject is standard Java JSR 330.


3 comments:

  1. I found your blog on Google and read a few of your other posts. I just added you to my Google News Reader. You can also visit Injection molding China for more plastic mold & injection molding service related information and knowledge, Keep up the great work Look forward to reading more from you in the future.

    ReplyDelete
  2. Hi there to everybody, it’s my first go to see of this web site; this weblog consists of awesome and in fact good stuff for visitors. Hurrah, that’s what I was exploring for, what stuff! Existing here at this blog, thanks admin of this web site. You can also visit Injection Mold Design Engineering for more Technodam related information and knowledge.

    ReplyDelete
  3. I read your post and this blog is very good. You have good knowledge on this. This post really impressed me. Thank you for sharing your knowledge with all of us. mold damage cleanup

    ReplyDelete