Ir al contenido

Field Injection (ARCH4200)

Esta página aún no está disponible en tu idioma.

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.

@Service
class OrderService {
@Autowired
private PaymentGateway gateway;
}
@Service
class OrderService {
private final PaymentGateway gateway;
OrderService(PaymentGateway gateway) { this.gateway = gateway; }
}

Remove the field annotation and inject the dependency as a final constructor parameter.