Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JHipster - Hidden @Autowired / @Inject

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?

like image 591
Eel0ne Avatar asked Aug 04 '17 15:08

Eel0ne


1 Answers

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

like image 92
Plog Avatar answered Oct 09 '22 02:10

Plog