All language subtitles for 019 Lazy Initialization - Overview_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese Download
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,170 --> 00:00:02,003 Instructor: In this video, 2 00:00:02,003 --> 00:00:03,843 we're gonna cover lazy initialization. 3 00:00:07,410 --> 00:00:09,750 Now by default, when your application starts, 4 00:00:09,750 --> 00:00:11,520 all beans are initialized. 5 00:00:11,520 --> 00:00:13,050 So it'll scan for all the components, 6 00:00:13,050 --> 00:00:14,580 and all those components will be initialized. 7 00:00:14,580 --> 00:00:16,559 So spring will create a new instance of each, 8 00:00:16,559 --> 00:00:18,333 and make them available. 9 00:00:21,480 --> 00:00:24,270 Now we can get some insights into this 10 00:00:24,270 --> 00:00:25,830 by setting up some diagnostics, 11 00:00:25,830 --> 00:00:28,830 or adding some println statements to our constructors. 12 00:00:28,830 --> 00:00:29,760 So on our CricketCoach, 13 00:00:29,760 --> 00:00:31,890 we'll add a println statement where we'll simply print 14 00:00:31,890 --> 00:00:35,250 out the name of the class that we're processing on. 15 00:00:35,250 --> 00:00:37,800 We'll do a similar thing here for baseball coach, 16 00:00:37,800 --> 00:00:40,263 track coach, and also tennis coach. 17 00:00:44,490 --> 00:00:46,920 And now, when we start our application, 18 00:00:46,920 --> 00:00:50,070 then in the actual logs we should see In constructor: 19 00:00:50,070 --> 00:00:53,520 BaseballCoach, PicketCoach, and so on. 20 00:00:53,520 --> 00:00:55,680 Again, by the default when your application starts, 21 00:00:55,680 --> 00:00:57,990 all beans are initialized and Spring will create an instance 22 00:00:57,990 --> 00:00:59,823 of each and make them available. 23 00:01:03,720 --> 00:01:06,000 Now we could make use of lazy initialization, 24 00:01:06,000 --> 00:01:08,310 where instead of creating all the beans up front, 25 00:01:08,310 --> 00:01:10,410 we can specify lazy initialization. 26 00:01:10,410 --> 00:01:13,980 So, a bean will only be initialized in the following cases: 27 00:01:13,980 --> 00:01:16,380 either when it's needed for a dependency injection, 28 00:01:16,380 --> 00:01:18,540 or it is explicitly requested. 29 00:01:18,540 --> 00:01:20,610 We simply add the lazy annotation 30 00:01:20,610 --> 00:01:23,793 to a given class and those rules will come into play. 31 00:01:27,000 --> 00:01:30,300 And here's a coding example: So for our track coach, 32 00:01:30,300 --> 00:01:32,044 we'll say that our track coach is lazy, 33 00:01:32,044 --> 00:01:34,110 (chuckling) so we'll make use of the lazy annotation. 34 00:01:34,110 --> 00:01:36,180 This given bean will only be initialized 35 00:01:36,180 --> 00:01:38,220 if it's needed for dependency injection. 36 00:01:38,220 --> 00:01:40,530 If it's not needed, they won't create it. 37 00:01:40,530 --> 00:01:41,640 In this coding example here, 38 00:01:41,640 --> 00:01:44,250 with our demo controller and our constructor injection, 39 00:01:44,250 --> 00:01:46,113 we're gonna inject the CricketCoach. 40 00:01:48,000 --> 00:01:50,310 And when we run the application, we'll see the outputs 41 00:01:50,310 --> 00:01:52,980 for our BaseballCoach, CricketCoach, and TennisCoach. 42 00:01:52,980 --> 00:01:55,500 Since we're not injecting the TrackCoach 43 00:01:55,500 --> 00:01:59,340 in this given scenario, then TrackCoach is not initialized. 44 00:01:59,340 --> 00:02:00,247 So basically we're saying, 45 00:02:00,247 --> 00:02:03,420 "Hey, don't create me unless I'm actually needed. 46 00:02:03,420 --> 00:02:05,920 I don't wanna simply stand around and do nothing." 47 00:02:09,870 --> 00:02:12,780 Now to configure the other beans for lazy initialization, 48 00:02:12,780 --> 00:02:16,110 well, we'd need to add the lazy annotation to each class. 49 00:02:16,110 --> 00:02:18,510 Not a big deal if we have a small number of classes, 50 00:02:18,510 --> 00:02:21,270 but it turns into some real tedious work for a large number 51 00:02:21,270 --> 00:02:24,450 of classes. I wish there was a way we could set up 52 00:02:24,450 --> 00:02:26,340 a global configuration property, 53 00:02:26,340 --> 00:02:28,473 just do it across the board. 54 00:02:31,530 --> 00:02:33,570 And the answer's, yes, we can actually do that. 55 00:02:33,570 --> 00:02:35,940 So in our application.properties file, 56 00:02:35,940 --> 00:02:37,830 we can set this spring boot property, 57 00:02:37,830 --> 00:02:41,610 spring.main.lazy-initialization=true. 58 00:02:41,610 --> 00:02:42,840 All beans are lazy, 59 00:02:42,840 --> 00:02:45,420 no beans are created until they're explicitly needed, 60 00:02:45,420 --> 00:02:47,730 including our demo controller. 61 00:02:47,730 --> 00:02:51,000 Once we access our REST endpoint of /dailywork out, 62 00:02:51,000 --> 00:02:52,890 then Spring will determine the dependencies 63 00:02:52,890 --> 00:02:56,370 for the demo controller and for the dependency resolution, 64 00:02:56,370 --> 00:02:58,830 Spring will create an instance of the CricketCoach first, 65 00:02:58,830 --> 00:03:01,050 and then create an instance of the DemoController 66 00:03:01,050 --> 00:03:04,443 and inject the actual CricketCoach into the DemoController. 67 00:03:09,870 --> 00:03:12,005 For more diagnostics, let's add a print line to our 68 00:03:12,005 --> 00:03:13,743 DemoController constructor. 69 00:03:16,020 --> 00:03:17,400 So here at DemoController, 70 00:03:17,400 --> 00:03:19,350 we have the auto wire annotation here 71 00:03:19,350 --> 00:03:21,660 System.out.printline In constructor, 72 00:03:21,660 --> 00:03:23,070 print out the actual class name. 73 00:03:23,070 --> 00:03:25,050 So again, for the dependency resolution, 74 00:03:25,050 --> 00:03:27,207 Spring will create an instance of the CricketCoach first, 75 00:03:27,207 --> 00:03:29,820 then create an instance of the DemoController, 76 00:03:29,820 --> 00:03:33,633 and inject that into the actual DemoController. 77 00:03:37,796 --> 00:03:39,660 All right, so kind of stepping back here and looking 78 00:03:39,660 --> 00:03:43,076 at lazy initialization, as far as the advantages: 79 00:03:43,076 --> 00:03:45,630 it only creates the objects as needed, 80 00:03:45,630 --> 00:03:48,390 it may help you with faster startup time, 81 00:03:48,390 --> 00:03:50,913 if you have a large number of components. 82 00:03:52,560 --> 00:03:55,680 The disadvantages: If you have some web related components 83 00:03:55,680 --> 00:03:59,910 like RestController, it will not be created until requested. 84 00:03:59,910 --> 00:04:02,370 So the first time out, you'll have to kind of 85 00:04:02,370 --> 00:04:04,410 create it first and then use it. 86 00:04:04,410 --> 00:04:06,150 May not discover any configuration issues 87 00:04:06,150 --> 00:04:09,150 until too late. And you also need to make sure 88 00:04:09,150 --> 00:04:13,020 you have enough memory for all beans once created. 89 00:04:13,020 --> 00:04:15,750 So with this lazy initialization feature, 90 00:04:15,750 --> 00:04:18,810 it's actually disabled by default. 91 00:04:18,810 --> 00:04:20,850 You should really profile your application 92 00:04:20,850 --> 00:04:23,700 before configuring lazy initialization, 93 00:04:23,700 --> 00:04:26,130 to see if the advantages will even help you. 94 00:04:26,130 --> 00:04:29,040 And also, I wanna say, avoid the common pitfall 95 00:04:29,040 --> 00:04:31,740 of premature optimization. 96 00:04:31,740 --> 00:04:33,330 Because you could try and optimize something 97 00:04:33,330 --> 00:04:34,950 that's really not even worth it. 98 00:04:34,950 --> 00:04:37,350 All right, so just be aware of that, 99 00:04:37,350 --> 00:04:40,950 if you're moving to thinking about lazy initialization. 100 00:04:40,950 --> 00:04:42,690 However, I did want to cover it here, 101 00:04:42,690 --> 00:04:44,340 just so you understand the concept, 102 00:04:44,340 --> 00:04:46,440 and just so you understand the techniques. 103 00:04:47,790 --> 00:04:49,830 Alrighty, so this is all really good stuff here. 104 00:04:49,830 --> 00:04:51,210 Let's go ahead and move into the next video, 105 00:04:51,210 --> 00:04:52,893 and let's do a code example. 8595

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.