# Dependency Injection with constructors?

DevFeed: [Dependency Injection with constructors?](<https://devfeed.tech/articles/dependency-injection-with-constructors-31953.md>)

Original publisher: [Read original article](<https://tech.finn.no2011/05/12/dependency-injection-with-constructors/>)

Author: mick

Published: 2011-05-12T23:01:00Z

Content type: opinion

Language: en

Sources: [Finn.no](<https://devfeed.tech/sources/finn-no.md>)

Topics: [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [Dependency Inversion](<https://devfeed.tech/topics/dependency-inversion.md>), [Framework](<https://devfeed.tech/topics/framework.md>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>)

Tags: [constructor](<https://devfeed.tech/tags/constructor.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [dependency-inversion](<https://devfeed.tech/tags/dependency-inversion.md>), [framework](<https://devfeed.tech/tags/framework.md>), [immutability](<https://devfeed.tech/tags/immutability.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [polymorphism](<https://devfeed.tech/tags/polymorphism.md>)

## AI overview

The article examines constructor injection compared with setter, field, and interface injection. It argues that constructor injection makes dependencies explicit, supports safer construction and immutability, and exposes application complexity, while noting cases where setter injection remains useful.

## Source excerpt

The debate whether to use constructors, setters, fields, or interfaces for dependency injection is often heated and opinionated. Should you have a preference? The argument for Constructor Injection We had a consultant working with us reminding us to take a preference towards Constructor injection. Indeed we had a large code base using predominantly setter injection because in the past that is what the Spring community recommended. The arguments for constructor injection goes like: Dependencies are declared public, providing clarity in the wiring of Dependency Inversion, Safe construction, what must be initialised must be called, Immutability, fields can be declared final, and Clear indication of complexity through numbers of constructor parameters. And that Setter injection can be used when needed for cyclic dependencies, optional and re-assignable dependencies, to support multiple/complicated variations of construction, or to free up the constructor for polymorphism purposes. Being a big fan of Inversion of Control but not overly of Dependency Injection frameworks something smelt wrong to me. Yet solely within the debate of constructor versus setter injection i don't disagree that constructor injection has the advantage. Having been using Spring's dependency injection through annotation a little recently and building a favouritism towards field injection I was happy to get the chance to ponder it over, to learn and to be taught new things. What was it i was missing? Is there a bigger picture? API vs Implementation If there is a bigger picture it has to be around the Dependency Inversion argument since this is known to be potentially complex. The point here of using constructor injection is that 1) through a public declaration and injection of dependencies we build an explicit graph showing the dependency inversion throughout the application, and 2) even if the application is wired magically by a framework such injection must still be done in the same way without th