2
2
.
.
9
9
@
@
A
A
u
u
t
t
o
o
w
w
i
i
r
r
e
e
d
d
I
I
n
n
f
f
o
o
@Autowired Annotation tells Spring Boot to create/inject Instance of Class into Variable by specifying either
Class @Autowired MyService myService;
Interface @Autowired MyServiceInterface myService;
In both cases Class must be Spring Component/Bean in order for Spring Boot to eb able to find it.
That means that Class must be Annotated with either: @Controller, @Component, @Service.
@Autowired can inject Instance of a Class into
Property @Autowired MyService myService;
Setter Param @Autowired public void setMyService(MyService myService) { this.myService = myService; }
Constructor Param @Autowired public MyController(MyService myService) { this.myService = myService; }
If you specify Interface (and there is more than one Class that implements that Interface) you need to tell Spring which
Class to instantiate by using any of the following Annotations (or their combination)
@Primary - only one Class can be @Primary per @Profile
@Qualifier("impl2") - selects named Component @Service("impl2")
@Profile("Profile1") - specify active Profile in application.properties with spring.profiles.active = Profile1
U
U
s
s
a
a
g
g
e
e
For example @Autowired can be used
inside Controller to instantiate Services
inside Service to instantiate Entities and Repositories