简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM