Zum Inhalt springen

Service Locator Injection (ARCH4198)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key java.quality.service-locator · Severity warning · Category quality

Injecting the container/provider and resolving services from it at runtime hides dependencies and hurts testability. Take dependencies via constructor injection instead.

@Service
class OrderService {
private final ApplicationContext ctx;
OrderService(ApplicationContext ctx) { this.ctx = ctx; }
void place() {
ctx.getBean(PaymentGateway.class).charge();
}
}
@Service
class OrderService {
private final PaymentGateway gateway;
OrderService(PaymentGateway gateway) { this.gateway = gateway; }
void place() { gateway.charge(); }
}

Inject the services you need directly as constructor parameters rather than the container.