• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dependency checking and constructor injection

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Spring's dependency checking feature checks only if a property has been injected via the setter method. Even if the particular property has been set by the bean via constructor injection it will still complain about UnsatisfiedDependencyException. What is the reason?

Another interesting thing about constructor vs. property injection, I observed was,
If there is both constructor and property injection in the bean, property injection takes precedence irrespective of the order of injection in the configuration file..

Can someone shed light of these 2 things?

Thanks,
Suhas.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Even if the particular property has been set by the bean via constructor injection it will still complain about UnsatisfiedDependencyException.


It should not. Please show your example.

If there is both constructor and property injection in the bean, property injection takes precedence irrespective of the order of injection in the configuration file..


How could this be the opposite ? You could not call setters before instanciating the bean (=before calling the constructor).

Also, from 3.3.1.2. Setter Injection:
"The BeanFactory supports both of these variants for injecting dependencies into beans it manages. (It in fact also supports injecting setter-based dependencies after some dependencies have already been supplied via the constructor approach.)"
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Christopher.

About the 2 points, I understood the second point from your explanation. If you have both constructor and property injection in the bean definition, constructor injection run first and then the property injection (which could potentially overwrite properties set in constructor injection). If only properties are being set, the class better have a default no-arg constructor - either explicit/implicit.

Thanks again...

I am looking at point 1 again and will reply to it
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Christopher,

Here's what I tried

bean config entry



The class com.apress.springrecipes.sequence.SequenceGenerator has these two properties:
prefix
suffix

It also has a constructor


As I understand, the prefix property is set using constructor injection, while the suffix via property injection. But just because, I had dependency-check set to simple in the bean declaration, I am forced to do property injection for both the properties, when I already had injected one of the properties via constructor injection.


I understand this behavior, I am just wondering why the behavior? Why doesnt constructor injection account for missing property injection declarations? (I might be answering my own question....but may be constructor injection might not inject all simple properties???)

Suhas.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there should be no problem here. If all properties are injected, whether by constructor or by setter, the dependency check will not fail. Please show me your whole SequenceGenerator class.
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure..here it is

The component:


The bean config: (the dependency-check attribute here complains about UnsatisfiedDependencyException, works fine if it is not specified)


The driver class:
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Christopher, did you get a chance to look at the source I provided?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like the setter setPrefix is messing things up. It will work if you comment out the setter. I can't tell if this is the right behaviour, but it looks like if there is a setter for a property, Spring assumes that it should be called to resolve the dependency.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Dependency resolution process in the doc says : "For each bean, its dependencies are expressed in the form of properties, constructor arguments, or arguments to the static-factory method if you are using that instead of a normal constructor. These dependencies are provided to the bean, when the bean is actually created."

I still can't say anything for sure, but it looks like in that case, the setter is going to be part of the dependency checking, so if it is not called, the dependency check will fail. The point is : don't use both setter and constructor injection for the same property
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True. Using constructor and property injection simultaneously will really need some justification. But just the case that it is possible to do it, I think why? why is there a scope to make it confusing....thats all
 
reply
    Bookmark Topic Watch Topic
  • New Topic