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 will cover bean scopes.
Scope refers to the lifecycle of a bean.
Such as, how long does the bean live,
how many instances are created, and how is the bean shared?
Now the default scope in spring is singleton.
Okay, so the default scope is singleton. Very important.
What is a singleton?
Well, Spring Container creates only one instance
of the bean by default, and it's cached in memory
and all dependency injections for that bean
will reference the same bean.
All right, so it's just a singleton,
one single item that's created in memory
and shared amongst different other items.
Here's an example of this.
So I have this demo controller.
I have two references here, myCoach, anotherCoach.
I'll inject it using qualifier CricketCoach, theCoach,
another qualifier in CricketCoach, theAnotherCoach,
and they both point to the same instance,
because by default, spring beans are singleton beans.
There's only one instance that's created.
Now, we could also explicitly specify the bean scope.
So in our class cricket coach
we could make use of scope annotation,
and then we give configurable bean factory
dot scope singleton.
There's additional spring bean scopes.
We already saw the first one here for the singleton scope.
There's also prototype scope.
It'll create a new bean incense
for each container request or for each injection point.
There's request scope.
That's scoped to an HTTP web request only used for web apps.
There's also a session scope that's scoped
to an HTTP web session.
Again, only used for web apps.
And then there's a global session that's scoped
to the global HTTP web session in the case of web apps.
Now let's take a look at a prototype scope example.
Now with prototype scope, a new object instance is created
for each injection.
So we have to specify the scope
on the actual class here the bean.
So cricket coach, we have scope
configurable bean factory dot scope prototype,
create a new one for each instance.
Let's look at a prototype scope example,
a new object instance for each injection.
We have our demo controller, we have our two fields here
and then we reference our cricket coach, the coach,
cricket coach, the another coach.
Since this is a prototype scope bean,
you'll get a new object instance for each injection.
So they point to two different areas of memory
or two different beans.
How could we check on the scope?
Well, we could write some code here
as far as a get mapping for slash check
And a return to results here.
So I'll say comparing beans
to my coach equals another coach
and then I put the actual expression here.
So this will check to see if this is the same bean.
This will return true or false depending
on the bean scope.
For singleton scope, they return true,
because remember
singleton is shared and it points to the same bean.
If it's prototype scope, it'll return false
because with prototype you get a new instance
for each one of them and they won't point to the same item.
All right, so this is really good.
Let's go ahead and dive into our IDE
and let's write the code for this and check out scopes.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.