[英]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.