DIGITAL GURU
Java Roadmap Portfolio

Field Injection & Why Avoid It

Understand direct field injection with @Autowired and why professional teams avoid it.

Anuj Kumar Singh Written by Anuj Kumar Singh (Lead Engineer, 13+ yrs exp) 5 min read Verified Spring Boot 3+ Guide
3D Architecture Visualizer

What is Field Injection?

Field Injection means putting the @Autowired annotation directly on top of a private instance variable without constructors or setters.

BadExample.java (Field Injection)
@Service
public class UserService {

    @Autowired // Direct field injection - NOT RECOMMENDED!
    private UserRepository userRepository;

}

Why Do Senior Developers Avoid Field Injection?

  1. Cannot make fields final: The variable can be accidentally mutated or assigned null.
  2. Hides dependencies: As a class grows, having 10 `@Autowired` fields hides code smell (violates Single Responsibility Principle).
  3. Hard to Unit Test: You cannot test `UserService` in a plain JUnit test without using heavy Reflection or booting Spring.