Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,791 --> 00:00:02,624
Instructor: In this video,
2
00:00:02,624 --> 00:00:04,703
we're gonna configure beans with Java code.
3
00:00:07,980 --> 00:00:10,170
What we'll do is we'll introduce a new coach.
4
00:00:10,170 --> 00:00:11,550
So we'll have this swim coach
5
00:00:11,550 --> 00:00:13,383
that implements the coach interface,
6
00:00:14,310 --> 00:00:16,200
and we're not gonna use any special annotations
7
00:00:16,200 --> 00:00:17,033
on the class.
8
00:00:17,033 --> 00:00:17,866
So for example,
9
00:00:17,866 --> 00:00:20,010
we're not gonna use the @Component annotation.
10
00:00:20,010 --> 00:00:23,163
Instead, we're gonna configure this via Java code.
11
00:00:27,510 --> 00:00:29,940
And here's our development process.
12
00:00:29,940 --> 00:00:30,773
So in the first step,
13
00:00:30,773 --> 00:00:33,660
we're gonna create this configuration class,
14
00:00:33,660 --> 00:00:35,970
and then in step two we'll define a @Bean method
15
00:00:35,970 --> 00:00:37,383
to configure the bean.
16
00:00:39,300 --> 00:00:42,250
And then finally we'll inject the bean into our controller.
17
00:00:46,170 --> 00:00:48,150
Okay, step one of creating a Java class
18
00:00:48,150 --> 00:00:51,300
and annotating it using the @Configuration annotation.
19
00:00:51,300 --> 00:00:54,240
So we'll have this public class here called sport config.
20
00:00:54,240 --> 00:00:57,180
And then we have this annotation for configuration.
21
00:00:57,180 --> 00:00:59,070
So this is basically a configuration class
22
00:00:59,070 --> 00:01:02,553
for configuring Spring using our custom approach.
23
00:01:05,640 --> 00:01:06,473
Then in step two,
24
00:01:06,473 --> 00:01:09,450
we'll define the @Bean method to configure the bean.
25
00:01:09,450 --> 00:01:11,040
So in this configuration class,
26
00:01:11,040 --> 00:01:14,310
we'll have this new annotation here, @Bean,
27
00:01:14,310 --> 00:01:17,760
and then we'll have this method, public coach, swim coach.
28
00:01:17,760 --> 00:01:18,593
And inside of here,
29
00:01:18,593 --> 00:01:21,120
we'll actually return an instance of the swim coach.
30
00:01:21,120 --> 00:01:23,940
So we'll manually construct the object
31
00:01:23,940 --> 00:01:26,580
and return it to the given caller.
32
00:01:26,580 --> 00:01:29,790
Now the bean ID actually defaults to the method name.
33
00:01:29,790 --> 00:01:32,853
So this bean will have a bean ID of swim coach.
34
00:01:36,570 --> 00:01:38,310
And in step three, we'll inject the bean
35
00:01:38,310 --> 00:01:39,633
into our controller.
36
00:01:41,040 --> 00:01:42,930
Here's our demo controller code.
37
00:01:42,930 --> 00:01:45,270
And then notice here for the qualifier,
38
00:01:45,270 --> 00:01:46,860
we make use of the bean id.
39
00:01:46,860 --> 00:01:50,100
And so in this case, the bean ID is swim coach,
40
00:01:50,100 --> 00:01:51,960
because we're using the default bean ID
41
00:01:51,960 --> 00:01:54,993
based on the method name of that bean annotation.
42
00:01:58,710 --> 00:02:01,743
Now here's the use case for the @Bean annotation.
43
00:02:02,850 --> 00:02:05,430
You may wonder, well using the new keyword, is that it?
44
00:02:05,430 --> 00:02:07,080
Why not just annotate the class
45
00:02:07,080 --> 00:02:09,509
with the component annotation?
46
00:02:09,509 --> 00:02:10,710
We could do that in this example
47
00:02:10,710 --> 00:02:12,360
since we actually have access to the code.
48
00:02:12,360 --> 00:02:14,520
But I'll show you some scenarios where we will need
49
00:02:14,520 --> 00:02:16,353
to use the @Bean annotation.
50
00:02:20,190 --> 00:02:22,170
The main use case for the @Bean annotation
51
00:02:22,170 --> 00:02:25,080
is to make an existing third-party class available
52
00:02:25,080 --> 00:02:26,910
to the Spring framework.
53
00:02:26,910 --> 00:02:29,220
In these scenarios here, you may not have access
54
00:02:29,220 --> 00:02:31,440
to the source code of the third-party class.
55
00:02:31,440 --> 00:02:34,650
You simply may have a JAR file, you wanna pull that in,
56
00:02:34,650 --> 00:02:37,230
and then leverage that as a Spring bean.
57
00:02:37,230 --> 00:02:38,220
So that's the whole idea.
58
00:02:38,220 --> 00:02:41,340
So you wanna take a given outside a third-party class
59
00:02:41,340 --> 00:02:44,070
and make that class available as a Spring bean.
60
00:02:44,070 --> 00:02:45,502
So that's the main motivation
61
00:02:45,502 --> 00:02:48,093
for using the @Bean annotation.
62
00:02:51,600 --> 00:02:54,600
And then also, let me give you a real world project example.
63
00:02:55,560 --> 00:02:57,150
On one of the projects that I worked on,
64
00:02:57,150 --> 00:03:00,630
we made use of Amazon Web Services, or AWS,
65
00:03:00,630 --> 00:03:02,460
to store documents.
66
00:03:02,460 --> 00:03:03,293
AWS has this feature
67
00:03:03,293 --> 00:03:07,560
called the Amazon Simple Storage Service, or Amazon S3.
68
00:03:07,560 --> 00:03:10,320
S3 is really just a cloud-based storage system
69
00:03:10,320 --> 00:03:12,720
for storing PDF documents, images,
70
00:03:12,720 --> 00:03:15,060
or any type of binary object out there
71
00:03:15,060 --> 00:03:17,010
or text object out there that you want.
72
00:03:17,010 --> 00:03:18,660
So just think of it as like a file store
73
00:03:18,660 --> 00:03:19,890
that's in the cloud.
74
00:03:19,890 --> 00:03:23,070
And we wanted to make use of the AWS S3 client
75
00:03:23,070 --> 00:03:25,890
as a Spring bean in our application.
76
00:03:25,890 --> 00:03:27,630
So we wanted to have our code
77
00:03:27,630 --> 00:03:28,890
that could communicate with the cloud
78
00:03:28,890 --> 00:03:31,863
and store documents and also retrieve documents.
79
00:03:35,520 --> 00:03:39,060
Now the AWS S3 client code is part of the AWS SDK.
80
00:03:39,060 --> 00:03:42,390
So we can't really modify the AWS SDK source code
81
00:03:42,390 --> 00:03:45,000
'cause it comes as a JAR file or Maven dependency.
82
00:03:45,000 --> 00:03:48,060
So we can't simply just add the @Component annotation
83
00:03:48,060 --> 00:03:49,320
to their code, right?
84
00:03:49,320 --> 00:03:53,070
It's all managed and coordinated by the AWS team.
85
00:03:53,070 --> 00:03:55,140
However, we can configure it
86
00:03:55,140 --> 00:03:58,233
as a Spring bean using the @Bean annotation.
87
00:04:01,440 --> 00:04:03,840
So here's some sample codes similar
88
00:04:03,840 --> 00:04:05,880
to the project that I worked on.
89
00:04:05,880 --> 00:04:09,030
So we have this configuration class called documents config,
90
00:04:09,030 --> 00:04:11,550
and then we have this @Bean annotation,
91
00:04:11,550 --> 00:04:13,620
and we make use of this S3 client.
92
00:04:13,620 --> 00:04:18,620
So S3 client is from the AWS S3 SDK,
93
00:04:18,870 --> 00:04:20,339
and we have this remote client.
94
00:04:20,339 --> 00:04:22,620
We go through and we create an S3 client instance
95
00:04:22,620 --> 00:04:24,690
to connect to AWS S3 storage.
96
00:04:24,690 --> 00:04:27,510
So we go through all the credentials provider,
97
00:04:27,510 --> 00:04:29,560
we select our region, we go through
98
00:04:30,515 --> 00:04:32,181
and build the client and so forth.
99
00:04:32,181 --> 00:04:33,030
But at this point, once we have the client built in,
100
00:04:33,030 --> 00:04:35,130
we can return this S3 client.
101
00:04:35,130 --> 00:04:38,820
And so now it's a Spring bean,
102
00:04:38,820 --> 00:04:41,340
and the nice thing about it is that once it's a Spring bean,
103
00:04:41,340 --> 00:04:44,673
then we can use it in other parts of our Spring application.
104
00:04:47,940 --> 00:04:51,333
Now I can go through and inject the S3 client as a bean.
105
00:04:52,560 --> 00:04:54,810
So here's my document service.
106
00:04:54,810 --> 00:04:56,790
I have this private S3 client,
107
00:04:56,790 --> 00:04:59,730
and then I can auto-wire in this S3 client.
108
00:04:59,730 --> 00:05:02,460
So for this given constructor injection here,
109
00:05:02,460 --> 00:05:05,070
document service, then I'll auto-wire,
110
00:05:05,070 --> 00:05:07,620
then I'll inject the S3 client bean
111
00:05:07,620 --> 00:05:09,470
and make the appropriate assignments.
112
00:05:13,140 --> 00:05:15,030
Then I could have this other method here
113
00:05:15,030 --> 00:05:16,380
in my document service
114
00:05:16,380 --> 00:05:18,630
once I've already auto wired this bean here.
115
00:05:18,630 --> 00:05:20,430
Then I can go ahead and process the documents.
116
00:05:20,430 --> 00:05:23,400
So I'll pass in some document, objects,
117
00:05:23,400 --> 00:05:25,500
something that's specific to our project.
118
00:05:25,500 --> 00:05:27,690
But basically, we're gonna use this
119
00:05:27,690 --> 00:05:30,900
to actually store a document in the cloud.
120
00:05:30,900 --> 00:05:35,220
So I go through and create this, put object request builder,
121
00:05:35,220 --> 00:05:39,780
set up a bucket name, the key, and the ACLs and so forth.
122
00:05:39,780 --> 00:05:42,360
And then I'll actually perform the put object operation
123
00:05:42,360 --> 00:05:46,140
to the AWS S3 cloud using our auto-wired bean.
124
00:05:46,140 --> 00:05:48,510
So S3 client dot put object,
125
00:05:48,510 --> 00:05:50,160
pass in the appropriate parameters,
126
00:05:50,160 --> 00:05:52,950
and then I'll actually store our document in the cloud.
127
00:05:52,950 --> 00:05:55,170
So don't worry about all the gory details here,
128
00:05:55,170 --> 00:05:58,530
but basically we can auto-wire in this S3 client.
129
00:05:58,530 --> 00:06:00,180
And then from there, we can actually go through
130
00:06:00,180 --> 00:06:02,940
and store our document in the cloud,
131
00:06:02,940 --> 00:06:05,343
or process our document to go into the cloud.
132
00:06:09,270 --> 00:06:11,520
So kind of as a wrap up here, you know,
133
00:06:11,520 --> 00:06:15,060
with this example, is that we could use the AWS S3 client
134
00:06:15,060 --> 00:06:17,040
in our Spring application.
135
00:06:17,040 --> 00:06:20,160
The S3 client class was not originally annotated
136
00:06:20,160 --> 00:06:22,440
with the @Component annotation.
137
00:06:22,440 --> 00:06:24,360
However, we configured the S3 client
138
00:06:24,360 --> 00:06:27,990
as a Spring bean using the @Bean annotation.
139
00:06:27,990 --> 00:06:29,250
It is now a Spring bean,
140
00:06:29,250 --> 00:06:32,970
and we can inject it into other services of our application.
141
00:06:32,970 --> 00:06:34,290
So the main use case here
142
00:06:34,290 --> 00:06:36,750
for the @Bean annotation is to make an existing
143
00:06:36,750 --> 00:06:40,350
third party class available to the Spring framework.
144
00:06:40,350 --> 00:06:42,090
So hopefully, this kind of pulls it together
145
00:06:42,090 --> 00:06:43,920
for you to see the real use case here
146
00:06:43,920 --> 00:06:45,993
for the @Bean annotation.
147
00:06:47,220 --> 00:06:49,140
Alrighty, so let's go ahead and move to the next video,
148
00:06:49,140 --> 00:06:51,330
and we'll write some code where we can test out
149
00:06:51,330 --> 00:06:52,830
using the @Bean annotation.
150
00:06:52,830 --> 00:06:55,440
So I'll see ya in the next video.
151
00:06:55,440 --> 00:06:56,273
Yo yo.
11977
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.