Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,110 --> 00:00:03,710
Instructor: In this video will cover bean scopes.
2
00:00:06,960 --> 00:00:09,180
Scope refers to the lifecycle of a bean.
3
00:00:09,180 --> 00:00:11,250
Such as, how long does the bean live,
4
00:00:11,250 --> 00:00:14,373
how many instances are created, and how is the bean shared?
5
00:00:17,790 --> 00:00:20,730
Now the default scope in spring is singleton.
6
00:00:20,730 --> 00:00:23,703
Okay, so the default scope is singleton. Very important.
7
00:00:27,390 --> 00:00:28,680
What is a singleton?
8
00:00:28,680 --> 00:00:31,440
Well, Spring Container creates only one instance
9
00:00:31,440 --> 00:00:34,530
of the bean by default, and it's cached in memory
10
00:00:34,530 --> 00:00:36,990
and all dependency injections for that bean
11
00:00:36,990 --> 00:00:39,540
will reference the same bean.
12
00:00:39,540 --> 00:00:40,680
All right, so it's just a singleton,
13
00:00:40,680 --> 00:00:42,780
one single item that's created in memory
14
00:00:42,780 --> 00:00:45,573
and shared amongst different other items.
15
00:00:49,710 --> 00:00:50,820
Here's an example of this.
16
00:00:50,820 --> 00:00:52,440
So I have this demo controller.
17
00:00:52,440 --> 00:00:56,190
I have two references here, myCoach, anotherCoach.
18
00:00:56,190 --> 00:00:58,893
I'll inject it using qualifier CricketCoach, theCoach,
19
00:00:59,790 --> 00:01:02,730
another qualifier in CricketCoach, theAnotherCoach,
20
00:01:02,730 --> 00:01:05,099
and they both point to the same instance,
21
00:01:05,099 --> 00:01:09,120
because by default, spring beans are singleton beans.
22
00:01:09,120 --> 00:01:11,170
There's only one instance that's created.
23
00:01:14,880 --> 00:01:18,690
Now, we could also explicitly specify the bean scope.
24
00:01:18,690 --> 00:01:20,370
So in our class cricket coach
25
00:01:20,370 --> 00:01:23,160
we could make use of scope annotation,
26
00:01:23,160 --> 00:01:25,080
and then we give configurable bean factory
27
00:01:25,080 --> 00:01:26,973
dot scope singleton.
28
00:01:30,900 --> 00:01:33,000
There's additional spring bean scopes.
29
00:01:33,000 --> 00:01:36,360
We already saw the first one here for the singleton scope.
30
00:01:36,360 --> 00:01:37,950
There's also prototype scope.
31
00:01:37,950 --> 00:01:39,480
It'll create a new bean incense
32
00:01:39,480 --> 00:01:43,590
for each container request or for each injection point.
33
00:01:43,590 --> 00:01:44,490
There's request scope.
34
00:01:44,490 --> 00:01:48,960
That's scoped to an HTTP web request only used for web apps.
35
00:01:48,960 --> 00:01:50,790
There's also a session scope that's scoped
36
00:01:50,790 --> 00:01:52,440
to an HTTP web session.
37
00:01:52,440 --> 00:01:54,600
Again, only used for web apps.
38
00:01:54,600 --> 00:01:56,340
And then there's a global session that's scoped
39
00:01:56,340 --> 00:02:00,663
to the global HTTP web session in the case of web apps.
40
00:02:04,140 --> 00:02:06,813
Now let's take a look at a prototype scope example.
41
00:02:07,710 --> 00:02:10,380
Now with prototype scope, a new object instance is created
42
00:02:10,380 --> 00:02:11,793
for each injection.
43
00:02:12,870 --> 00:02:14,460
So we have to specify the scope
44
00:02:14,460 --> 00:02:16,380
on the actual class here the bean.
45
00:02:16,380 --> 00:02:18,360
So cricket coach, we have scope
46
00:02:18,360 --> 00:02:21,690
configurable bean factory dot scope prototype,
47
00:02:21,690 --> 00:02:23,583
create a new one for each instance.
48
00:02:26,820 --> 00:02:29,040
Let's look at a prototype scope example,
49
00:02:29,040 --> 00:02:31,383
a new object instance for each injection.
50
00:02:32,460 --> 00:02:35,340
We have our demo controller, we have our two fields here
51
00:02:35,340 --> 00:02:38,190
and then we reference our cricket coach, the coach,
52
00:02:38,190 --> 00:02:40,230
cricket coach, the another coach.
53
00:02:40,230 --> 00:02:42,660
Since this is a prototype scope bean,
54
00:02:42,660 --> 00:02:46,020
you'll get a new object instance for each injection.
55
00:02:46,020 --> 00:02:48,840
So they point to two different areas of memory
56
00:02:48,840 --> 00:02:50,193
or two different beans.
57
00:02:53,190 --> 00:02:54,780
How could we check on the scope?
58
00:02:54,780 --> 00:02:56,070
Well, we could write some code here
59
00:02:56,070 --> 00:02:59,190
as far as a get mapping for slash check
60
00:02:59,190 --> 00:03:00,510
And a return to results here.
61
00:03:00,510 --> 00:03:01,800
So I'll say comparing beans
62
00:03:01,800 --> 00:03:03,900
to my coach equals another coach
63
00:03:03,900 --> 00:03:06,090
and then I put the actual expression here.
64
00:03:06,090 --> 00:03:09,540
So this will check to see if this is the same bean.
65
00:03:09,540 --> 00:03:11,160
This will return true or false depending
66
00:03:11,160 --> 00:03:12,183
on the bean scope.
67
00:03:13,080 --> 00:03:15,390
For singleton scope, they return true,
68
00:03:15,390 --> 00:03:16,223
because remember
69
00:03:16,223 --> 00:03:18,360
singleton is shared and it points to the same bean.
70
00:03:18,360 --> 00:03:20,550
If it's prototype scope, it'll return false
71
00:03:20,550 --> 00:03:22,440
because with prototype you get a new instance
72
00:03:22,440 --> 00:03:26,070
for each one of them and they won't point to the same item.
73
00:03:26,070 --> 00:03:27,570
All right, so this is really good.
74
00:03:27,570 --> 00:03:29,040
Let's go ahead and dive into our IDE
75
00:03:29,040 --> 00:03:31,863
and let's write the code for this and check out scopes.
5933
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.