Dependency Injection vs Hardwiring Resources in Java

Prefer Dependency Injection to Hardwiring Resources Many classes depend on underlying resources: a spell checker needs a dictionary, a service needs a repository, a client needs an HTTP adapter, and so on. A common mistake is to hardwire these dependencies using static fields or singletons. It looks simple, but it makes the code inflexible and hard to test. When a class hides its dependencies inside, it assumes there is “one true resource” forever: one dictionary, one data source, one configuration. In reality, we often need different implementations for different languages, environments, tenants, or tests. Trying to “fix” this with mutable fields and setters introduces complexity and is dangerous in concurrent systems. The better approach is dependency injection: the class declares what it needs, and the caller provides the resource. Instead of creating the dictionary inside the spell checker, we pass it through the constructor and store it in a final field. For example: public SpellChecker(Lexicon dictionary) { this.dictionary = Objects.requireNonNull(dictionary); } Now we can plug different dictionaries (English, Spanish, test doubles) without touching the SpellChecker code. This pattern scales to multiple dependencies and preserves immutability, allowing safe sharing between clients. It also extends to static factories and builders. A powerful variant is injecting factories instead of concrete instances, for example using Supplier<? extends Tile> to decide how each tile is created on demand. In large systems, thousands of dependencies can make manual wiring noisy. That is where DI frameworks like Spring, Guice, or Dagger help. But they are just automation on top of the same idea: your classes should not create their own resources; they should receive them. In summary: avoid static utilities and singletons for behavior that depends on external resources. Inject those resources (or factories) via constructors, factories, or builders. You gain flexibility, reusability, and much better testability. #Java #SpringBoot #Microservices #JavaDeveloper #SoftwareDevelopment #SoftwareEngineering #BackendDevelopment #FullStackDevelopment #Programming #Coding #CleanCode #CodeQuality #WebDevelopment #DesignPatterns #SoftwareArchitecture #SystemDesign #ObjectOrientedDesign #GangOfFour #DevOps #AWS #CloudComputing #CICD #TechCommunity #ContinuousLearning #CareerInTech #DeveloperLife #EngineeringCulture

  • graphical user interface, diagram

This is a timeless engineering principle. Hardwiring dependencies feels convenient early on, but it quietly taxes flexibility, testability, and concurrency safety. Dependency Injection makes dependencies explicit, preserves immutability, and keeps classes honest about what they need to function. DI frameworks don’t change the idea, they just automate the wiring. The real win is designing classes that receive resources instead of creating them.

Great point. Constructor injection forces explicit dependencies and makes coupling visible, which is exactly what good design should do.

Like
Reply

Totally agree. From a QA perspective, dependency injection is essential for testability. It allows us to isolate components, use mocks or test doubles easily, and validate behavior without relying on static resources or singletons.

Great points on dependency injection. It really does make a difference in keeping code flexible and maintainable.

Absolutely! DI keeps code flexible, testable, and clean—much better than hardwiring or singletons.

Great analysis Julio César ! Thank you! 🚀💯

Hardwiring is the fastest way to make a codebase impossible to unit test. I've seen too many 'legacy' services that require a full database connection just to run a simple logic test because the repository was hardcoded as a Singleton. Moving to Constructor Injection makes it so much easier to swap in a Mockito mock and keep the test suite fast. Are you using manual wiring for smaller modules, or do you go straight to Spring?

See more comments

To view or add a comment, sign in

Explore content categories