So when the best practices page says 'not testable', is it more
accurate to say 'hard to test without using a Guice module in your
tests' ? I understand that with constructor or method injection, I
don't even need Guice in my tests. I could just create mock POJOs and
call setter methods myself. But with field injection, I'd need to
either use a module with Guice, or else resort to reflection tricks to
set the fields.
What you're saying is accurate. Also note that with field injection you enter a gray zone when it comes to thread safety and safe publication, since you can't make your fields final and Guice doesn't inject the values until after the constructor executes. In a sense, field injection is the "autowiring" of injection: simple to begin with but surprisingly difficult to get right in the long term.
On Tue, Jul 22, 2008 at 6:53 AM, Robbie Vanbrabant <robbie.v...@gmail.com> wrote:What you're saying is accurate. Also note that with field injection you enter a gray zone when it comes to thread safety and safe publication, since you can't make your fields final and Guice doesn't inject the values until after the constructor executes. In a sense, field injection is the "autowiring" of injection: simple to begin with but surprisingly difficult to get right in the long term.
I don't think there are any thread safety concerns here. Injection happens all in one thread. You can make the fields final. Of course, you'll have to initialize them to some default value, but Guice will overwrite that value and you'll get the same semantics that you get if the field's set in the constructor (http://jeremymanson.blogspot.com/2008/07/immutability-in-java-part-3.html).
Thanks for the link, that's something I didn't know. Which somehow confirms my concerns, though.
Wow, is this really true? It goes against everything I thought I'd
known with threading concerns & publishing. I also had no clue that
'setAccessible' let you overwrite final fields -and- did it safely
with threading.
Sam
Wow, is this really true?