0
0
Spring Bootframework~10 mins

Field injection and why to avoid it in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Field injection and why to avoid it
Start: Spring creates bean
Inject dependencies via fields
Bean ready to use
Problems arise: hard to test, hidden dependencies
Better: use constructor injection
Clear dependencies, easier testing, immutable fields
Spring creates a bean and injects dependencies directly into fields, which works but causes hidden problems. Using constructor injection instead makes dependencies clear and testing easier.
Execution Sample
Spring Boot
public class MyService {
  @Autowired
  private UserRepository userRepository;

  public void doWork() {
    userRepository.save(new User());
  }
}
This code shows field injection where Spring sets the userRepository field directly before the bean is used.
Execution Table
StepActionField userRepositoryResult
1Spring creates MyService instancenullInstance created, userRepository not set yet
2Spring injects UserRepository into userRepository fieldUserRepository instanceDependency injected silently
3doWork() calledUserRepository instanceuserRepository.save() called successfully
4Testing scenarionull or mockHard to inject mock, field is private and hidden
5ConclusionUserRepository instanceField injection hides dependencies, hard to test
💡 Field injection completes but causes hidden dependencies and testing difficulties
Variable Tracker
VariableStartAfter InjectionAfter Method CallFinal
userRepositorynullUserRepository instanceUserRepository instanceUserRepository instance
Key Moments - 3 Insights
Why is it hard to test a class using field injection?
Because the dependency is injected directly into a private field (see execution_table step 4), it is not easy to replace it with a mock or stub during tests.
What does Spring do when it injects dependencies via fields?
Spring sets the private field directly after creating the instance (execution_table step 2), which happens silently and hides the dependency from the constructor.
Why is constructor injection preferred over field injection?
Constructor injection makes dependencies explicit and immutable, improving clarity and testability (refer to concept_flow and key_moments).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of userRepository after Spring injects it?
AUserRepository instance
BMock object
Cnull
DUndefined
💡 Hint
Check execution_table row 2 under 'Field userRepository'
At which step does the difficulty in testing appear according to the execution_table?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look for the step mentioning testing challenges in execution_table
If we switch to constructor injection, how would the variable_tracker change?
AuserRepository would be null throughout
BuserRepository would be set at object creation
CuserRepository would be set after method call
DuserRepository would be set to null after injection
💡 Hint
Constructor injection sets dependencies when the object is created, not later (see concept_flow)
Concept Snapshot
Field injection uses @Autowired on private fields.
Spring sets these fields after creating the object.
This hides dependencies and makes testing hard.
Constructor injection passes dependencies via constructor.
It makes dependencies clear and easier to test.
Avoid field injection for better code quality.
Full Transcript
Field injection in Spring Boot means putting @Autowired on private fields so Spring sets them after creating the object. This works but hides what the class needs, making it hard to test because you cannot easily replace those fields with mocks. The execution table shows Spring creating the object, then injecting the dependency silently into the field. Testing is difficult because the field is private and not set via constructor. Constructor injection is better because it makes dependencies clear and immutable, improving readability and testability. Avoid field injection to write cleaner, easier-to-test code.