+ 4

SpringBoot annotation based configuration

In Spring core when you want to inject a dependency, we generally use @Component annotation with the class that is suppose to inject an object. And we use @Autowired with the declaration of that reference in the service class. But I am learning SpringBoot recently and there I could see @Autowired annotation but the class that should inject is annotated with @Service instead. Is there a contract between @Service and @Autowired just like it is with @Component and @Autowired?

8th Nov 2020, 10:41 AM
Avinesh
Avinesh - avatar
3 Antworten
+ 7
Avinesh When we use @Service, @Repository and @Component in our class, These sterotype annotation helps to create bean (object) in a Application Context because of IOC(Inversion of control) at the time of application start. So After sucessfully creating beans in Applications Context now you have to inject these dependency in our class. So @Autowired inject the bean (object) in our class. Because It's a object or bean from application Context and inject it. We can @Autowired external beans by using:- 1. Constructor injection. 2. Field Injection. 3. Setter Injection. We can say that there is contract between service, component, repository with autowired.
8th Nov 2020, 11:32 AM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 5
P∆WAN M∆URY∆ Thanks a lot for the explanation. I have played around with spring for a couple of months and was used to @Component but @Service was somewhat new. So it does have contract.
8th Nov 2020, 11:38 AM
Avinesh
Avinesh - avatar
+ 5
Avinesh Anytime :)
8th Nov 2020, 12:04 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar