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