juniorDependency Injection
What is Field Injection?
Updated Apr 28, 2026
Short answer
Field injection occurs when a framework injects dependencies directly into private or public fields, often using attributes or annotations.
Deep explanation
While highly convenient and concise, field injection is widely considered an anti-pattern. It hides the class's dependencies, making it impossible to instantiate the class in unit tests without using the DI container or reflection.
Real-world example
Android development using Dagger/Hilt often used field injection for Activities, as the OS controls the constructor.
Common mistakes
- Relying on field injection in core domain logic, heavily coupling the code to the DI framework.
Follow-up questions
- Why is Constructor Injection preferred over Field Injection?