spring-5-with-spring-boot-2 / 07 - Spring Getting Started /008 DI Using Spring Boot_en.srt
Tan115's picture
Add files using upload-large-folder tool
f0e6b9f verified
Raw
History Blame Contribute Delete
16.5 kB
1
00:00:00,000 --> 00:00:03,000
(gentle music)
2
00:00:03,000 --> 00:00:07,000
-: Now let's try to implement dependency injection in Spring.
3
00:00:07,000 --> 00:00:09,000
Now the thing is we started with Spring Boot,
4
00:00:09,000 --> 00:00:12,000
but then the actual sense or what happens
5
00:00:12,000 --> 00:00:15,000
behind the scene will make much more sense once we do this
6
00:00:15,000 --> 00:00:17,000
using Spring framework.
7
00:00:17,000 --> 00:00:18,000
Now, why we are doing in Spring Boot is
8
00:00:18,000 --> 00:00:19,000
because to understand what Spring Boot can do.
9
00:00:19,000 --> 00:00:24,000
In terms of dependency injection, of course Spring Boot
10
00:00:24,000 --> 00:00:26,000
can do lot of different things,
11
00:00:26,000 --> 00:00:28,000
but we are starting with the first step.
12
00:00:28,000 --> 00:00:30,000
Okay, now when I say what we wanna do here,
13
00:00:30,000 --> 00:00:33,000
before I do that, I have one more update.
14
00:00:33,000 --> 00:00:36,000
So in the last video we talked about the same thing.
15
00:00:36,000 --> 00:00:40,000
We ran this application but it was using Java 17.
16
00:00:40,000 --> 00:00:42,000
So what I did is I have installed Java 21
17
00:00:42,000 --> 00:00:43,000
and now we are going
18
00:00:43,000 --> 00:00:46,000
to run all the application using Java 21.
19
00:00:46,000 --> 00:00:49,000
Okay, so what dependency I'm talking about?
20
00:00:49,000 --> 00:00:50,000
See this is our main code, right?
21
00:00:50,000 --> 00:00:52,000
So we have a main application here.
22
00:00:52,000 --> 00:00:53,000
In this if you can see,
23
00:00:53,000 --> 00:00:55,000
what we are doing is we have a main method,
24
00:00:55,000 --> 00:00:58,000
and then we are simply saying Springapplication.run.
25
00:00:58,000 --> 00:01:01,000
Basically it activates your Spring framework.
26
00:01:01,000 --> 00:01:03,000
It also has something called a container,
27
00:01:03,000 --> 00:01:06,000
where you can create your Spring beans.
28
00:01:06,000 --> 00:01:08,000
As I mentioned before, Spring is responsible
29
00:01:08,000 --> 00:01:09,000
to create the objects
30
00:01:09,000 --> 00:01:12,000
and these objects are basically called beans.
31
00:01:12,000 --> 00:01:15,000
So any object which is created by Spring, so created,
32
00:01:15,000 --> 00:01:18,000
managed by Spring will be called beans.
33
00:01:18,000 --> 00:01:22,000
Nothing fancy, it's just normal object, different name.
34
00:01:22,000 --> 00:01:25,000
Okay so this is how you run the container,
35
00:01:25,000 --> 00:01:27,000
but we are not using container in this code.
36
00:01:27,000 --> 00:01:29,000
We are simply running Hello World.
37
00:01:29,000 --> 00:01:31,000
So nothing about containers till now,
38
00:01:31,000 --> 00:01:33,000
but I want to use container now.
39
00:01:33,000 --> 00:01:36,000
So what do is I want to create a simple class here.
40
00:01:36,000 --> 00:01:37,000
Very simple class.
41
00:01:37,000 --> 00:01:41,000
Let's say I want to say maybe alien, okay?
42
00:01:41,000 --> 00:01:43,000
Now if you're not sure why I always create
43
00:01:43,000 --> 00:01:45,000
or work with alien, basically I believe
44
00:01:45,000 --> 00:01:48,000
that programmers are aliens, okay?
45
00:01:48,000 --> 00:01:50,000
It's because we don't live in this world,
46
00:01:50,000 --> 00:01:53,000
mentally or virtually, we live in virtual world, right?
47
00:01:53,000 --> 00:01:56,000
So we are aliens anyway, nothing fancy.
48
00:01:56,000 --> 00:01:59,000
Imagine every time I say alien you say programmer.
49
00:01:59,000 --> 00:02:00,000
That's fine.
50
00:02:00,000 --> 00:02:01,000
Okay so we got alien here
51
00:02:01,000 --> 00:02:04,000
and in this alien, what I'll do is I want
52
00:02:04,000 --> 00:02:05,000
to do a very simple thing, okay?
53
00:02:05,000 --> 00:02:08,000
First of all, I have to deactivate my Github copilot.
54
00:02:08,000 --> 00:02:09,000
It's giving some suggestion, okay?
55
00:02:09,000 --> 00:02:11,000
So what I'll do now is I will create a very
56
00:02:11,000 --> 00:02:12,000
simple method here.
57
00:02:12,000 --> 00:02:15,000
I will say public void code because there's a programmer
58
00:02:15,000 --> 00:02:17,000
or as as alien, we code.
59
00:02:17,000 --> 00:02:19,000
And then what I'm going to do here is
60
00:02:19,000 --> 00:02:20,000
I'm going to do a very simple thing.
61
00:02:20,000 --> 00:02:21,000
I will simply print something
62
00:02:21,000 --> 00:02:23,000
and let's say, I'll say coding, that's it.
63
00:02:23,000 --> 00:02:27,000
I'm just saying code. So printIn Coding.
64
00:02:27,000 --> 00:02:30,000
But now I want to call this from my main method.
65
00:02:30,000 --> 00:02:32,000
So let me just keep this here.
66
00:02:32,000 --> 00:02:36,000
And okay, so we got both the codes parallelly here
67
00:02:36,000 --> 00:02:38,000
and now if you can see if I will remove this Hello World,
68
00:02:38,000 --> 00:02:40,000
I want to create the object of alien
69
00:02:40,000 --> 00:02:42,000
and we know how to do that, right?
70
00:02:42,000 --> 00:02:47,000
We can simply create alien object here and alien object
71
00:02:47,000 --> 00:02:49,000
and then I can simply call code.
72
00:02:49,000 --> 00:02:51,000
That's the only way you can call this method
73
00:02:51,000 --> 00:02:53,000
because this is a non-static method,
74
00:02:53,000 --> 00:02:56,000
and if you want to call it, you need object.
75
00:02:56,000 --> 00:02:58,000
And that's what we are doing here, we are saying alien obj,
76
00:02:58,000 --> 00:03:01,000
got a new alien, we got the object created.
77
00:03:01,000 --> 00:03:04,000
And then using the reference we are calling the method.
78
00:03:04,000 --> 00:03:05,000
Let's see if this works.
79
00:03:05,000 --> 00:03:08,000
I'll right click here, see run on this
80
00:03:08,000 --> 00:03:10,000
and you will get the output.
81
00:03:10,000 --> 00:03:12,000
Output says okay, it's taking some time,
82
00:03:12,000 --> 00:03:13,000
but output says coding.
83
00:03:13,000 --> 00:03:16,000
So at least this is working.
84
00:03:16,000 --> 00:03:18,000
But now the idea is I don't want
85
00:03:18,000 --> 00:03:20,000
to create this object by myself.
86
00:03:20,000 --> 00:03:24,000
What I want is I want Spring to give me this object.
87
00:03:24,000 --> 00:03:27,000
That means new alien is not something I want.
88
00:03:27,000 --> 00:03:29,000
And that's tricky, right, because if you don't say
89
00:03:29,000 --> 00:03:32,000
new alien, how it'll create the object
90
00:03:32,000 --> 00:03:35,000
and now you are saying we don't want to create the object,
91
00:03:35,000 --> 00:03:36,000
we want Spring to create it.
92
00:03:36,000 --> 00:03:39,000
How Spring will know that you want to create the object?
93
00:03:39,000 --> 00:03:40,000
Now that's funny, right? Okay.
94
00:03:40,000 --> 00:03:42,000
For that, what you have to do is first of all,
95
00:03:42,000 --> 00:03:45,000
if you say Spring creates the object, let's say
96
00:03:45,000 --> 00:03:48,000
with this code, I'm assuming that Spring will create
97
00:03:48,000 --> 00:03:50,000
the object for me because we are using Spring framework,
98
00:03:50,000 --> 00:03:52,000
where exactly it'll create the object.
99
00:03:52,000 --> 00:03:54,000
So it'll create the object inside the container.
100
00:03:54,000 --> 00:03:57,000
Remember I mentioned about the containers?
101
00:03:57,000 --> 00:03:59,000
So imagine you have a JVM, inside JVM,
102
00:03:59,000 --> 00:04:01,000
you have a special area
103
00:04:01,000 --> 00:04:03,000
or the special box which is running,
104
00:04:03,000 --> 00:04:04,000
which is your container.
105
00:04:04,000 --> 00:04:07,000
So every time your Spring framework creates the object
106
00:04:07,000 --> 00:04:10,000
for you, it'll be available in the container.
107
00:04:10,000 --> 00:04:14,000
That means I have to find a way to get to that container.
108
00:04:14,000 --> 00:04:15,000
And the way you can do that is
109
00:04:15,000 --> 00:04:18,000
by using application context, okay?
110
00:04:18,000 --> 00:04:20,000
That means we have to get object for it now.
111
00:04:20,000 --> 00:04:22,000
The thing is if you look at this particular method here,
112
00:04:22,000 --> 00:04:24,000
which is Springapplication.run,
113
00:04:24,000 --> 00:04:26,000
and if you go to run method,
114
00:04:26,000 --> 00:04:30,000
and if you can see run returns the object
115
00:04:30,000 --> 00:04:33,000
of configurable application context, okay?
116
00:04:33,000 --> 00:04:36,000
This configurable application context
117
00:04:36,000 --> 00:04:38,000
extends application context.
118
00:04:38,000 --> 00:04:42,000
That means if you can simply use this run method return
119
00:04:42,000 --> 00:04:45,000
value, you got the application context object.
120
00:04:45,000 --> 00:04:47,000
Okay, let me just do that now.
121
00:04:47,000 --> 00:04:50,000
So I will say application context,
122
00:04:50,000 --> 00:04:53,000
and I will say this is name as context itself equal to.
123
00:04:53,000 --> 00:04:57,000
So what we're doing is this run returns this object context,
124
00:04:57,000 --> 00:05:00,000
which means now you have a way to communicate
125
00:05:00,000 --> 00:05:02,000
with your IOC container.
126
00:05:02,000 --> 00:05:03,000
Okay now how will you communicate?
127
00:05:03,000 --> 00:05:05,000
So it's very simple.
128
00:05:05,000 --> 00:05:09,000
You will simply say, hey context, that means hey container,
129
00:05:09,000 --> 00:05:10,000
give me the object.
130
00:05:10,000 --> 00:05:13,000
And remember, the object is not object in Spring.
131
00:05:13,000 --> 00:05:15,000
The object is called bean. Okay?
132
00:05:15,000 --> 00:05:18,000
So you have to say getBean. So you will say getBean.
133
00:05:20,000 --> 00:05:22,000
And then here you mention which class object
134
00:05:22,000 --> 00:05:24,000
you want, of course, right?
135
00:05:24,000 --> 00:05:26,000
You have to mention because in your project you might be
136
00:05:26,000 --> 00:05:28,000
having multiple classes.
137
00:05:28,000 --> 00:05:31,000
So how will your Spring framework know what you want?
138
00:05:31,000 --> 00:05:34,000
So mention, hey, you know, I want the object of alien,
139
00:05:34,000 --> 00:05:36,000
which you have created inside your container.
140
00:05:36,000 --> 00:05:38,000
Your Spring will say, okay, I got it.
141
00:05:38,000 --> 00:05:41,000
You basically need this Alien.class object.
142
00:05:41,000 --> 00:05:44,000
I mean you need object of this class,
143
00:05:44,000 --> 00:05:47,000
but the IOC container will be able
144
00:05:47,000 --> 00:05:51,000
to give you this object only if the object
145
00:05:51,000 --> 00:05:52,000
is there in the container.
146
00:05:52,000 --> 00:05:55,000
We are assuming it'll be there, but is it?
147
00:05:55,000 --> 00:05:58,000
Let's try, let's rerun this
148
00:05:58,000 --> 00:06:00,000
and let's see what it says.
149
00:06:01,000 --> 00:06:05,000
Okay, your Spring is not happy, it's giving you some error.
150
00:06:05,000 --> 00:06:09,000
It says no such bean definition. Can you see that?
151
00:06:09,000 --> 00:06:10,000
We got the exception here.
152
00:06:10,000 --> 00:06:13,000
And it says no qualifying bean of type
153
00:06:13,000 --> 00:06:15,000
com.telusko.app.alien okay?
154
00:06:15,000 --> 00:06:17,000
So basically it's not able to find this bean.
155
00:06:17,000 --> 00:06:20,000
But you'll be saying then why we are using Spring
156
00:06:20,000 --> 00:06:23,000
framework, if it cannot create the object for us?
157
00:06:23,000 --> 00:06:24,000
Actually it can.
158
00:06:24,000 --> 00:06:27,000
The only thing is see in your project,
159
00:06:27,000 --> 00:06:29,000
let's say you have a hundred classes,
160
00:06:29,000 --> 00:06:32,000
do you really want Spring to create all the object for you?
161
00:06:32,000 --> 00:06:34,000
Of course not, right?
162
00:06:34,000 --> 00:06:35,000
So there should be a way
163
00:06:35,000 --> 00:06:38,000
that you should be able to tell Spring framework
164
00:06:38,000 --> 00:06:41,000
which object you want and which you don't want.
165
00:06:41,000 --> 00:06:42,000
So Spring by default says,
166
00:06:42,000 --> 00:06:44,000
"I will not create any object for you.
167
00:06:44,000 --> 00:06:46,000
Only when you mention that you want the object
168
00:06:46,000 --> 00:06:48,000
or you want me to create the object,
169
00:06:48,000 --> 00:06:50,000
then only I will create."
170
00:06:50,000 --> 00:06:53,000
How will you tell your Spring framework you want the object?
171
00:06:53,000 --> 00:06:56,000
And for that you have to use annotations.
172
00:06:56,000 --> 00:06:59,000
So on top of it you will simply use annotation,
173
00:06:59,000 --> 00:07:00,000
which is called component.
174
00:07:00,000 --> 00:07:04,000
Now making this class as a component, you are making sure
175
00:07:04,000 --> 00:07:06,000
that your Spring knows that Spring has
176
00:07:06,000 --> 00:07:09,000
to manage this particular object.
177
00:07:09,000 --> 00:07:13,000
So create the object, assemble the object, and manage it.
178
00:07:13,000 --> 00:07:17,000
Everything will be done by Spring just by this annotation.
179
00:07:17,000 --> 00:07:18,000
I know you don't believe me.
180
00:07:18,000 --> 00:07:23,000
Let's see, let's run this and here we go.
181
00:07:23,000 --> 00:07:25,000
Okay, so you can see we got the output coding.
182
00:07:25,000 --> 00:07:29,000
Basically what is happening here is you have not
183
00:07:29,000 --> 00:07:30,000
created any object.
184
00:07:30,000 --> 00:07:31,000
Spring is doing it.
185
00:07:31,000 --> 00:07:33,000
Just because you say component,
186
00:07:33,000 --> 00:07:36,000
the moment your Spring framework loads, it says,
187
00:07:36,000 --> 00:07:38,000
I mean Spring Boot here, it says,
188
00:07:38,000 --> 00:07:41,000
"Okay, I can see a component on top of alien.
189
00:07:41,000 --> 00:07:45,000
That means it is my responsibility to create this object."
190
00:07:45,000 --> 00:07:48,000
I mean you try out; create another class, let's say for
191
00:07:49,000 --> 00:07:52,000
a laptop, and then don't make component
192
00:07:52,000 --> 00:07:54,000
and try to say getBean; it'll not work,
193
00:07:54,000 --> 00:07:58,000
because only those objects will be created
194
00:07:58,000 --> 00:08:01,000
where you have component on top of your class
195
00:08:01,000 --> 00:08:02,000
and that's how basically your
196
00:08:02,000 --> 00:08:04,000
dependency injection is happening okay?
197
00:08:04,000 --> 00:08:07,000
So Spring is taking this object in the container
198
00:08:07,000 --> 00:08:09,000
and it is also getting injected here
199
00:08:09,000 --> 00:08:10,000
because we are saying getBean.
200
00:08:10,000 --> 00:08:13,000
Okay, now can I create this object multiple times?
201
00:08:13,000 --> 00:08:15,000
Let's see, let's see.
202
00:08:15,000 --> 00:08:20,000
I will say one Alien obj1 context.getBean
203
00:08:23,000 --> 00:08:25,000
and I will say alien.class.
204
00:08:26,000 --> 00:08:28,000
Let me say obj1.code.
205
00:08:28,000 --> 00:08:31,000
Now will this work? That's the first question.
206
00:08:31,000 --> 00:08:34,000
Let's run this and see what happens. Run.
207
00:08:34,000 --> 00:08:36,000
And you can see, yes,
208
00:08:36,000 --> 00:08:38,000
so coding is getting printed two times.
209
00:08:38,000 --> 00:08:41,000
That means this is working, right?
210
00:08:41,000 --> 00:08:44,000
But the question is, are we getting the same object
211
00:08:44,000 --> 00:08:46,000
or we getting different object?
212
00:08:46,000 --> 00:08:48,000
Okay, now that's tricky and I don't want to answer that now.
213
00:08:48,000 --> 00:08:51,000
Maybe we'll do that once we move towards
214
00:08:51,000 --> 00:08:53,000
understanding Spring framework.
215
00:08:53,000 --> 00:08:56,000
You know, sometimes surprises are fun,
216
00:08:56,000 --> 00:08:58,000
suspense is fun basically.
217
00:08:58,000 --> 00:09:00,000
And yeah, that's on this video.
218
00:09:00,000 --> 00:09:02,000
But then in the next video, let's also discuss
219
00:09:02,000 --> 00:09:03,000
what if you have a different object?
220
00:09:03,000 --> 00:09:06,000
What if alien is dependent on some other object?