[英]Autowire without annotation @Autowired
我正在查看工作区中的一些旧示例。 我看不到自动装配是如何完成的,因为没有@Autowired 。 Spring Boot + facebook 默认配置。
@Controller
@RequestMapping("/")
public class HelloController {
private Facebook facebook;
private ConnectionRepository connectionRepository;
public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
this.facebook = facebook;
this.connectionRepository = connectionRepository;
}
@GetMapping
public String helloFacebook(Model model) {
System.out.println("we are here!!!");
if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
return "redirect:/connect/facebook";
}
PagedList<Post> feed = facebook.feedOperations().getFeed();
model.addAttribute("feed", feed);
return "hello";
}
}
它完美无缺,但是这些 bean 如何在没有 @Autowired 的情况下自动装配? 它们是作为字段自动装配还是在构造函数中自动装配?
使用 spring boot 1.4+ 构造函数会自动自动装配
在这个控制器中,你没有给出任何注释@Autowired,可能你在控制器中有接口,所以你不需要给出注释,如果你没有给出@,还可以进一步检查服务层@Service 注释是否存在。您也不能使用 @Autowired 的服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.