Performance: Field injection and why to avoid it
MEDIUM IMPACT
This affects application startup time and memory usage by impacting dependency injection efficiency and testability.
public class MyService {
private final Dependency dependency;
@Autowired
public MyService(Dependency dependency) {
this.dependency = dependency;
}
public void doWork() {
dependency.action();
}
}public class MyService {
@Autowired
private Dependency dependency;
public void doWork() {
dependency.action();
}
}| Pattern | Reflection Usage | Startup Time Impact | Testability | Verdict |
|---|---|---|---|---|
| Field Injection | High (uses reflection) | Medium (slower startup) | Low (harder to test) | [X] Bad |
| Constructor Injection | Low (no reflection needed) | Low (faster startup) | High (easy to test) | [OK] Good |