Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring injects dependencies in constructor without @Autowired annotation

I'm 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 on AppRunner.java class, I have 2 questions:

  1. When server is starting, if I put a breakpoint in this class's constructor, seems like in the constructor, the GitHubLookupService is provided by spring, using the @Service bean that was configured. BUT, there was no @Autowired annotation on the constructor, so how in the world this constructor get called with the right dependency? It was supposed to be null.

Is it an automatic assumption of Spring Boot?
Does Spring see "private field + constructor argument, and it assumes it should look for an appropriate bean?
Is it Spring Framework or Spring boot?
Am I missing something?

  1. As I remember, it was mendatory to provide default constructor to beans / service etc. How come this class (AppRunner) doesn't have a default constructor? How does Spring knows that it should run the constructor with the argument? Is it because it is the only constructor?
like image 790
winter Avatar asked Dec 12 '16 00:12

winter


People also ask

Is @autowired required for constructor injection?

Here the quote from the official documentation : As of Spring Framework 4.3, an @Autowired annotation on such a constructor is no longer necessary if the target bean only defines one constructor to begin with.

Which annotations can be used for injecting dependencies in Spring?

Spring Dependency Injection Configuration with Annotations@Configuration annotation is used to let Spring know that it's a Configuration class. @ComponentScan annotation is used with @Configuration annotation to specify the packages to look for Component classes.

What can I use instead of Autowired?

You can indicate a @Primary candidate for @Autowired. This sets a default class to be wired. Some other alternatives are to use @Resource, @Qualifier or @Inject.

What is the difference between Autowired and constructor injection?

These are mentioned on the above links. By constructor means to not annotate the class properties but the constructor arguments. Auto detect is like scanning where Spring will work out bean candidates. So, autowiring by constructor is a way of implementing constructor based injection.


1 Answers

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

Regarding the default constructor: You either need the default constructor, a constructor with the @Autowired annotation when you have multiple constructors, or only one constructor in your class with or without the @Autowired annotation.

Read the @Autowired chapter from the official Spring documentation for more information.

like image 193
dunni Avatar answered Sep 29 '22 03:09

dunni