Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowire without annotation @Autowired

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?

like image 702
strash Avatar asked Apr 12 '17 07:04

strash


1 Answers

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

like image 168
Darren Forsythe Avatar answered Sep 30 '22 13:09

Darren Forsythe