Field Injection (ARCH4200)
هذا المحتوى غير متوفر بلغتك بعد.
Key java.quality.field-injection · Severity warning · Category quality
Annotation-based field injection hides dependencies, prevents final, and makes passing dependencies in unit tests harder. Use constructor injection.
Incorrect
Section titled “Incorrect”@Serviceclass OrderService { @Autowired private PaymentGateway gateway;}Correct
Section titled “Correct”@Serviceclass OrderService { private final PaymentGateway gateway; OrderService(PaymentGateway gateway) { this.gateway = gateway; }}Remove the field annotation and inject the dependency as a final constructor parameter.