Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,217 --> 00:00:02,050
Instructor: In this video,
2
00:00:02,050 --> 00:00:03,663
we're gonna cover setter injection.
3
00:00:06,750 --> 00:00:09,300
Earlier I mentioned the two recommended injection types;
4
00:00:09,300 --> 00:00:11,580
constructor injection and setter injection.
5
00:00:11,580 --> 00:00:14,073
In this video, we'll focus on setter injection.
6
00:00:17,370 --> 00:00:19,470
Setter injection is when we inject dependencies
7
00:00:19,470 --> 00:00:21,873
by calling them setter methods on your class.
8
00:00:26,160 --> 00:00:28,620
Now let's take a look at an Autowiring example.
9
00:00:28,620 --> 00:00:31,230
Here we want to inject a coach implementation,
10
00:00:31,230 --> 00:00:33,300
Spring's gonna scan for components,
11
00:00:33,300 --> 00:00:34,133
and it'll basically say,
12
00:00:34,133 --> 00:00:37,380
"Hey, is there anyone that implements the coach interface?
13
00:00:37,380 --> 00:00:39,240
If so, let's inject them."
14
00:00:39,240 --> 00:00:41,217
For example, the "CricketCoach."
15
00:00:44,070 --> 00:00:45,870
And here's our development process.
16
00:00:45,870 --> 00:00:47,310
So the first thing we'll do is we'll create
17
00:00:47,310 --> 00:00:49,710
the setter methods in our class for injections,
18
00:00:49,710 --> 00:00:51,810
and then we'll configure the dependency injection
19
00:00:51,810 --> 00:00:53,883
using the Autowired Annotation.
20
00:00:57,900 --> 00:00:58,980
All right, let's start with step one
21
00:00:58,980 --> 00:01:02,610
of creating the setter methods in our class for injections.
22
00:01:02,610 --> 00:01:04,980
Here we have our DemoController,
23
00:01:04,980 --> 00:01:08,703
and then we'll have this new setter method here set coach.
24
00:01:12,360 --> 00:01:13,193
And then in step two,
25
00:01:13,193 --> 00:01:15,090
we'll configure the dependency injection
26
00:01:15,090 --> 00:01:17,190
with the Autowired Annotation.
27
00:01:17,190 --> 00:01:18,780
So in our "setCoach" method,
28
00:01:18,780 --> 00:01:21,393
we make use of the Autowired Annotation.
29
00:01:25,230 --> 00:01:27,210
The Spring Framework will perform operations
30
00:01:27,210 --> 00:01:28,713
behind the scenes for you.
31
00:01:32,580 --> 00:01:33,660
Now let's take a look at how
32
00:01:33,660 --> 00:01:35,820
Spring will process your application.
33
00:01:35,820 --> 00:01:37,260
We have our coach interface,
34
00:01:37,260 --> 00:01:41,070
our CricketCoach implementation, and our DemoController,
35
00:01:41,070 --> 00:01:42,630
and we want to inject the dependency
36
00:01:42,630 --> 00:01:44,253
into our DemoController.
37
00:01:46,170 --> 00:01:47,970
Behind the scenes, Spring will create an instance
38
00:01:47,970 --> 00:01:49,380
of the CricketCoach,
39
00:01:49,380 --> 00:01:51,630
it'll create an instance of the DemoController,
40
00:01:51,630 --> 00:01:54,030
and then it'll say demoController.setCoach,
41
00:01:54,030 --> 00:01:56,403
and it'll pass in the coach implementation.
42
00:02:00,150 --> 00:02:02,670
Now we could also inject our dependencies
43
00:02:02,670 --> 00:02:05,340
by calling any method on our class.
44
00:02:05,340 --> 00:02:06,690
We can give it any method name,
45
00:02:06,690 --> 00:02:09,509
simply give the Autowired Annotation.
46
00:02:09,509 --> 00:02:12,120
And here's a coding example of this.
47
00:02:12,120 --> 00:02:14,250
We have our DemoController,
48
00:02:14,250 --> 00:02:16,890
and instead of a traditional setter method,
49
00:02:16,890 --> 00:02:19,140
we could say, "Do some stuff."
50
00:02:19,140 --> 00:02:21,090
We annotate this method as Autowired,
51
00:02:21,090 --> 00:02:23,670
and we can simply give any method name here
52
00:02:23,670 --> 00:02:24,870
for this given method,
53
00:02:24,870 --> 00:02:27,753
and Spring will handle the dependency injection for us.
54
00:02:30,930 --> 00:02:31,763
Okay, great.
55
00:02:31,763 --> 00:02:33,000
So let's kind of step back a bit.
56
00:02:33,000 --> 00:02:34,800
You know, we've saw the different injection types,
57
00:02:34,800 --> 00:02:37,080
constructor injection, setter injection,
58
00:02:37,080 --> 00:02:37,927
and you're probably wondering,
59
00:02:37,927 --> 00:02:40,557
"Well, which injection type should I use?"
60
00:02:42,360 --> 00:02:44,160
Constructor injection, this is the one that you
61
00:02:44,160 --> 00:02:46,560
use when you have required dependencies.
62
00:02:46,560 --> 00:02:47,910
It's generally recommended
63
00:02:47,910 --> 00:02:51,093
by the spring.io development team as the first choice.
64
00:02:53,010 --> 00:02:54,360
And then setter injection,
65
00:02:54,360 --> 00:02:57,060
you should use this when you have optional dependencies.
66
00:02:57,060 --> 00:02:59,340
So if a dependency is not provided,
67
00:02:59,340 --> 00:03:02,280
then your app can provide some reasonable default logic.
68
00:03:02,280 --> 00:03:04,230
So that's the basic guidance
69
00:03:04,230 --> 00:03:05,730
that the Spring Development Team provides
70
00:03:05,730 --> 00:03:08,283
as far as which injection type to use.
71
00:03:09,630 --> 00:03:11,670
Okay, this is all really good stuff here.
72
00:03:11,670 --> 00:03:13,140
Let's go ahead and move into our IDE,
73
00:03:13,140 --> 00:03:14,853
and let's write some code.
5630
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.