Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,098 --> 00:00:01,931
Instructor: In this video,
2
00:00:01,931 --> 00:00:02,820
we'll cover field injection
3
00:00:02,820 --> 00:00:04,743
with Annotations and Autowiring.
4
00:00:07,560 --> 00:00:08,760
Now, as I discussed earlier,
5
00:00:08,760 --> 00:00:10,620
there are different Spring injection types,
6
00:00:10,620 --> 00:00:12,210
and so there are the types that are recommended
7
00:00:12,210 --> 00:00:14,100
by the spring.io development team.
8
00:00:14,100 --> 00:00:16,890
That's constructor injection for required dependencies,
9
00:00:16,890 --> 00:00:19,860
setter injection for optional dependencies.
10
00:00:19,860 --> 00:00:22,920
Now, here's an injection type that's not recommended
11
00:00:22,920 --> 00:00:24,027
by the spring.io development team
12
00:00:24,027 --> 00:00:26,133
and that's field injection.
13
00:00:30,000 --> 00:00:32,130
And field injection is no longer cool.
14
00:00:32,130 --> 00:00:33,210
So in the early days,
15
00:00:33,210 --> 00:00:36,450
field injection was very popular on Spring projects
16
00:00:36,450 --> 00:00:38,970
but in recent years it has fallen out of favor.
17
00:00:38,970 --> 00:00:39,900
And why is that?
18
00:00:39,900 --> 00:00:43,920
Because in general, it makes the code harder to unit test.
19
00:00:43,920 --> 00:00:45,900
Now, as a result, the spring.io team
20
00:00:45,900 --> 00:00:48,060
does not recommend field injection,
21
00:00:48,060 --> 00:00:51,360
however, you'll still see it being used on legacy projects,
22
00:00:51,360 --> 00:00:52,770
and also you'll see it being used
23
00:00:52,770 --> 00:00:55,740
in a lot of old blog posts on the internet,
24
00:00:55,740 --> 00:00:58,650
and even in previous versions of this course,
25
00:00:58,650 --> 00:01:00,720
I actually used field injection,
26
00:01:00,720 --> 00:01:02,760
but now with modern times here,
27
00:01:02,760 --> 00:01:04,500
removing the useless field injection,
28
00:01:04,500 --> 00:01:07,890
and instead making use of construction or setter injection,
29
00:01:07,890 --> 00:01:10,020
but I'll still show you a little quick example here
30
00:01:10,020 --> 00:01:12,990
of using field injection just in case you encounter it
31
00:01:12,990 --> 00:01:14,853
on some of your legacy projects.
32
00:01:18,360 --> 00:01:20,970
Field injection is the idea of injecting in dependencies
33
00:01:20,970 --> 00:01:24,060
by setting the values on your class directly,
34
00:01:24,060 --> 00:01:26,430
even on private fields,
35
00:01:26,430 --> 00:01:29,343
and this is accomplished by using Java Reflection.
36
00:01:32,310 --> 00:01:34,110
So in step one, you simply configure
37
00:01:34,110 --> 00:01:37,290
the dependency injection using the Autowired Annotation,
38
00:01:37,290 --> 00:01:40,380
and so here's a code example on our DemoController.
39
00:01:40,380 --> 00:01:43,530
Notice here we have this field private Coach, myCoach,
40
00:01:43,530 --> 00:01:45,783
and we give the Autowired Annotation,
41
00:01:47,250 --> 00:01:48,840
and behind the scenes,
42
00:01:48,840 --> 00:01:52,200
Spring will inject a given Coach implementation,
43
00:01:52,200 --> 00:01:55,320
and it'll do it behind the scenes even on a private field.
44
00:01:55,320 --> 00:01:59,160
It'll automatically or directly set it on this controller,
45
00:01:59,160 --> 00:02:02,250
and notice here, there's no need for constructors,
46
00:02:02,250 --> 00:02:03,900
there's no need for setters,
47
00:02:03,900 --> 00:02:06,093
Spring will set the field directly.
48
00:02:08,310 --> 00:02:09,389
However, like I mentioned,
49
00:02:09,389 --> 00:02:11,220
field injection is not recommended
50
00:02:11,220 --> 00:02:13,110
by the spring.io development team
51
00:02:13,110 --> 00:02:15,843
because it makes the code harder to unit test.
52
00:02:17,580 --> 00:02:18,960
All right, so that's field injection.
53
00:02:18,960 --> 00:02:20,340
So I wanted to show it to you
54
00:02:20,340 --> 00:02:21,750
just in case you encounter it
55
00:02:21,750 --> 00:02:23,763
on some of your legacy projects.
4334
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.