Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Constructor vs field injection

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello @sharma and @sari, I am starting a greenfield j2ee app and I want to know if I should exclusively use constructor injection. The last app I wrote used field injection. It was very easy. I've read articles, however, touting the visibility of the dependencies when using constructor injection. I didn't have problem with dependencies in my application because it was pretty small as j2ee apps go. I was able to get my head around the system.. This next app, though, will be quite large.

What's your opinion on the matter? Does constructor injection really make the system more comprehensible? Does it offer a technical benefit such as establishing mocks for testing?

The best thing about spring and spring boot is it's simplicity. It doesn't burden us with cognitive noise. I just want to make sure if I adopt constructor injection that I don't loose these kind of benefits.

Thanks
Gary
 
Sheriff
Posts: 4645
300
IntelliJ IDE Clojure Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you mean to say JEE rather than J2EE? The last J2EE release was well over a decade ago.

My personal preference is Constructor injection because it allows me to write immutable classes which I believe greatly increases the ability for a person to reason with a program. A common argument against constructor injection is that it's easy to get the arguments in the wrong order and introduce errors, or worse runtime errors if you've misplaced arguments of the same type. This becomes more of a problem if the class has many dependencies, and thus many constructor arguments. I often improve this situation by including a builder with the class to aid with object construction.

You mention technical benefit for testing. I don't think there's any particular gained benefit here for using one injection pattern over another, it's just a different way of interacting with the object.

I've had this discussion numerous times over the years and the best conclusion I've come to is that it's personal preference.
 
gary fong
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, yes, JEE. That's what I get for posting at 3am. I agree about your statement about the benefits of immutable classes. Can you briefly explain how constructor injection makes it possible to write immutable classes?

- Gary
 
Tim Cooke
Sheriff
Posts: 4645
300
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An immutable object is an object who's state cannot be changed after it has been created. Take the following example:

If I create an instance of a Person
is person mutable? Can I change its state?
 
gary fong
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand immutability. I was just curious how spring's autowired constructor injection makes the class more immutable. Again, I've only used field injection and I don't necessarily have to have setters for spring to inject what it wants to into the class. Again, I've heard constructor injection has many benefits so I thought I'd ask the book authors. I'll give it a try myself. It's probably as awesome as everyone has been saying.
 
Saloon Keeper
Posts: 10404
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Field injection makes a class mutable by definition. If the framework can inject a value into a field after the object has been constructed, it's not immutable. This may become more clear in multi-threaded environments: All threads are guaranteed to see the same value of a final field. If a field is not final, then different threads may see the uninitialized value of the field, even after the constructor has finished. Immutable classes should have final fields, and @Inject requires that fields are not final.
 
Tim Cooke
Sheriff
Posts: 4645
300
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Immutable classes should have final fields, and @Inject requires that fields are not final.


Only if the @Inject annotation is on the fields, otherwise if the @Inject annotation is on the Constructor then the class fields can be final.
 
Stephan van Hulst
Saloon Keeper
Posts: 10404
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that's why constructor injection helps with immutability
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!