Skip to main content
3 of 7
Added link to the debate on JMockit google groups site
Eric B.
  • 1.2k
  • 1
  • 9
  • 14

Dependency Injection: Field Injection vs Constructor Injection?

I know this is a hot debate and the opinions tend to change over time as to the best approach practice.

I used to use exclusively field injection for my classes, until I started reading up on different blogs (exs: petrikainulainen and schauderhaft and fowler) about the benefits of constructor injection. I have since switched my methodologies to use constructor injection for required dependencies and setter injection for optional dependencies.

However, I have recently gotten into a debate with the author of JMockit - a mocking framework - in which he sees constructor & setter injection as bad practice and indicates that the JEE community agrees with him.

In today's world, is there a preferred way of doing injection? Is Field injection preferred?

Having started using constructor injection, I find it a lot cleaner to use now.

Eric B.
  • 1.2k
  • 1
  • 9
  • 14