(gentle music)
-: Now let's try to implement dependency injection in Spring.
Now the thing is we started with Spring Boot,
but then the actual sense or what happens
behind the scene will make much more sense once we do this
using Spring framework.
Now, why we are doing in Spring Boot is
because to understand what Spring Boot can do.
In terms of dependency injection, of course Spring Boot
can do lot of different things,
but we are starting with the first step.
Okay, now when I say what we wanna do here,
before I do that, I have one more update.
So in the last video we talked about the same thing.
We ran this application but it was using Java 17.
So what I did is I have installed Java 21
and now we are going
to run all the application using Java 21.
Okay, so what dependency I'm talking about?
See this is our main code, right?
So we have a main application here.
In this if you can see,
what we are doing is we have a main method,
and then we are simply saying Springapplication.run.
Basically it activates your Spring framework.
It also has something called a container,
where you can create your Spring beans.
As I mentioned before, Spring is responsible
to create the objects
and these objects are basically called beans.
So any object which is created by Spring, so created,
managed by Spring will be called beans.
Nothing fancy, it's just normal object, different name.
Okay so this is how you run the container,
but we are not using container in this code.
We are simply running Hello World.
So nothing about containers till now,
but I want to use container now.
So what do is I want to create a simple class here.
Very simple class.
Let's say I want to say maybe alien, okay?
Now if you're not sure why I always create
or work with alien, basically I believe
that programmers are aliens, okay?
It's because we don't live in this world,
mentally or virtually, we live in virtual world, right?
So we are aliens anyway, nothing fancy.
Imagine every time I say alien you say programmer.
That's fine.
Okay so we got alien here
and in this alien, what I'll do is I want
to do a very simple thing, okay?
First of all, I have to deactivate my Github copilot.
It's giving some suggestion, okay?
So what I'll do now is I will create a very
simple method here.
I will say public void code because there's a programmer
or as as alien, we code.
And then what I'm going to do here is
I'm going to do a very simple thing.
I will simply print something
and let's say, I'll say coding, that's it.
I'm just saying code. So printIn Coding.
But now I want to call this from my main method.
So let me just keep this here.
And okay, so we got both the codes parallelly here
and now if you can see if I will remove this Hello World,
I want to create the object of alien
and we know how to do that, right?
We can simply create alien object here and alien object
and then I can simply call code.
That's the only way you can call this method
because this is a non-static method,
and if you want to call it, you need object.
And that's what we are doing here, we are saying alien obj,
got a new alien, we got the object created.
And then using the reference we are calling the method.
Let's see if this works.
I'll right click here, see run on this
and you will get the output.
Output says okay, it's taking some time,
but output says coding.
So at least this is working.
But now the idea is I don't want
to create this object by myself.
What I want is I want Spring to give me this object.
That means new alien is not something I want.
And that's tricky, right, because if you don't say
new alien, how it'll create the object
and now you are saying we don't want to create the object,
we want Spring to create it.
How Spring will know that you want to create the object?
Now that's funny, right? Okay.
For that, what you have to do is first of all,
if you say Spring creates the object, let's say
with this code, I'm assuming that Spring will create
the object for me because we are using Spring framework,
where exactly it'll create the object.
So it'll create the object inside the container.
Remember I mentioned about the containers?
So imagine you have a JVM, inside JVM,
you have a special area
or the special box which is running,
which is your container.
So every time your Spring framework creates the object
for you, it'll be available in the container.
That means I have to find a way to get to that container.
And the way you can do that is
by using application context, okay?
That means we have to get object for it now.
The thing is if you look at this particular method here,
which is Springapplication.run,
and if you go to run method,
and if you can see run returns the object
of configurable application context, okay?
This configurable application context
extends application context.
That means if you can simply use this run method return
value, you got the application context object.
Okay, let me just do that now.
So I will say application context,
and I will say this is name as context itself equal to.
So what we're doing is this run returns this object context,
which means now you have a way to communicate
with your IOC container.
Okay now how will you communicate?
So it's very simple.
You will simply say, hey context, that means hey container,
give me the object.
And remember, the object is not object in Spring.
The object is called bean. Okay?
So you have to say getBean. So you will say getBean.
And then here you mention which class object
you want, of course, right?
You have to mention because in your project you might be
having multiple classes.
So how will your Spring framework know what you want?
So mention, hey, you know, I want the object of alien,
which you have created inside your container.
Your Spring will say, okay, I got it.
You basically need this Alien.class object.
I mean you need object of this class,
but the IOC container will be able
to give you this object only if the object
is there in the container.
We are assuming it'll be there, but is it?
Let's try, let's rerun this
and let's see what it says.
Okay, your Spring is not happy, it's giving you some error.
It says no such bean definition. Can you see that?
We got the exception here.
And it says no qualifying bean of type
com.telusko.app.alien okay?
So basically it's not able to find this bean.
But you'll be saying then why we are using Spring
framework, if it cannot create the object for us?
Actually it can.
The only thing is see in your project,
let's say you have a hundred classes,
do you really want Spring to create all the object for you?
Of course not, right?
So there should be a way
that you should be able to tell Spring framework
which object you want and which you don't want.
So Spring by default says,
"I will not create any object for you.
Only when you mention that you want the object
or you want me to create the object,
then only I will create."
How will you tell your Spring framework you want the object?
And for that you have to use annotations.
So on top of it you will simply use annotation,
which is called component.
Now making this class as a component, you are making sure
that your Spring knows that Spring has
to manage this particular object.
So create the object, assemble the object, and manage it.
Everything will be done by Spring just by this annotation.
I know you don't believe me.
Let's see, let's run this and here we go.
Okay, so you can see we got the output coding.
Basically what is happening here is you have not
created any object.
Spring is doing it.
Just because you say component,
the moment your Spring framework loads, it says,
I mean Spring Boot here, it says,
"Okay, I can see a component on top of alien.
That means it is my responsibility to create this object."
I mean you try out; create another class, let's say for
a laptop, and then don't make component
and try to say getBean; it'll not work,
because only those objects will be created
where you have component on top of your class
and that's how basically your
dependency injection is happening okay?
So Spring is taking this object in the container
and it is also getting injected here
because we are saying getBean.
Okay, now can I create this object multiple times?
Let's see, let's see.
I will say one Alien obj1 context.getBean
and I will say alien.class.
Let me say obj1.code.
Now will this work? That's the first question.
Let's run this and see what happens. Run.
And you can see, yes,
so coding is getting printed two times.
That means this is working, right?
But the question is, are we getting the same object
or we getting different object?
Okay, now that's tricky and I don't want to answer that now.
Maybe we'll do that once we move towards
understanding Spring framework.
You know, sometimes surprises are fun,
suspense is fun basically.
And yeah, that's on this video.
But then in the next video, let's also discuss
what if you have a different object?
What if alien is dependent on some other object?