Autowire without annotation @Autowired

点点圈 提交于 2020-01-30 06:06:06

问题


I am looking at some old example I have in the workspace. I can't see how is the autowiring done as there is no @Autowired. Spring boot + facebook default configurations.

@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";
    }
}

It works perfect but how these beans autowire themselves without the @Autowired? Are they autowired as a field or in the constructor?


回答1:


With spring boot 1.4+ constructors are automatically autowired

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html



来源:https://stackoverflow.com/questions/43363122/autowire-without-annotation-autowired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!