DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Stuck-in-The-Matrix arguing between constructor and setter injection...

Stuck-in-The-Matrix arguing between constructor and setter injection...

Mick Semb Wever user avatar by
Mick Semb Wever
·
May. 23, 11 · Java Zone · Interview
Like (0)
Save
Tweet
1.57K Views

Join the DZone community and get the full member experience.

Join For Free

This article takes an indepth look at when to and when not to use constructor injection by broadening the context that usually limits the debate between constructor vs setter injection.

 

 From http://tech.finn.no/Dependency-Injection-with-constructors/

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...

Is there a bigger picture? Are we just stuck in a matrix not seeing the truth...
What about API design?
What about the differences between IoC and DI?
What about the different types of application?
...
Question everything…by remaining focused on what is required from the code at hand we can be pragmatic in a world full of rules and recommendations. By knowing: when what can, and for how long, be dropped; we can incrementally evolve complex code towards a modular design in a sensible, sustainable, and practical way.

Dependency injection

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022
  • The End of the Beginning for Apache Cassandra
  • Debugging the Java Message Service (JMS) API Using Lightrun
  • JUnit 5 Tutorial: Nice and Easy [Video]

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo