Update on @Autowired considered harmful

After writing my post about the pox of @Autowired, I’ve been doing a lot of things with Spring and have softened my position a little bit.  Spring is a very powerful tool and, unfortunately, with anything that is powerful you need guidelines and constraints to keep from doing something stupid that will cause a lot of pain down the road.  Perhaps my hardline on @Autowired to not be used anywhere was a little short-sighted, however, I’m still weary of it in many cases (due to the experiences I’ve had using it).  Anyway, the new stance I have is that @Autowired should be used purely on constructors and kept away from injecting dependencies any other way.  As a wonderful side effect, this is allows you to hold true with Josh Bloch’s Effective Java rule to prefer immutability over mutability and fail fast.

Leave a Reply

Your email address will not be published. Required fields are marked *