Spring introduces dependencies in constructor without @ Automatic annotation

I am experimenting with examples from this official Spring tutorials , and there is a dependency on this code:
https://github.com/spring-guides/gs-async-method/tree/master/complete

If you look at the code of the AppRunner.java class, I have 2 questions:

1) When the server starts, if I set a breakpoint in this class constructor, it seems like the constructor, GitHubLookupService is provided by spring using the @Service bean that was configured. BUT, there was no @Autowired annotation in the @Autowired , since in the world this constructor is called with the correct dependency? It should have been null .

Is this an automatic Spring Boot assumption?
Does Spring represent the private private + constructor argument, and does it assume that it should look for a suitable bean?
Is it Spring Framework or Spring boot ?
Did I miss something?

2) As far as I remember, it was advisable to provide a default constructor for beans / service, etc. How does this class (AppRunner) not have a default constructor? How does Spring know that it should run the constructor with an argument? Is it because it's the only constructor?

+8
spring spring-boot dependency-injection autowired
source share
1 answer

Starting with Spring 4.3, if a class that is configured as a Spring bean has only one constructor, the Autowired annotation can be omitted, and Spring will use this constructor and introduce all the necessary dependencies.

Regarding the default constructor: you need either a default constructor, or a constructor with an Autowired annotation when you have several constructors, or just one constructor in your class with an Autowired annotation.

Read @Autowired from Spring's official documentation for more details.

+29
source share

All Articles