File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Dependency checking and constructor injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Dependency checking and constructor injection" Watch "Dependency checking and constructor injection" New topic
Author

Dependency checking and constructor injection

Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.)"

[My Blog]
All roads lead to JavaRanch
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
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

Joined: May 16, 2006
Posts: 95
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: May 16, 2006
Posts: 95
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

Joined: May 16, 2006
Posts: 95
Hey Christopher, did you get a chance to look at the source I provided?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: May 16, 2006
Posts: 95
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependency checking and constructor injection