Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,889 --> 00:00:03,038
In this video, we're gonna discuss
2
2
00:00:03,038 --> 00:00:06,038
the bean scopes available in Spring.
3
3
00:00:07,715 --> 00:00:09,968
So what exactly are scopes?
4
4
00:00:09,968 --> 00:00:12,979
Well, a scope refers to the lifecycle of a bean,
5
5
00:00:12,979 --> 00:00:16,108
for example, it tells you how long the bean will live,
6
6
00:00:16,108 --> 00:00:18,533
how many instances will be created
7
7
00:00:18,533 --> 00:00:22,700
and also how is the bean shared in the Spring environment?
8
8
00:00:24,865 --> 00:00:27,697
So the default scope for a bean is singleton,
9
9
00:00:27,697 --> 00:00:30,211
so here's an example here of just some bean code,
10
10
00:00:30,211 --> 00:00:33,402
that we had earlier and we didn't explicitly give a scope,
11
11
00:00:33,402 --> 00:00:37,112
so by default, the scope is singleton.
12
12
00:00:37,112 --> 00:00:39,124
But now you're probably wondering,
13
13
00:00:39,124 --> 00:00:41,050
well, what exactly is a singleton? (laughs)
14
14
00:00:41,050 --> 00:00:43,529
we know it's a default scope, but what is it?
15
15
00:00:43,529 --> 00:00:46,035
Well, for a singleton, the Spring Container
16
16
00:00:46,035 --> 00:00:50,534
creates only one instance of the bean, it's cached in memory
17
17
00:00:50,534 --> 00:00:52,766
and then all requests for that bean
18
18
00:00:52,766 --> 00:00:56,543
will return a shared reference to the same bean,
19
19
00:00:56,543 --> 00:00:59,017
so the end result is that there is only one bean
20
20
00:00:59,017 --> 00:01:01,267
and everyone will share it.
21
21
00:01:03,579 --> 00:01:06,639
Here's a nice diagram to kind of show you this example.
22
22
00:01:06,639 --> 00:01:09,361
In this example, we have theCoach
23
23
00:01:09,361 --> 00:01:11,323
equals context.getBean myCoach
24
24
00:01:11,323 --> 00:01:13,544
and it'll give you a reference to like a TrackCoach,
25
25
00:01:13,544 --> 00:01:16,836
that you have defined and then later on in the code,
26
26
00:01:16,836 --> 00:01:18,724
if you would also do a similar thing,
27
27
00:01:18,724 --> 00:01:22,504
saying context.getBean myCoach, the same bean id,
28
28
00:01:22,504 --> 00:01:26,830
then it'll basically give you a reference to the same bean.
29
29
00:01:26,830 --> 00:01:28,923
We have these two object references here
30
30
00:01:28,923 --> 00:01:32,144
and they point to the same area of memory
31
31
00:01:32,144 --> 00:01:35,221
or they point to the same bean,
32
32
00:01:35,221 --> 00:01:37,748
so again, Spring makes use of a singleton,
33
33
00:01:37,748 --> 00:01:41,262
it'll create only one bean and then share it
34
34
00:01:41,262 --> 00:01:43,382
for everyone who requests that bean,
35
35
00:01:43,382 --> 00:01:45,288
so the singleton scope is default
36
36
00:01:45,288 --> 00:01:48,277
and the best use case for this is for a stateless bean,
37
37
00:01:48,277 --> 00:01:51,860
where you don't need to maintain any state.
38
38
00:01:52,851 --> 00:01:55,382
You can explicitly specify the bean scope,
39
39
00:01:55,382 --> 00:01:57,161
I mean, by now you know that by default,
40
40
00:01:57,161 --> 00:01:58,666
you have the singleton scope,
41
41
00:01:58,666 --> 00:02:00,416
but if you want to explicitly specify,
42
42
00:02:00,416 --> 00:02:02,720
then you make use of the scope attribute,
43
43
00:02:02,720 --> 00:02:04,631
so you say scope equals singleton
44
44
00:02:04,631 --> 00:02:05,940
and that'll make it a singleton bean
45
45
00:02:05,940 --> 00:02:08,993
and that's kind of the preferred approach
46
46
00:02:08,993 --> 00:02:13,801
to minimize the number of beans, that are created.
47
47
00:02:13,801 --> 00:02:16,239
But now, there are additional Spring bean scopes,
48
48
00:02:16,239 --> 00:02:17,621
that you can make use of.
49
49
00:02:17,621 --> 00:02:19,876
We've already covered singleton,
50
50
00:02:19,876 --> 00:02:21,606
there's also the prototype scope,
51
51
00:02:21,606 --> 00:02:24,386
which creates a new bean instance for each container request
52
52
00:02:24,386 --> 00:02:26,484
and we'll see examples of that coming up
53
53
00:02:26,484 --> 00:02:31,124
and we'll also use that as a demo in the video coming up,
54
54
00:02:31,124 --> 00:02:32,872
then the next three items here,
55
55
00:02:32,872 --> 00:02:34,877
request, session and global-session,
56
56
00:02:34,877 --> 00:02:37,357
these scopes are only used in a web environment,
57
57
00:02:37,357 --> 00:02:40,951
so request is for a given web request,
58
58
00:02:40,951 --> 00:02:44,234
session is for a HTTP web session, for like session tracking
59
59
00:02:44,234 --> 00:02:46,150
for like maybe a shopping cart or something
60
60
00:02:46,150 --> 00:02:49,488
and then global-session is scope application-wide,
61
61
00:02:49,488 --> 00:02:51,268
but we'll talk more about these later,
62
62
00:02:51,268 --> 00:02:53,520
when we get into the Spring MVC section,
63
63
00:02:53,520 --> 00:02:57,818
but for now, we'll simply focus on singleton and prototype.
64
64
00:02:57,818 --> 00:03:00,129
So here's an example of using prototype scope,
65
65
00:03:00,129 --> 00:03:02,872
so again, remember, prototype scope,
66
66
00:03:02,872 --> 00:03:05,495
a new object is created for each request,
67
67
00:03:05,495 --> 00:03:09,310
so in this example here, I have my bean id of myCoach
68
68
00:03:09,310 --> 00:03:12,047
and I have scope equals prototype,
69
69
00:03:12,047 --> 00:03:13,547
so that means that every time
70
70
00:03:13,547 --> 00:03:16,544
I make a request for this myCoach,
71
71
00:03:16,544 --> 00:03:19,877
they'll create a new instance each time.
72
72
00:03:21,012 --> 00:03:23,368
So, a nice little diagram here.
73
73
00:03:23,368 --> 00:03:24,901
So the line of code at the top,
74
74
00:03:24,901 --> 00:03:27,447
theCoach equals context.getBean myCoach,
75
75
00:03:27,447 --> 00:03:29,713
it'll create a new instance of that bean,
76
76
00:03:29,713 --> 00:03:32,949
I'll get a reference to it and then a similar thing here,
77
77
00:03:32,949 --> 00:03:37,116
when I say alphaCoach equals context.getBean myCoach,
78
78
00:03:37,963 --> 00:03:39,826
it'll create a new object for you
79
79
00:03:39,826 --> 00:03:42,743
and you'll have your own reference.
80
80
00:03:43,637 --> 00:03:46,407
So the prototype scope is good for keeping track
81
81
00:03:46,407 --> 00:03:49,160
of stateful data, so again, whenever you see prototype,
82
82
00:03:49,160 --> 00:03:52,781
just think of the new keyword, it's gonna create a new bean
83
83
00:03:52,781 --> 00:03:57,418
for each request for that component or that object.
84
84
00:03:57,418 --> 00:03:59,750
Alright, so this is some really good stuff.
85
85
00:03:59,750 --> 00:04:01,117
Let's go ahead and move forward,
86
86
00:04:01,117 --> 00:04:03,390
in the next videos, we're gonna dive into Eclipse
87
87
00:04:03,390 --> 00:04:04,766
and we'll actually write some code,
88
88
00:04:04,766 --> 00:04:07,036
that'll make use of the singleton scope
89
89
00:04:07,036 --> 00:04:08,614
and the prototype scope,
90
90
00:04:08,614 --> 00:04:11,165
so you can see everything in action.
91
91
00:04:11,165 --> 00:04:14,582
Alrighty, I'll see you in the next video.
8101
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.