π Add to Chrome β Itβs Free - YouTube Summarizer
Category: N/A
No summary available.
00:00
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 dependenc injection we have talked about what is spring Boot and then we have also created a simple project with the help of the spring
00:15
initializer and we got the web project but we are not going to use this project basically we let's create a new project without the web and let's try to implement depense 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
00:34
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 uh the Java version
00:49
I have is 21 so 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 springboard options I will click on generate and you can see we got
01:04
the download and I've also unzipped it now it's time to open that in the IDE so we got this intellig 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 uh in this
01:19
window yeah okay so now you can see this is a default project nothing fancy there's no web part here uh if you go to the pom.xml we just got the spring boot stter nothing fancy a simple dependency right now what I want to do is let's
01:36
experiment with dependency injection and we have talked about depend injection before uh 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 uh so let's create a main code so we
01:52
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 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
02:08
it does is here so when you say Spring application. 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 uh project so let's say this is your project in this project you have lot of classes uh so
02:23
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 uh that's my job I will do it but question is where exactly spring
02:40
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 uh this is jvm and this is where you create all the objects now when you talk about spring spring itself has its
02:57
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
03:13
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 glasses you want the object and where exactly spring will create the object so spring is going to
03:30
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 classes 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
03:46
need is not the object but this container and this line here is responsible to uh create that container So when you say Spring application. Run it will simply run that container for you okay so we have the container ready right but then we want the object also
04:01
and for the objects we want class so what I will do is I will create a very simple class here and normally I call my participants my students as aliens uh is just that I believe that we don't live in this real world we live in virtual world not physically but virtually
04:17
because we create virtual Solutions right so we 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
04:33
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
04:50
our job or maybe instead of saying code I will say build that sounds much better right uh see difference between coding and building is when you code you you write some straight ments 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
05:06
print working on awesome project again just want to print it some 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 uh the question is how I'm going to call okay let me just put that
05:22
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
05:38
method and if it is a non-static method we have 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 is equal to new Dev right and then using this obj you will be calling build
05:56
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 you can see we got the statement which is working on the awesome project is it a good idea of course you it works and basically this is what we used to do when you don't use spring basically you
06:12
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
06:28
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 and how will you do that it's very simple now since this is a spring project I'm assuming that spring might
06:45
be creating the object behind the scene I just have to use it right maybe the object is already there maybe this is the object is already there why you have to get a new object for Dev uh so in that case what I will do is I will not be creating this object so will not say new DA because that's what creat the
07:02
object but if I remove it you know your compiler is giving you some bad words compiler says hey what you doing variable obj might not have been intialized okay so we have to iniz it and one way to do that is just to uh
07:17
fake your ID your compiler you can send null and your 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 era in the world uh and we love it no we just I'm just
07:32
kidding and the other is the null pointer exception we don't want that right so how do we do this null assigning n is not a good idea so we can get this object from the uh container now question how do you talk to The Container because now you are in this
07:48
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
08:05
container here so let's say the type of this ioc container is of type application context 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 for that
08:21
object there should be some type and the type is application context so what if you can simply get application context and you can see it came from this spring framework application context from Spring framework. context and we can use the reference of it I can say context
08:36
equal to 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 application context will work only when you create the object of it right but what if I can get the
08:52
object from Spring itself remember when we talked about this particular line at the start and I 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
09:08
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 Spring application. Class it basically Returns the object of configurable application context if I go here it Returns the object oh sorry it
09:25
extends the interface called application content text that means this run is returning you the object of application context that means we we already have the object so what you have to do is you just get this cut put it here and say
09:43
equal to so what we are doing is we are ass signing 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 you when I go forward I
09:58
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 I hope you have entered the answer so it's actually the get bean so in the get bean you have to mention which class object you want so I
10:16
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 the container I just have to use it and
10:31
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
10:47
of type com. telescope.
myapp dodev oh okay so we don't have this object in the container I was assuming that there's a there's a object but it's not there container is there for sure because this is what creates the container but inside
11:02
the container the object is not there and the entire video got wasted is because spring says I'm not going to create the object why is not creating the object now if you remember in the one of the topic or one of the video we have mentioned that Spring by default will not create object of all the classes and we don't even want it
11:18
because if spring creates object of all the classes and if you have 100 or thousand 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
11:34
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 or we can use Java based configuration uh we can actually use something called an annotation so on top of your class
11:49
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 class which I have to manage so this is a managed Bean what it means is spring will create the object for you in
12:06
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 and it worked
12:21
can you see that it says working on this awesome project so this is working right and that's how we get DEF injection so what we are doing is in this code we wanted the object of developer or Dev and spring is injecting that dependency
12:37
okay now we can go bit more layers example let's say uh 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
12:54
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 uh 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
13:12
work let's try to understand that in the next video but yeah I hope you got something from this video uh something about the deeny injection using spring boot where you are injecting a dependency in this particular section so that's it from this video uh I hope you
13:28
remember the target the target for this video is uh 200 comments again and see you in the next video bye-bye