I'm one who has mostly used field injection but I see merit in doing constructor injection. Would it be considered desirable to update jhipster's backend to use constructor injection where possible? I know there are trade-offs both ways but my research seems to make me lean in favour of constructor for mandatory collaborators. @gmarziou, I know you think this is not worthwhile (http://stackoverflow.com/questions/38771746/why-does-jhipster-use-field-injection-and-not-constructor-injection#comment64918144_38771746). Is there a specific reason that we're sticking with field injection or is it the effort to required migrate to constructor injection? This could also be a "later" thing that doesn't need to be happening while ng2 is in progress.
See http://olivergierke.de/2013/11/why-field-injection-is-evil/ and https://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/
for example, instead of
public class BankAccountResource {
@Inject
private BankAccountRepository bankAccountRepository;
}
do (note: @Inject on Constructor is implied with Spring 4.3. See https://spring.io/blog/2016/04/06/spring-framework-4-3-goes-rc1)
public class BankAccountResource {
private BankAccountRepository bankAccountRepository;
public BankAccountResource(BankAccountRepository bankAccountRepository) {
this.bankAccountRepository = bankAccountRepository;
{
}
That's quite surprising from the Spring team, as I thought there was a consensus on field injection - then Oliver joined SpringSource after I quitted, so maybe things have changed since that time, with all those new people.
Anyway, I'm a big fan of field injection:
Anyway we're not going to move this, that would be a lot of work.
Fair points. Hadn't seen any jhipster issues or documentation concerning it
so thought I'd at least bring it up to inquire. Thanks for providing your
thoughts.
On Thu, 11 Aug 2016 5:45 pm Julien Dubois, [email protected] wrote:
Closed #3968 https://github.com/jhipster/generator-jhipster/issues/3968.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/jhipster/generator-jhipster/issues/3968#event-753309472,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABHncsq03_gO6us7oLWfIBXnf_hlki-Oks5qe5efgaJpZM4Jih_G
.
I prefer to use constructor injection over field injection. With constructor injection you know when the objects are injected and it is much more readable than field injection in my opinion. Also it allows immutable and therefore thread safe objects that don't have partially constructed state. Even though field injection is lesser code, constructor is where all the object initiation that is required for the class should ideally take place.
Did you look at the generated code ? We have switched to constructor injection a long time ago...
Most helpful comment
Did you look at the generated code ? We have switched to constructor injection a long time ago...