Creating a Simple Spring Boot Project
Welcome back, aliens. My name is D Ready and in this video, let's try to implement dependency injection using Spring Boot. Now, till this point, we have talked about what is dependency injection, we have talked about what is Spring Boot, and then we have also created a simple project with the help of the Spring Initializer and we got the web project. But we are not going to use this project. Basically, let's create a new project without the web and let's try to implement dependency injection.
Okay, so what I'm going to do is first of all, let's create the simple project. For that, I'm going to go to start.spring.io and here let's create a new project. So I will click on Maven and I'm going to select Java here. The version is 3.2.5. Group ID is the Isco project name is my app. Of course, this can be anything, but that's in my app. The Java version I have is 21 and the jar packaging.
I'm not going to add any dependency because when you say you want to go for web, you have to add web dependency. But if you don't add any dependency, you will get a default Spring Boot options. I will click on generate and you can see we got the download and I've also unzipped it. Now it's time to open that in the IDE. So we got this IntelliJ IDEA Community version. I will simply open that project. So this is the project which I got just now and I will click on open in this window. Yeah, okay.
So now you can see this is a default project, nothing fancy. There's no web part here. If you go to the pom.xml, we just got the spring-boot-starter, nothing fancy, a simple dependency right now.
Understanding the Spring IoC Container
What I want to do is, let's experiment with dependency injection and we have talked about dependency injection before. Basically when you have dependencies in one class, normally what you do is you create the object of it. Now we want to inject it. How we do that?
So let's create a main code. So we already have a main file here. So you can see we have a class and it says My App Application, weird name, but that's fine for us. And then we are saying public void main, this is your main code which takes the string arguments. And here we are running a Spring application. So what it does is here, so when you say SpringApplication.run, it basically creates a container. Okay?
Now you'll be saying what is container here? See what happens is when you talk about your project, so let's say this is your project. In this project you have a lot of classes. So let's say you have one class, two class, three class, four class, and let's see you have multiple classes here. And now you are saying, hey Spring framework, it is your job to create object of this. Spring will say, okay, that's my job, I will do it. But question is, where exactly spring will do it? Of course, objects are created inside the JVM. So you'll be having this big box here of JVM. I mean, this is your project, this is JVM and this is where you create all the objects.
Now when you talk about Spring, Spring itself has its own container inside it. So Spring will have a container inside the JVM and they call this container as a IoC container. So this particular section here, this green box here is called the IoC container or you can say Spring container, that's fine. Now in this container, you create all the objects. So let's say from your project, you don't want object of all the classes. Let's say you got object for this, you got object for this. So these two classes you want the object. And where exactly spring will create the object? So spring is going to create the object inside this. So this is where it will create the object. So it depends upon how many objects you want and which class's object you want, but this will create the object inside this container.
That means when you want to run this application, the first thing you need is not the object, but this container. And this line here is responsible to create that container. So when you say SpringApplication.run, it will simply run that container for you. Okay, so we have the container ready, right? But then we want the object also. And for the objects, we want class.
Manual Object Creation vs. The Spring Way
So what I will do is I will create a very simple class here and normally I call my participants, my students as aliens. Is just that I believe that we don't live in this real world, we live in virtual world. Not physically, but virtually because we create virtual solutions, right? So we live in virtual world and that's why I called alien. And every time you see alien I'm writing, imagine that's a developer or maybe I can also say Dev, if that is not an issue. So let's say Dev. So let's say we have a Dev class here.
So in this world we all are objects, right? I know that's not a good thing to objectify people, but let's say in this example, every developer is an object. So I'm my object, you're the object, and we came from the same class. Let's a Dev class. And what is your job? Your job is to code. So public void code, and that's our job. Or maybe instead of saying code, I will say build, that sounds much better, right? See, difference between coding and building is when you code, you write some statements, but will it run, there's no idea. But when you say build, you are actually building something. And now here I'm going to print "working on awesome project." Again, just want to print something and that's why I'm doing this. And this is what I want to call. So I want to call this method build. Now from where?
Of course, the execution starts from the main. The question is how I'm going to call... okay, let me just put that in side by side so that you can see it. So this is the method I want to call. So this build method I want to call from here. Of course, the way you can do that is you can go here and you can call build. Will this work? Of course not. We know in Java if you want to call a method and if it is a non-static method, we need object of it. So that means to call build, we have to create object of Dev. So what you will do, you will just come back here and you say Dev obj = new Dev();, right? And then using this obj, you will be calling build. And that's how thing works, right? So basically you call this and you run this example. If I run, if I click on run, it will create the object of Dev and you can see we got the statement which is working on the awesome project.
Is it a good idea? Of course, it works and basically this is what we used to do when you don't use Spring. Basically, you create the object by yourself. But the idea behind Spring framework is you don't have to manage the objects. Spring will do it for you because when you say new Dev here, what you're doing is you are manually creating an object inside the JVM, but not in the container. That means when you create this object, it is your responsibility to manage the entire cycle of it. We don't want to do that. We don't want to create this object by ourselves. We want Spring to create it.
Fetching a Bean from ApplicationContext
And how will you do that? It's very simple. Now, since this is a Spring project, I'm assuming that Spring might be creating the object behind the scene. I just have to use it, right? Maybe the object is already there. Maybe this object is already there. Why do you have to get a new object for Dev? So in that case, what I will do is I will not be creating this object, so I will not say new Dev() because that's what creates the object. But if I remove it, you know your compiler is giving you some bad words. Compiler says, hey, what you're doing? Variable obj might not have been initialized.
Okay, so we have to initialize it and one way to do that is just to fake your ID, your compiler. You can send null and your ID is now very happy, at least you have assigned something. But of course when you run this, you will get one of the most famous errors in the world, and we love it. No, I'm just kidding. And the error is the null pointer exception. We don't want that, right? So how do we do this? Null assigning null is not a good idea.
So we can get this object from the container. Now question, how do you talk to the container? Because now you are in this main code, container is there with the JVM. How will you talk to the container? We want to get a hold on it. How will you get the hold on it? Maybe we can get a reference of it, right? So if you can get a reference of the container, you're good to go. So basically, the type of this container here, so let's say the type of this IoC container is of type ApplicationContext. So the type of this particular object, of course, right, this is object, even the container inside your JVM, which is your IoC container itself is an object, right? So for that object, there should be some type and the type is ApplicationContext. So what if you can simply get ApplicationContext and you can see it came from this Spring framework ApplicationContext from springframework.context. And we can use the reference of it. I can say context =. Okay, we have to create the object for this now this is weird right because ultimately we are saying we don't want to create the object but now it says we have to create the object. See not exactly, see ApplicationContext will work only when you create the object of it right?
But what if I can get the object from Spring itself? Remember when we talked about this particular line at the start and I told you that this particular line creates the container for you? It does. Example, if I click on this run here, you can see this run method returns the object. So I just went to the source code of it. Basically I've decompiled the file with the help of IDE, and this run method which we are calling of SpringApplication.class, it basically returns the object of ConfigurableApplicationContext. If I go here, it returns the object... oh, sorry, it extends the interface called ApplicationContext. That means this run is returning you the object of ApplicationContext. That means we already have the object. So what you have to do is you just get this, cut, put it here and say equal to. So what we are doing is we are assigning this object which returns from run to the context. Once that is done, I can simply use the context and say... so context has multiple methods here. And you tell me, looking at this in the comments, before I go forward, I will take a pause. Which method you are going to use to get that object? Pause the video, let me know in the comments. Okay, so I hope you have entered the answer. So it's actually the getBean. In the getBean, you have to mention which class object you want. So I want the object of Alien class... that's it. Sorry, not Alien, we are not going for Alien anymore, we are going for Dev. Okay, so you can see we are saying that I want the object of Dev. Who has created the object? It is Spring. That's what I'm assuming, that the object is there in the container. I just have to use it and I'm doing the coding for that.
So now, this will give me the object which is existing. I'm assuming that it is there and I'm fetching it. Let's see if that works. I will run this and if you see, we got an error. It says No qualifying bean of type com.telescope.myapp.Dev. Okay, so we don't have this object in the container. I was assuming that there's an object, but it's not there. Container is there for sure because this is what creates the container, but inside the container, the object is not there.
Telling Spring to Manage a Bean with @Component
And the entire video got wasted is because spring says, I'm not going to create the object. Why is it not creating the object? Now, if you remember in one of the topics or one of the videos, we have mentioned that Spring by default will not create object of all the classes, and we don't even want it. Because if Spring creates object of all the classes and if you have 100 or 1000 classes, we don't want the JVM to be burdened with all these objects, which we're not going to even use it.
And that's why Spring says, "I'm not going to create the object by default. You tell me which class objects you want and whatever you say, I will create the object." And the question is, how will you talk to Spring framework? Maybe you need a config class or in the Spring Boot, we can use Java-based configuration. We can actually use something called an annotation. So on top of your class, whichever class object you want, just say this class is a component. Just by mentioning this annotation here, your Spring understands that this is the class which I have to manage. So this is a managed bean. What it means is spring will create the object for you in the container. So the moment you say component, Spring says, "Now I know what's my job. My job is to create the object, I will do it for you." And now let's see just by adding that component annotation, is it working? Let's relaunch the code, relaunch this application.
Verifying Dependency Injection and Next Steps
And it worked. Can you see that? It says "working on this awesome project". So this is working right? And that's how we get dependency injection. So what we are doing is in this code we wanted the object of developer or Dev and Spring is injecting that dependency.
Okay, now we can go a bit more layers. Example, let's say we got Dev and then Dev needs an object of a laptop. Of course as a programmer or as a developer, you want to work on a laptop and you don't have a laptop here. You're just saying working on a project, but how? In the air? Or maybe Oculus device or maybe Apple Vision Pro? Doesn't matter. You need something to work on, right? And we don't have it. So let's say in the next video, let's try to create one more layer. Because in this, we we got two layers, right? We got two classes. So main needs object of Dev. It is working. But what if Dev needs object of laptop? How that will work? Let's try to understand that in the next video. But yeah, I hope you got something from this video, something about the dependency injection using Spring Boot where you are injecting a dependency in this particular section.
So that's it from this video. I hope you remember the target. The target for this video is 200 comments again. And see you in the next video. Bye-bye.