简体   繁体   English

JHipster - 隐藏@Autowired / @Inject

[英]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 我一直在寻找自动生成的JHipster monolith应用程序,引起我注意的是缺少注释的事实@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. jHipster使用构造函数注入。 If you look in the UserResource class you'll see the constructor takes the userRepository as one of its arguments: 如果查看UserResource类,您将看到构造函数将userRepository作为其参数之一:

public UserResource(UserRepository userRepository) {
        this.userRepository = userRepository;
}

You used to have to mark the UserRepository as @Autowired in order to use constructor injection: 您曾经必须将UserRepository标记为@Autowired才能使用构造函数注入:

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. 但是从Spring 4.3开始,你不再需要注释,如果构造函数的任何参数都是Spring bean,它们将自动由Spring自动装配。

See: https://spring.io/blog/2016/03/04/core-container-refinements-in-spring-framework-4-3 请参阅: https//spring.io/blog/2016/03/04/core-container-refinements-in-spring-framework-4-3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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