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
Instructor: In this video,
we're gonna cover setter injection.
Earlier I mentioned the two recommended injection types;
constructor injection and setter injection.
In this video, we'll focus on setter injection.
Setter injection is when we inject dependencies
by calling them setter methods on your class.
Now let's take a look at an Autowiring example.
Here we want to inject a coach implementation,
Spring's gonna scan for components,
and it'll basically say,
"Hey, is there anyone that implements the coach interface?
If so, let's inject them."
For example, the "CricketCoach."
And here's our development process.
So the first thing we'll do is we'll create
the setter methods in our class for injections,
and then we'll configure the dependency injection
using the Autowired Annotation.
All right, let's start with step one
of creating the setter methods in our class for injections.
Here we have our DemoController,
and then we'll have this new setter method here set coach.
And then in step two,
we'll configure the dependency injection
with the Autowired Annotation.
So in our "setCoach" method,
we make use of the Autowired Annotation.
The Spring Framework will perform operations
behind the scenes for you.
Now let's take a look at how
Spring will process your application.
We have our coach interface,
our CricketCoach implementation, and our DemoController,
and we want to inject the dependency
into our DemoController.
Behind the scenes, Spring will create an instance
of the CricketCoach,
it'll create an instance of the DemoController,
and then it'll say demoController.setCoach,
and it'll pass in the coach implementation.
Now we could also inject our dependencies
by calling any method on our class.
We can give it any method name,
simply give the Autowired Annotation.
And here's a coding example of this.
We have our DemoController,
and instead of a traditional setter method,
we could say, "Do some stuff."
We annotate this method as Autowired,
and we can simply give any method name here
for this given method,
and Spring will handle the dependency injection for us.
Okay, great.
So let's kind of step back a bit.
You know, we've saw the different injection types,
constructor injection, setter injection,
and you're probably wondering,
"Well, which injection type should I use?"
Constructor injection, this is the one that you
use when you have required dependencies.
It's generally recommended
by the spring.io development team as the first choice.
And then setter injection,
you should use this when you have optional dependencies.
So if a dependency is not provided,
then your app can provide some reasonable default logic.
So that's the basic guidance
that the Spring Development Team provides
as far as which injection type to use.
Okay, this is all really good stuff here.
Let's go ahead and move into our IDE,
and let's write some code.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.