Dependency injectioneasy0-2 years
A teammate says "we use Spring, so of course this class does dependency injection" about a class that calls `applicationContext.getBean(PricingClient.class)` inside a method. Are they right?
No — that's service location, not injection, and the difference is exactly the thing dependency injection exists to fix. The class still decides which implementation it gets and still looks it up itself; the only change from new HttpPricingClient(url) is that the lookup goes through a container instead of a constructor. It's now coupled to Spring itself (the class cannot run without an ApplicationContext), which a genuinely injected class is not. Real dependency injection means the class declares what it needs — as a constructor parameter — and something else decides what that is and hands it in, without the class ever calling anything to fetch its own collaborator.