Jacek Laskowski wrote:
Hi,
While reviewing Raj's patch for a setter injection example I spot that
the private method was used with the @Resource dependency annotation.
As I've been thinking about it for a while before I saw it in Raj's
patch it's a good time to ask here and find out where I was wrong.
To me, using non-public methods with a dependency annotation is a bad
idea. It is because it hinders component testing capability, i.e.
there's no simple way to test out the component outside the container
(tm), not too mention that constructing an instance is much harder
with no DI env. Rather than just setting up the component via setters
(when public methods are the only ones dependency annotated) with no
complicated test environment, one would have to use DI framework to
inject the resources and only when it finishes fine start the tests.
To sum up, using non-public methods for setter injection is highly
discouraged and deemed a bad practice. And no field injection should
be used, either.
I feel, it would more depend on the component developer. For example,
she/he may choose to have default (package private) access modifier if
test classes are going to reside in the same package.
Am I missing something?
Jacek