Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,080 --> 00:00:04,080
Instructor: This video will cover the primary annotation.
2
00:00:07,680 --> 00:00:11,670
Now resolving the issue with multiple coach implementations,
3
00:00:11,670 --> 00:00:13,950
we saw the example in the previous set of videos,
4
00:00:13,950 --> 00:00:16,620
of, by making use of the qualifier annotation.
5
00:00:16,620 --> 00:00:19,860
We were super specific, by mentioning a coach by name.
6
00:00:19,860 --> 00:00:22,310
However, there's an alternate solution available.
7
00:00:26,370 --> 00:00:27,960
Instead of specifying a coach by name,
8
00:00:27,960 --> 00:00:30,030
using a qualifier annotation,
9
00:00:30,030 --> 00:00:31,290
I could say, you know what?
10
00:00:31,290 --> 00:00:32,700
I simply need a coach.
11
00:00:32,700 --> 00:00:34,500
I don't care which coach.
12
00:00:34,500 --> 00:00:37,590
And I could say, Hey, if there are multiple coaches
13
00:00:37,590 --> 00:00:40,770
out there, then you coaches figure it out.
14
00:00:40,770 --> 00:00:42,780
You all tell me who's the primary coach.
15
00:00:42,780 --> 00:00:44,220
I really don't care.
16
00:00:44,220 --> 00:00:46,590
I simply need a coach to help me out, here.
17
00:00:46,590 --> 00:00:47,423
That's it.
18
00:00:50,700 --> 00:00:53,160
So as before, we had our multiple coach implementations,
19
00:00:53,160 --> 00:00:54,510
our track coach, baseball coach,
20
00:00:54,510 --> 00:00:56,160
tennis coach, and cricket coach.
21
00:00:56,160 --> 00:00:57,570
And then the one thing to notice here is,
22
00:00:57,570 --> 00:01:00,720
that now we make use of this new annotation primary.
23
00:01:00,720 --> 00:01:02,700
So this annotation basically says,
24
00:01:02,700 --> 00:01:05,459
out of the multiple coach implementations, this is
25
00:01:05,459 --> 00:01:08,853
going to be the primary implementation that you should use.
26
00:01:12,390 --> 00:01:14,490
Now we can resolve this using the primary.
27
00:01:14,490 --> 00:01:16,920
So here's our code for our demo controller.
28
00:01:16,920 --> 00:01:19,230
And now one thing to notice here, with our demo controller,
29
00:01:19,230 --> 00:01:23,430
and our constructor injection, is that there's no need
30
00:01:23,430 --> 00:01:26,340
to use the qualifier annotation.
31
00:01:26,340 --> 00:01:29,580
Because now, if there's multiple coaches,
32
00:01:29,580 --> 00:01:33,570
we know the primary coach, based on that primary annotation,
33
00:01:33,570 --> 00:01:35,400
that's on the track coach.
34
00:01:35,400 --> 00:01:37,493
All right, so notice the difference, here.
35
00:01:41,070 --> 00:01:42,540
There's one caveat, here.
36
00:01:42,540 --> 00:01:45,210
When you are using the primary annotation, and you probably
37
00:01:45,210 --> 00:01:47,070
was wondering about this, and thought about this.
38
00:01:47,070 --> 00:01:51,090
When using the primary annotation can have only one
39
00:01:51,090 --> 00:01:53,280
for multiple implementations.
40
00:01:53,280 --> 00:01:57,210
If you mark multiple classes with primary, then hmm,
41
00:01:57,210 --> 00:01:58,473
we have a little problem.
42
00:01:59,970 --> 00:02:02,280
And when you run your application, if you try to
43
00:02:02,280 --> 00:02:04,380
mark multiple classes with primary,
44
00:02:04,380 --> 00:02:06,159
then, in the error messages it'll say, Hey,
45
00:02:06,159 --> 00:02:09,150
more than one primary bean found.
46
00:02:09,150 --> 00:02:11,520
So spring will say, I don't know what
47
00:02:11,520 --> 00:02:12,540
you want me to do, here.
48
00:02:12,540 --> 00:02:13,770
There's more than one primary.
49
00:02:13,770 --> 00:02:16,290
I'm not sure which one to use, so I'm not going to start.
50
00:02:16,290 --> 00:02:17,696
All right.
51
00:02:17,696 --> 00:02:19,078
So just have to be aware of that.
52
00:02:19,078 --> 00:02:20,453
You can only mark one class with primary.
53
00:02:23,850 --> 00:02:26,250
Now, you may also wonder, well, what about
54
00:02:26,250 --> 00:02:30,810
mixing primary and qualifier at the same time?
55
00:02:30,810 --> 00:02:33,630
Can I use both of those in the same class?
56
00:02:33,630 --> 00:02:36,843
The answer is yes, but you're asking for trouble.
57
00:02:38,460 --> 00:02:40,530
Not really, but you have to be aware of this is,
58
00:02:40,530 --> 00:02:44,463
that qualifier has the higher priority.
59
00:02:48,120 --> 00:02:53,120
So even if you mention a given class as the primary class,
60
00:02:54,180 --> 00:02:57,630
that can be overridden, or have higher priority,
61
00:02:57,630 --> 00:03:00,210
by making use of the qualifier annotation.
62
00:03:00,210 --> 00:03:03,360
So in this example here, qualifier has a higher priority.
63
00:03:03,360 --> 00:03:06,120
So even though there's a primary coach, track coach,
64
00:03:06,120 --> 00:03:09,120
this example will actually make use of cricket coach.
65
00:03:09,120 --> 00:03:11,123
All right, so just something to be aware of.
66
00:03:14,670 --> 00:03:16,890
And so you may wonder, well, which one should I use?
67
00:03:16,890 --> 00:03:19,620
Should I use primary, or should I use qualifier?
68
00:03:19,620 --> 00:03:22,500
Well, primary leads it up to the implementation classes.
69
00:03:22,500 --> 00:03:25,290
You could have issues of multiple primary classes,
70
00:03:25,290 --> 00:03:26,940
leading to an error.
71
00:03:26,940 --> 00:03:28,980
Qualifier allows you to be very specific
72
00:03:28,980 --> 00:03:30,390
on which bean you want.
73
00:03:30,390 --> 00:03:33,330
And so in general, I recommend using qualifier,
74
00:03:33,330 --> 00:03:35,970
and the reasoning is, is that it's more specific,
75
00:03:35,970 --> 00:03:37,860
and it also has a higher priority,
76
00:03:37,860 --> 00:03:40,203
compared to primary annotation.
77
00:03:42,360 --> 00:03:43,620
This all looks pretty good.
78
00:03:43,620 --> 00:03:45,390
Let's go ahead and move into the video, and let's go ahead
79
00:03:45,390 --> 00:03:48,153
and write some code using the primary annotation.
6374
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.