+ 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?
3 odpowiedzi
+ 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.
+ 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.
+ 5
Avinesh Anytime :)