java – 没有autowire注释的Spring注入

前端之家收集整理的这篇文章主要介绍了java – 没有autowire注释的Spring注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我找到了一些答案:https://stackoverflow.com/a/21218921/2754014关于依赖注入.没有像@ Autowired,@ Inject或@Resource这样的注释.让我们假设这个示例TwoInjectionStyles bean没有任何XML配置(简单< context:component-scan base-package =“com.example”/>除外).

在没有指定注释的情况下注入是否正确?

@H_502_7@最佳答案
从Spring 4.3开始,构造函数注入不需要注释.

  1. public class MovieRecommender {
  2. private CustomerPreferenceDao customerPreferenceDao;
  3. private MovieCatalog movieCatalog;
  4. //@Autowired - no longer necessary
  5. public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
  6. this.customerPreferenceDao = customerPreferenceDao;
  7. }
  8. @Autowired
  9. public setMovieCatalog(MovieCatalog movieCatalog) {
  10. this.movieCatalog = movieCatalog;
  11. }
  12. }

但你仍然需要@Autowired进行二传手注射.我刚才用Spring Boot 1.5.7(使用Spring 4.3.11)检查过,当我删除@Autowired时,没有注入bean.

猜你在找的Spring相关文章