I've been looking into autogenerated JHipster monolith application and something that enchain my attention was the fact of missing annotation @Autowired/@Inject above
private static final UserRepository userRepository;
How is it possible that this works fine, but when I tried making something similar it didn't?
jHipster makes use of constructor injection. If you look in the UserResource class you'll see the constructor takes the userRepository as one of its arguments:
public UserResource(UserRepository userRepository) {
this.userRepository = userRepository;
}
You used to have to mark the UserRepository as @Autowired in order to use constructor injection:
public UserResource(@Autowired UserRepository userRepository) {
this.userRepository = userRepository;
}
But since Spring 4.3 you no longer need the annotation and if any arguments of the constructor are Spring beans they will automatically be autowired by Spring.
See: https://spring.io/blog/2016/03/04/core-container-refinements-in-spring-framework-4-3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With