Transcript for:
Spring Boot Autowiring

so now we know how exactly your spring boot helps you with the dependency injection but now let's add one more layer so there's a concept called Auto wiring and in this video let's try to focus on auto wiring with the help of one more layer Target for this video is 250 comments now which layer I'm talking about now just for example let's say when you talk about developers of course as a developer I need a machine to work with it can be a laptop desktop doesn't matter I just want a machine to work with so what I can do is I can work on the project but while doing that maybe I also want to uh compile the code so maybe I want to call a method called compile uh maybe I want to call the method uh debug so we got all these methods to work with and of course I can't do this in my mind we do that when you want to do a dry compilation but technically if you want to really build a project you need to have a machine where you can compile and debug so in order to call this methods I need a class where I can Define this this and that class is your laptop class so let me create a class here and time in I will just close this and let's create a new class so here I will create a class called a laptop okay and then in this laptop class basically I'll be having method like public void uh compile of course you need a compiler in your machine to compile compile something but let's say if I have a method called compile and it will say compiling with 4 4 bugs but now uh okay this is this can be a simple joke right where I say 44 bugs where you it's not found bugs not found maybe I tried maybe I failed anyway the point is uh I'm trying to print compiling with 44 bugs and this is what I want to print and now since I have a laptop class since I have a method now I can okay it still shows you a error okay it's there he is here so basically if I want to call this compile of course I need the object of laptop here so I can simply say laptop laptop I got the reference not the object and using that reference I can call this I can say laptop. compile let's say we don't have debug just to keep it simple let's go with single method of course you can have multiple methods so I got the laptop reference and I can call it now there is no compile time issue okay you can see there's no problem here but by default when you talk about the instance variables so laptop is a variable which is an instance variable by default it will get a value which is null and we don't want it right you'll get the same problem which we got earlier I want to show you that I want to show you the error which we got earlier so I will just run this and you will get waiting for the error okay so you can see we got null Point exception because is because the laptop is by default null uh let me just put this side by side so how do we connect this so one way is you can create the object here so you can say new uh laptop right and then this will will solve the problem is because now you don't have a null there you got the real object so if I run this it it says working on this awesome project also it says comparing with 44 bucks so our problem is solved basically it's able to call the laptop but then this is not what I want I don't want to say new laptop and even if you want to say new laptop there are multiple places you can do that we don't actually create the object when you define the instance you basically do that in the Constructor of this class of developer or in the setter of course this is a variable right so this should be private where you can create a Setter method for this and you can do that in the set up if you want to use the new laptop but anyway I don't want to use new laptop anywhere what I want is I want spring to create this object and then automatically connect it here so the first thing is what I will do is so I will talk to Spring framework by saying hey Spring framework I know you are creating the object for Dev I want you to create object for the laptop as well but it's not doing that you know why because we forgot to do one thing which is ADD component now when you add add component here now your spring framework knows or your spring boot knows hey Dev is not the only class we have to create the object it's also the laptop so now spring boot will create both the objects and that's not a big deal the big deal is how will you connect this to now the developer says I want laptop how will you connect this see one way you can do is you can use the application context remember in the main basically we had this application context and if you can get this in the developer or Dev class you can do that but I don't want to invoke application context I don't want to use that there so what you can do is you can use certain annotations one of The annotation we can use here or you can use one annotation which is called autowired now what this does is it says see as a developer or as a programmer here in this case not the programmer or in the example but someone like me I'm doing a code now I want to connect this to right so basically when you say connect that basically means wiring now since I want this wiring to be done automatically I can call this as Auto wiring and that's what the the anation is when you say autowired now your spring boot says Okay I want to get object of Dev but Dev is dependent on the laptop let me connect this to so behind the scene it will connect it and now you actually got the instance for the laptop so using this it will create the object but using Auto it will connect it and how do we check it of course to check it we have to run it so we just relaunch this application and you can see we got the same output without the new keyword so that's the beauty of Auto wiring okay and you can do that in multiple places not just here and basically when you do it here it is called a field injection so when you do that on the top it is field injection remember when we talked about dep injection we talked about three different types field injection Constructor injection set injection so when you do it here that's your field injection but let's say if you don't want to do that here what's the other option you can use a Constructor here so I can say Dev and in the Constructor you can pass the object of laptop and you can say laptop is equal to I mean this do laptop is equal to laptop if you're not sure what this line is uh go through the video of this keyword on ja on YouTube so search for this keyword in Java and you will know what I've did here okay so now this is how it does the Constructor injection you don't have to use Auto that which is optional so when you run this uh this also works so this is Constructor injection otherwise if you don't want to use construct injection see if you if you just uncheck if you just comment the auto and con injection if you try this you will get the same eror which we were getting before so those things are working okay uh let me try the seta injection so what I will do is I will just create a seta method which is public void set laptop and I will say laptop laptop this do laptop is equal to laptop okay so now I got the seter injection or setup method let's try if this works now when you do that it is still giving you an error so by default setup will not be going for auto wirring you have to say autowired here now when you mention autowire then it will behave like the way we want it will do the set injection we have not done for the Constructor you can do it it will not give you error but even if you skip that it is by default picking it so by for Constructor it is default it is optional but for for field injection and for seter injection you have to use Auto wire which is better uh Constructor and set is better field is not but you you will see me using that in the upcoming uh videos is because I want to keep this simple so that you will understand the project or you will understand how spring framework Works behind the scene I want to do one more thing here so what I will do is I will just go with the auto wiring Fu injection and let me remove or let me just commend this Constructor and SE let's only focus on the auto wiring here now question arise how exactly is connecting how do it how your spring framework knows that when you say autoare it will connect with this class object not some other classes because in your project you might be having multiple classes right so how it knows that we have to connect with laptop only so what happens is uh when you say Auto wiring it goes for by type it is not searching for name if you're thinking it's because of this name no it's not because of the name it is because of the type of the class so so since we are saying laptop it says laptop connected but the question is what if you try to apply loose coupling here what I'm trying to say is what if you create a interface out of this so I will say refactor extract the interface and let's code for the interface because see in this world there's nothing called computer it is either a laptop or desktop but both are called as computer okay do we got computer here or I don't know we just clicked on okay so we got computer here and you can see computer is not having any method so I will say void compile and this will be declared let me just put that down here I know there are multiple things open here but yeah we don't have a choice you can see on the left hand side we got Dev here we got computer and here we got laptop and laptop implements computer so basically what we are trying to do is we are according for the interface okay so here we got the interface which has method called compile and laptop implements computer now what's the advantage of this see one of the advantages when you talk about developers when you say you want to work on a project when you join a company your demand for MacBook right and that's the industry standards now everyone wants MacBook but you don't get it uh I'm still waiting for my MacBook but anyway the point is when you join a company they don't promise you that they will give you a laptop they promise you that they will give you a computer this can be a laptop or desktop and that's why a developer should not be dependent on the laptop this is hardcoring a developer should be dependent on the not on computer not on component on the computer and I will say comp here and let me replace this with comp so basically what I'm saying is as a developer you should focus on dependent on the computer not on the laptop and it depends upon the company what they want to provide you they want to provide you laptop or desktop that's their choice right now when you say computer will this work because see add component we are writing on top of laptop right but here we are looking for computer and as I mentioned before it goes by type so by type means it will search for the type of computer and laptop is a type of computer so if I relaunch it and you will see it it works so basically it is not compos that you should use only laptop you can also use computer and that's the good practice to use so we got computer here but with this I got one more thought what if now since it is searching for computer and you got laptop that's a good thing right but what if there's a confusion okay what confusion talking about let me copy this laptop class and paste it here of course it will we have to change the name of it so I will say desktop and now you can see I got a desktop class as well and so many windows open let me close computer now we know that computer is a interface which has only one method let me close it we got two classes here one is a desktop and one is a laptop and both are implementing computer and the the method also same compile and compile is because they're implementing computer here I will just write a different thing comparing with 400 44 bugs but faster and now uh you can see we got two different implementation right now my question to you we got two classes both are implementing computers so both are type of computer both have component on top of it so basically in your container you will have now three objects right so this is for the dev this is for the laptop and this is for the desktop a desktop here so basically I got three objects and now in the developer when you say Auto wire with computer Which object it will connect with laptop or desktop confusion right it's like you're joining a company and then they are giving you two options and you are confused because on the on the desktop you will get with the same amount of price desktop works faster plus it is connected to the high power source laptop is portable so you're confused there and that's what is happening with spring now so let me re rerun this and see what spring says okay so we have not got the eror but we got I mean we have we got the error not the red one but we got the error the error says application fail to start reason field comp this one is uh in this particular Dev class required a single bean I mean of course right we want only one object but two are found and this is what happens when you are confused between two things uh one is desktop and one is laptop which one should I pick for and it's basically confused how do we solve this one of the ways you can delete it at desktop class it will solve the problem or you can basically remove the add component on top of desktop so now what you're doing is your saying in the container you only got one object of laptop we don't have the object for desktop so there will not be any confusion there it will become up the laptop part but what if you have a component here and you don't have a component on top of laptop in this case it will pick up the uh desktop and you can say it says faster so this desktop but what if both has it now that's a problem right now in this case in case of confusion you can use certain annotation or one annotation so you know when you have two options sometime one one thing becomes your preference let's say for me I prefer desktops but maybe you prefer laptop so in this case on top of your laptop you can use one annotation called primary so what's the use of primary in case of confusion this class will be preferred and now when you say component on both but when you run it it will prefer the laptop you can see it doesn't say it's faster so that's laptop but what if you write primary on both not a good idea but let's say what let's see what happens when you say primary on both of both of them again there will be confusion but this time it is giving you different error but yeah it says the same thing it says more than one primary being found among the candidates so not a good idea but uh yeah this one one way you can put a primary on one class what if you don't want to mention primary what if you want to say component then you can decide in this level so you can also use something called a qualifier now in the qualifier you can mention the name of your class so you can mention laptop here okay not the name of the class but the name of the instance see by default this instance will have some name or we say be name so for the laptop it will be your class name but uh without the capital letter the first capital letter so if it is laptop it will be laptop but with small l so that's the name you have to pick up here okay so when you say qualifier in case of confusion it will pick up the laptop so now you you run this we got laptop you can also do do it for the desktop and it works so basically this is how you do auto wiring so in this video we have not just talked about the auto wiring we have talked about primary and qualifier and we have seen how to we code for the interfaces yeah lengthy video but I think worth it so I hope you got some idea behind the scene how Auto wiring is working see you in the next video