News for hackers. Stuff that matters.
Re:A bad citizen in Javaland (2006) by lmm on Thursday December 03, 2015 @11:42AM (#10670555)
Relying on dependency injection is even worse than setters. Will DI behave the same in tests as in real code? Are you sure?

The right thing is to use an actual constructor that takes constructor parameters. Guice and Spring can both handle this correctly (you just put the @Autowired on the constructor itself). The trouble is that in Java this requires ~twice as many lines as putting a magic annotation on your fields.

Re:A bad citizen in Javaland (2006) by pbh101 on Thursday December 03, 2015 @02:31PM (#10671920)
Especially in the multi-core era, this becomes more and more important. Thanks to the Java Memory Model specification, if you are using constructors (and yes, pretty much every DI framework by now has this feature), with just a bit of care[1] you can ensure that your object is never visible to any threads without it having been completely constructed.

[1] Make sure to not leak references to your object in your constructor. Most of the time that you would want to do this is starting a service or registering a handler, etc. The solution is to use a static creation method which first calls the constructor, and then registers/subscribes it, etc.