You are right... it is working, the problem appears when you use
withAutomatic() instead of withAnnotatedFieldInjection().
In my case I need both annotation and constructor injection. Do you
Jonathan.
Is the following representative ...............
Post by Jonathan ArielI seems to still have problems with this... and it is when I have
C extends B that extends A
B and A are abstract
in A I'm injecting D
when getting an instance of C from the container, D is null.
On Wed, Nov 26, 2008 at 1:24 PM, Jonathan Ariel
I agree. I'll change my code. Thanks! Good reasons :)
Well five is a bit borderline, yes. Firstly, if you're using IDEA
for looking after your component set, then using the refactoring
"introduce parameter" can help lots - it you do that in the super
class, all subclasses will get it too. Then there's "Change method
signature" which IDEA and Eclipse have.
Alternatively, the purists may study the five components that are
dependencies and wonder if there is some separation of concerts.
They would try to look to see if there is some intermediate
dependency that could encompass the capabilities of, say, three of
the components. With the introduction of the intermediate
component, you would see a reduce number of deps going into the
final one. Of course, that's easy for me to say not having seen
your set of components.
You know you always have all your dependencies when you get an
instance of your component from the container. You don't have to
write any "afterInitialized" method where you check that you really
have all your dependencies set -- Spring does this, I don't know
about Guice. For testing it's particularly nice because you know
all your required dependencies by looking at the constructor. With
setter injection, it's up to the programmer to look at all the bean
properties and make sure they're all set in the test.
Constructor injection is even more compelling when you are adding
new dependencies to existing classes -- the IDE will remind you
that your test objects are missing dependencies (because they are
missing a constructor parameter) whereas setter injection will
probably cause the test to fail at runtime with a
NullPointerException.
Re: Too many dependencies
As Paul said, if you have too many dependencies, there's probably
some other abstraction which is trying to come out. Just like
testing with mock objects can tell you if your interfaces need
rethinking ("Why do I have to mock all these calls to get my code
to work?"), long constructor lists can tell if you if you have too
many or unusual dependencies.
And yes, a good IDE will help you deal with the mechanics of
introducing new parameters, moving them, etc.
Dan