Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
In this video we're going to learn 2
2
about Spring Dependency Injection with Annotations 3
3
and Autowiring. 4
4
So we'll continue with the Coach example 5
5
where our Coach already provides daily workouts. 6
6
Now we'll also provide daily fortunes 7
7
and will make use of the FortuneService 8
8
and this is a dependency. 9
9
We've seen this in previous videos, 10
10
but now we're going to inject this dependency 11
11
using annotations and autowiring. 12
12
Now what exactly is Spring AutoWiring? 13
13
Well, for dependency injection, 14
14
Spring can automatically wire up your objects together, 15
15
so basically what'll happen is that Spring 16
16
will look for a class that matches a given property. 17
17
And it'll actually match by type, 18
18
so the type could be either the class or the interface. 19
19
Once Spring finds a match, 20
20
then it'll automatically inject it. 21
21
Hence it's called autowired. 22
22
So let's take a look at this using an example 23
23
and we'll actually make use 24
24
of that Coach and FortuneService. 25
25
So we need to inject a FortuneService 26
26
into a Coach implementation. 27
27
So how does it work behind the scenes? 28
28
Well, Spring will actually scan all of the components 29
29
and it'll say hey, I need to satisfy a dependency. 30
30
I need to inject a FortuneService, 31
31
so Spring will basically ask, 32
32
say hey, is there anyone out there 33
33
that implements the FortuneService interface? 34
34
If so, Spring will grab that component or bean 35
35
and actually inject it. 36
36
In our examples that we're going to work through, 37
37
we're going to actually have a happy FortuneService 38
38
which is an implementation of the FortuneService interface 39
39
and Spring will find that implementation 40
40
and automatically inject it into our class 41
41
and that's called Autowiring. 42
42
And we'll actually walk through this 43
43
with a full code example so if the theory 44
44
and the concepts are a little funny to you right now, 45
45
don't worry we'll see all the code for it. 46
46
Now with this Autowiring, 47
47
they actually have three different types of injections. 48
48
They have Constructor Injection. 49
49
Setter Injection and Field Injection 50
50
and we'll cover all of these in this video series, 51
51
but we'll simply start off with Constructor Injection. 52
52
Now here's the development process 53
53
for constructor injection. 54
54
And again, I luv doing things step-by-step, 55
55
so the first thing is that we'll define 56
56
the dependency interface and class, 57
57
we'll also create a constructor on our class for injections 58
58
and then the main point here is that 59
59
we'll configure the dependency injection using 60
60
that Autowired Annotation 61
61
and we'll see all of this step-by-step. 62
62
All right, so let's start with step one. 63
63
Define the dependency interface and class. 64
64
So here we'll have this dependency called FortuneService, 65
65
it's simply an interface. 66
66
Has a simple method called getFortune, returns a string. 67
67
And then we'll actually have 68
68
an implementation of this interface 69
69
called HappyFortuneService 70
70
and they simply provide a very basic implementation. 71
71
And we've seen a lot of this before. 72
72
The only thing that's new here with the HappyFortuneService 73
73
is that we have the @Component annotation there, 74
74
so Spring can auto-scan and find this implementation 75
75
for the Spring container. 76
76
All right, so that's step one. 77
77
Now step two is that we need to create 78
78
a constructor in our class for injections. 79
79
So for our TennisCoach we'll simply add a constructor 80
80
and again, the constructor has the same name as the class, 81
81
TennisCoach, and then we pass in our dependency, 82
82
FortuneService 83
83
and we'll make a basic assignment. 84
84
But now the real work happens here in step three 85
85
where we configure the dependency injection 86
86
using that Autowired Annotation. 87
87
So this is where you have the real magic here. 88
88
So note on our constructor for TennisCoach, 89
89
we have Autowired. 90
90
So we're basically saying, 91
91
the parameter being passed in FortuneService, 92
92
we want Spring to automatically wire up this component, 93
93
so instead of doing it long-hand, using XML configs, 94
94
we're making use of this Autowired annotation. 95
95
So again, what happens in the background 96
96
is that Spring will say hey, 97
97
I need to satisfy this dependency. 98
98
So Spring will again, it'll scan all the components there, 99
99
it'll find the component that implements 100
100
this FortuneService interface, 101
101
which in our case is HappyFortuneService. 102
102
And it'll take that bean, actually inject it, 103
103
or Autowire it here into this TennisCoach. 104
104
So that's how they handle the dependency injection 105
105
using that Autowired annotation. 106
106
So that's the whole idea there. 107
107
Now again, stepping back here, 108
108
let's kind of look at the big picture again. 109
109
Again, we're simply going to retrieve a Coach object 110
110
from our Object Factory. 111
111
So now this Object Factory 112
112
all the dependencies of the helper objects 113
113
are being handled or injected by making use 114
114
of annotations and Autowiring. 115
115
So all that work is happening 116
116
behind the scenes by Spring. 117
117
So when we simply retrieve the Coach 118
118
we get it fully assembled. 119
119
So here there's no assembly required, 120
120
we get the Coach object and it's FortuneService 121
121
already wired together, 122
122
thanks to Spring. 123
123
And in the next video, 124
124
we'll actually move into Eclipse 125
125
and we'll enhance our demo 126
126
to make use of annotations and Autowiring. 127
127
So I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.