Why autowiring in spring | XML | @Autowired | @qualifier | spring annotation tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so this video is gonna be the most important video of this series well I keep saying this thing in all the videos you are going to hate me for this but well this video is going to be really important because here we are going to understand one of the most important concept of spring called Auto wearing right so Auto wearing how it works in XML what about the Auto add annotation there are so many questions right so that's that's the question that you keep on asking me in each of my spring videos and also I have got a lot of emails from you guys you know saying that you know when you are going to publish the Auto wearing video so this is the video for you guys and we are going to do a whole lot of things today so we are going to learn how to configure auto auto wearing in the spring XML file also we are going to understand that how to configure you know Auto wedding in the Java side by using the auto word annotation and also we're going to understand the earth qualifier annotation in this particular video so in this video we're going to use the little bit of annotation kind of stuff as well and we are going to get started with annotation in spring right so you know there are a lot of good things in the store hopefully you guys are excited and I am really really excited for this particular video and I hope that it is going to help you guys a lot right so this video you are done means your life will be 100% easy trust me this time right this is the important video and this video you are done that means you are almost good in spring court framework right nobody can stop you so here we go so let's go for the tutorial then [Music] you alright guys so right now I hope you guys can see my screen here and if you are then what to wait for we can get started with our coding right so I do have a project here called spring auto wiring qualifier so this is the project name that I've given over here and you know as always I do have a package here and I do have a pin start XML file that I have copied from my previous project so in this time you know let me rename this particular package name to come dot selenium Express dot ortho where and hit OK and there you go right so I do have a phrase package over here and right now we are going to create few classes and we're going to deal with the auto wedding stuff but before that let's make our basic stuff ready right I'm going to create a very phrase example over here and that example that I'm going to follow you know to out this entire video right so anybody is watching this video who haven't completed my earlier stuff like IRC like setter injection constructor injection and dependency kind of stuff then this video is going to be difficult let me tell you auto wearing is very easy if you understood those thing previously right so so if you haven't watch them then please go and watch that first and come back and watch this video right it is going to be very easy if you understood them but I know you have completed that and let's go ahead right I don't want to talk this things which doesn't make any sense right so let me create a class here let's say my class name is human right so let's do some fun stuff today okay so I have a class called human here and let's say this human does have a dependency and the dependency name is hot right so I'll write private in hot hot right so this a human class does have a dependency called hot and okay so right now Eclipse is complaining right he's saying okay you don't have a class called hot right so I'll say eclipse okay you create one for me so I I just do what create class hot and I'll go ahead and finish this thing okay so I okay so right now I do have a class here called hot and here in this hard class I just have a simple method as usual public forehead pump because hot is going to pump right if it is pumping then I can say this out and I'll say pumping and let's say your heart is pumping right and I'll put another sit here just how it and I'll say life okay just having some fun here okay so right now this is my heart class and this heart is a dependency for the human right okay so right now how to inject the value for this heart right so for this dependency I'm going to insert the object to that by using a setter injection so I'll go to source I'll create a generate getters and setters and I want only the setters and I'll hit okay right so right now I do have a setter here and I'll write a simple method public void let's say start pumping start pumping oops pumping okay what happens to my typing skill okay it's a right now what I'm going to do I'm going to call the hot class pump method using the hot reference right so this is the reference that I do have over here and this reference will get the value by using a setter injection right see the spring is going to do the injection by using the setter method right so let's configure our bin start XML file so if I'll go to Binstead XML file here I do have a very simple Binstead XML file that I have copied from my last project and this looks very familiar to you right now I know so right now first of all how many objects we are dealing dealing with over here one is human object for this we need to create the object and the second object is inside the human there is a another being there is an inner beam that this human depends upon that is hot so two object one is human object one is hot upset so let's go to go to the bin store XML file and let's configure our object first of all I need to create a beam and this beam ID I'll give as hot because you know let's say human okay this this is the human object and for the human the class name is what I'll go to the human class and I will copy the qualified name over here and there you go and if I go to bin start XML file I'll do a ctrl V in that's it okay so my I have configured the beam for human being mean object as usual right so right now this human has another dependency so right now let's set the dependency for the human so that whenever spring will create the object for human it'll also inject the dependency right so right now I have to set the dependency for the human okay so if I'll not set it okay if I not set it so then then try right now okay so I'll go to the my auto where project I'm going to create a new class over here and let's say my class name is buddy okay and here let us say in the body class I have a main method so right main control space and there you go I have a man method right now and right now what I want to do first of all I need to you know read my XML file so I'll do what I'll do application context application context context equal to plus path XML application context and here I need to provide my beans dot XML file the file from where I'm reading everything so this is my Binsted XML file and as you can see this is the file name and I am providing this particular file name in my body Java right and there you go and after this okay I skipped the new keyword here gnu classpath application context and there you go and after this I do a context dot get bean and I want to get the bean for what my bean name is what my bean name is human I'm going to copy this I'm going to paste it over here and this human being belongs to his class human class right so human gut class right so right now this gate beam method is going to give me what the human class object right because this is the class file that is him under class obviously it is going to give me the human class object right so human human and there you go right simple we are going so quick right now but I know you will not have any problem so right now I'm going to call the human class which method human class which method human class start pumping method if I call this particular method I can expect that this pump method will be get called I came into the heart class pump method and this pump method should execute and this particular value will be you know displayed in the console right so let's see that is happening or not right so let me go back and let me go back to my body dot Java and I need to call this particular method called start pumping I already got the human class object so I'll do human dot start pumping okay there you go let's run it and you should get an exception yeah there you go nor pointer exception because right now this particular you know object does not have any value this reference does not have a object because we haven't configured it yet so just to make some make it more fun what I'm going to do I'm going to execute this particular method when the hot object is not equal to null at that time only execute this particular method okay otherwise don't execute otherwise else let's say since out let's say you are dead oops so scary okay so right now you know what I'm going to do if I go to my body to Java class if I'm going to run this particular application okay you are dead okay so right now our job is to inject the object for this if we are going to inject the hot object right if we are going to implement the heart we are programmers we're engineers let's be a doctor and implement the hot object to this hot variable right then we're going to be alive so okay so right now let's go to bean start XML file and I'm going to configure the dependency over here mark over here what I am doing right because it's very important so right now this is there is a property okay why I am writing property because this is a property here and I'm going to inject the property by using the setter injection so I'm going this is my property I'm going to come and see it I'm going to go to beans dot XML file I'm going to press the property here and I'm going to give it a ref right so ref equal to so I'll define it another bin here because because this heart is what this heart is a class this heart is a object that we we are going to create for the class right and how we create the object in spring we clear the object in spring by using the bin tag right so let's create another object beam ID equal to let's say I'd equal to heart and in the class is equal to I need the fully qualified name of the heart class I'm going to come on click over here I'll go to the heart class I'll do a right click copy qualified name come to the bin start xml-based a fully qualified name of the in a hot class right there we go so this is my ID I'm going to come and say it if you are getting confused if you are getting confused let's say heart heart object right there simple so right now this is the object for my heart so this is the class and for this particular object this is the reference something like hot hot object equal to new heart and right now I am going to pass this heart object through refa tribute right very simple if you understand the last tutorial this will be nothing right so there you go so can i end the property by clicking this here yes right so I just given a slice over here and that is gone so this is how we are setting the setting the dependency right now right so our human object whenever you are creating the human object there is a dependency called Hawk inject that object as well and we explicitly you know configuring this property over here we have done it and we have manually configured this Hart dependency over here inside this human being very simple right so now spring when it is going to create the human object it is going to see okay there is a dependency called Hart and for this dependency it is going to inject this particular object this object belongs to what the heart class and for the heart object only this is going to inject the variable heart so this will get the heart of zipped and your program will run fine right so let's let's do that if I'll go to body to Java right now and if I am going to run it so let's say oh your heart is pumping alive congratulation so we have successfully injected the heart object okay we have successfully injected the heart object to is respective variable right and we have done it by using the dependency injection feature of spring by setting the property over here heart and giving it a reference it is as simple as that and you might be questioning me right now hey we lodged why you are saying me this again you already told me this yeah there there there the fun part come because from now you don't need to set the property explicitly spring is going to do it for you so what I mean by that okay so let us understand in this way right so right now I do have my property configured over here this is my dependency for the human being for the human class for the human object there is a dependency called heart and this particular dependency I have configured it over here right now I'm going to remove the dependency if I'm going to remove the dependency if I'll do control s right now this human object does not configure the dependency so right now for this human object the dependency is has not been configured so if the dependency has not been configured then spring is not going to insert the value for the heart by default it will be null and if I'll be running this particular program okay you are dead right because the heart objected it will not get so right now what I will do I'm going to tell you an exciting feature of spring okay and the feature name is what the feature name is auto wearing okay so here you are explicitly here you are explicitly doing this thing right you are even explicitly you know now putting this property in you are configuring the property but how about if I will not do the configuration by myself so I will remove it and I will tell spring that hey spring you check if my human java class sorry if my human class has any property has any dependency you automatically wear it up you automatically inject the object for that I don't need to manually configure the thing over here by doing like this thing right so how to do that right now I am removing it once again okay right now my human dirt for my human class there is no property configured right so I'll do what I'll use a spring feature called ortho wearing right Auto wear and as you can say by default it is it is set to no right there are a few property if you do a control spicier then you can see no default constructor by type by name know by default it is set to no and default is of no use I'll be talking about that later first of all let's talk about the bind name property I'll go with the by name so if I you know change the auto where to buy a name right now what is going to happen so if I am going to my body dot Java class right and if I am going to run it do you think do you think this program is going to run and our hot object will be incepted see here we have the hot object and here we are configuring our human object for this human object we are telling spring that hey spring if you find any of the dependency here in the human class like cut you don't wait for me to configure it you only inject the object to this particular reference right you only give it object to this like new heart right so how can we do that if this thing is going to work if I if I'm going to budget or Java if I'm going to run it let's see what is going to happen okay you are dead that means still it is not injecting but guys you know there is nothing wrong in this particular program you are almost done right so let's inject the value again unless he was going wrong over here the only thing you need to understand that when you are going to go with the auto whereby name approach you have to take care of only one thing if it is by name go to your human dot Java class so in the line number five this is your dependency right what is the name of your dependency what is the variable name what is the reference variable name the name of the variable is hot right so you need to do what you need to come to your bin start xml file give the ID name is hot right instead of hot object i will just give the ID name is same as this one right I'll do what I'll do copy it copy and I'll go to my bin start xml file and instead of the hot object i'll give the ID name as control-v hot right there you go nothing else right now only thing I did is the bean name the bean name over here what is the bean name bin name is hot or the ID what you are saying the name of the ID is hot and the reference name what is the reference variable name hot this and this only I have messed the name right right now let's go to body dot Java and right now let's run this particular program unless see what's happening and you can see the result your heart is pumping a lie so right now you have injected you have injected the object to this particular variable right like you have injected the object to this by something like this new heart right who made a heart implementation right you have done this thing right by using spring and by not configuring the property over here inside this human class but the human class has a dependency but we are not setting the dependency explicitly by putting the property tak like we used to do it like property right name equal to XYZ then the then the refuel to some reference value right this thing we are not doing it and we are not going to do it at all after this because we got this Auto wearing future spring is going to automatically check ok this human class does have a dependency called heart and whenever spring is going to create the object for human class bean means object whenever spring is going to create the object for human class at that time it is going to see this come on hey do the auto wearing by name so spring is going to check ok so there is a dependency called heart inside the human class and this heart I need to match with any of the bean present inside this bin start xml file whose ID is also hot so there is a beam configured over here whose ID is also hot right so it is going to create the object for this and for this this object it is going to tack and it is going to inject that particular object to here right so you know is something like this you know the same thing only it is going to create the object for this you know what we can say heart heart equal to new heart and by using the setter injection by using the setter injection it is going to inject this particular object to this okay whenever it is creating the human class subject so to make sure this hetero method is getting called I can do is this out over here I'll say setter method call right okay if I go to budget or Java let's check this out there you go setter method called so how spring is going to are going to you know inject your object it is injecting your object by using the setter method only right but the thing to noted over here is we are not you know doing property name you know rare fickle to something we're not doing that stuff it it is as simple as that you're getting it all okay going good you understand how I did it for by name right the ID name and the reference variable name both need to stand all right good let's go to the next concern [Music] okay so right now let's go for the next concept and this concept is a bit more simpler than the last thing if you understand that then this one will be nothing for you guys so let's say right now guys as I said if this and the idea of the beam the name of the beam this it is going to match at that time only the you know wearing is happening because we have could we have written over here photo whereby name right but what about if I change something to hot object right this is a hot object value right now what will happen now let's take the the variable name and the ID is this matching no it's not missing if it is not missing what we what the command you have given over here for the auto wearing you are saying his spring whenever you are finding the ID name and the variable name they both are same at that time will you do the injection but at that time the ID name and this name is not same so if I going to burrito Java and if I'm going to run this particular program obviously the object will not be injected to your heart dependency right to your heart dependency at that time if you have a scenario like this okay you cannot change the ID okay your birth said hey the ida nadi name should be that only okay but you have to do the auto very so how you are going to do it so at that time that is a another approach see here the heart variable name this is of what type this is of what type the type is hot type means class thing like this this is of what type hot type right the heart is of hot type this is the variable name this variable name is the type of heart hot is their class right so since the class and also in the bin start XML you are also configuring the class right so this class is apart heart class right for heart class only you are configuring the beam over here right this beam belongs to which class hot class right so here class secure too hot so this is heart and the dependency that you have inside the human the name of the variable is heart and the type is heart so this and this if this is matching right you can do by type so hey spring create the object and you know do the dependency whenever the auto where is equal to by type right by type means the class name and the class name right this need to match the type need to match right now if I go to body dot Java and if I'm going to run this particular program 100% be sure that you are going to be a life I'm sorry I am going to be a live let's say this guy is me okay this human is me I shouldn't be saying that okay so there we go right and how this you know thing is happening how spring is injecting your object by using the setter injection only by using this by using this set heart method right so you can see the setter method is getting called so very simple whatever the thing used to do manually that the same thing spring is doing it is creating the object for human human human equal to new human it is creating the object for heart heart heart object value equal to new heart and then it is going to use the setter injection to inject the value for this hot object so it is going to do what human dot this particular setter method okay and it is going to pass the object of this right not it [Music] the audit by type means the type net to match by name means the name net to match the ID name and the variable name and here the variable type and the class type that you are defining in your bin start XML right very simple right and this is up to type to type we understood right by a name and by type so what about by constructor there is another one over here called by constructor the name says it all you already guessed it I know and in this time you are saying hey spring do the dependency injection automatically when you find a constructor for the dependency okay so right now if I'm going to save this and if I'm going to run this body to Java will this particular program is going to run let's check this out no you are dead why it is happening because you are saying that hey use a constructor to do the dependency injection so right now is there a constructor present here inside the human class no there is no constructor present right so let me let me do what let me resize my court command a control shift F okay so there is no constructor right now present over here right only the setter is present over here so right now we need to create a constructor for this particular you know dependency to inject right so what I can do over here right now I can create a constructor I am going to do a right-click go to what is that source then generate constructor using field and I'll hit OK there you go so I got a constructor over here let me remove this so you know about constructor the class name and the constructor name is always same so here it is my constructor let me clear up some white spaces here and right now I think you can see everything in my screen right commands it F okay I think it is all already ready and there you go okay simple right so right now what I am going to do if now I have a constructor here for the human class and this particular constructor does the same thing like the setter right it is the setting the hot object to this so right now if I loaded my bean store XML file the Ottawa type I would define its constructor so hey spring so whenever you find the constructor for the dependency do the injection right so I do have the constructor over here for the human class which is taking the heart plus object as the parameter and setting that to this right very simple so right now let's let me go to body dot Java and let me run it and let's see what is happening and there you go you can see your heart is pumping and a lie that means the object is automatically get injected to your reference by spring and all thanks to you know the auto wear concept of spring it is making your day useful right and more productive so guys you know these are the three different thing about the auto wearing concept right and if you are clear on this in the next thing that I am going to tell you this will be just like a joke for you right it will be very very simple but before you go ahead something I want to tell you guys see you know think like if I if my human class has so many dependency right let's say 20 dependency and if I'm using the auto wear concept over here this fine spring is going to do all the things for me but wait if I want to understand the application right if I have a defect if I have a problem so for debugging purpose it will be very very difficult because I cannot see anything any any configuration I do not have over here so if you have more number of Bin's like 100 means 200 means or more than that then do configure everything manually because that is going to make your life easy but if you have very less amount of you know dependency present inside a class just like 1 or 2 or 5 or 10 then you can use this Auto wear approach because you don't need to configure anything explicitly here and spring is going to do all the injection for you guys free of cost and spring is very polite right it is doing all the things for us but don't miss utilize it otherwise it would be a problem for you guys right in a real-time application so that's it that's all about the auto word concept and that's how you need to configure it in the XML right you there you go so right now let's talk about what let's talk about the eight Auto well annotation if you understand this that thing will be nothing for you right so what I'm going to do the annotation is going to work same as though this particular thing inside the XML it is also can do it by name by type by constructor stuff we can do this in the Java side in the Java side only we are going to use the annotation we are not going to write like this I'm going to remove this auto where equal to constructor over here still I want to do the injection right so how can I do it I'll just go to the human dot Java class and before this particular constructor I'll do what it ought oh we're right I'll make this a toe toe where and that's it got it so this is it your program is done right you don't need to do it over there in the pin start XML file I remove this thing okay and this thing this whatever stuff I configured in the Java side I went to the constructor and before the constructor I have written pet Auto Wed right so that's it you are done but if I run this particular program the program is done you don't need to think anything else right right now let's see our application is running or there is any problem right so let's run it oh now you are getting a lot of exception sorry I am getting a lot of exception let's check this out so if I open this console here then you can see it is saying error creating bean named human defined in the class path resource and it is saying instantiation being felled nested exception this benben instantiation exception it is saying no or default constructor found why we got this exception if you know core java now guys it should be very easy okay so it is saying that human class human object it cannot create because it does not have a default constructor why because forget about this auto word concept right now because I'll tell you what is the problem right but let's first understand why we got that particular error you you so in the bin saud xml file whenever you know you are creating this particular object right whenever you are creating the human object okay and it is if it is looking for a default constructor first of all suppose it is trying to create object like human human equal to new human at that time it need a default constructor right new human that new whom that human constructor it need the default constructor right so do we have a default constructor present over here inside the human class check over here we we have not created any default constructor over here we have one parameter constructor present over here which takes a hot object but we do not have a default constructor do you remember the default constructor if you're not going to write the constructor the default constructor will be created by the JVM but if you are overriding the constructor over here 34 constructor JVM will not create it for you hey if you are creating or if you are overriding the constructor means your job only you have to create the default constructor so let me create the default constructor over here human constructor and there you go I got a default constructor over here right now okay so now let's say now let's run this particular program and let's see is it running or not right now it is saying you are dead you right now now we are coming back to the you know actual task here so now it's saying you are dead but I said guys the program is correct right this is how only you need to do the auto word configuration whenever you are talking about at Auto add annotation in the Java side we need to use this at Auto add annotation that's it but why it is not working because guys as I said by default the Jota where is up we need to make it on to make it on we need to go to the insert XML file okay so how to make it on to make it on I will use a another element called context rice a context controls face but wait nothing is coming right context under context element is also not coming why it is not coming whenever I am typing here being bean is coming right but why context is not coming if I if it is not coming how I'm going to use it it is not coming because here we have not defined the namespace for the context like we have defined the namespace for bean bean is coming being a beam element you are coming like that you have to define the namespace for context right so again I need to go to my spring jars and the spring you know that installation file that I have downloaded I need to go to that okay okay so here I am inside my spring Czar's that I have downloaded from the spring site and I'll go to les Al Gore to Docs I'll go to spring framework references I'll go to html5 and I'll click on cor dot HTML and from here only I am going to copy those stuff right because I'm going to be a little lazy over here okay I'm going to do ctrl F and I was okay I have already started and I'll search for slash context I'll hit enter here and there we go so this is for the context I am going to copy all this thing right up to beam I am going to copy this I'm going to go back to my Eclipse and I'm going to replace this with the new one that I have copied and there we go right so right now I can see here I do have context namespace here right I have an M space for beans and as well as the context right the right now let's say context now the context is coming right context a notation config and that's it nothing else context a notation current pick open it and close it if you want you can do like this classier that's that's done right now this is opponent closed this done now your annotation is activated now you go if you go to body dot Java and if you are going to run this particular program let's see it is working or not there you go your heart is pumping alive congratulation you have done it by using the auto word annotation if I remove it if I remove it no control is come here run it oops you are dead right so before the setter inject sorry before the constructor only you I'm putting the editor word so my constructor is getting called this constructor is getting call tries this out I'll say human constructor he is getting called which his heart is argument okay so if I go to buddy if I'm going to run this again then you are going to get that in the console to make sure that that particular constructor is getting executed and your auto wearing is happening by the constructor right as simple as that isn't it okay so now let's do it for the setter okay let's put the 808 annotation before the setter and let's see what is happening alright guys so as of now we have done with the auto wait by constructor in annotation approach right now let's do with the auto word by name or by type forget about that I'm going to use the editor annotation before the setter method and let's see what is going to happen so if you come to the Binford XML file just to make sure I haven't said the dependency for our human class right so human class is one dependency and their dependency I am NOT setting in the bin start XML file so what I'm going to do I'm going to go to the setter method for this particular dependency where the setter method is present for this particular dependency here it is so I'm going to move this editor head constructor from here to here I'm going to do a ctrl s and I'm going to run this application what is going to happen do you think it is going to run let's check this out there you go ok it was running fine right setter method is called your heart is pumping and you were like setter method is called make you sure that this is actually using this particular set heart method to inject the hot object to the actual hot object which is this dot heart and this dot heart is what this one right clear so this is how we are using at Auto where before the set setter method but right now here is a puzzle right and I am NOT going to make you puzzle around this because this is very simple don't be confuse it will be very simple I shouldn't be saying this word don't be confused because when we say don't be confused you start being confused for nothing right so this is very simple guys again it is kind of a tenth standard thing right so if you talk about it afterward right now this I taught over I am using before the cetera right before the setter method so right now I'll ask you a question what do you think what approach this is for lowing to inject the hot object to this is it following the by type or the by name approach we know about the by type in the by name approach it off now right we know that by name means the variable name and the ID name of the beam need to match right and by typing the type of the variable the type of the reference that means the class name or the interface whatever the type of that particular each see over here the type of this is hot and if we have been called hot configure there in our pin stirred XML file at that time you know it is going to inject the dependency but right now what is happening first of all here I do have a different ID that means this ID and this ID is not matching that means byte by name is not happening right because it is not matching but this class okay the heart heart and this heart this is matching the classes the type is matching right if the type is matching by by what approach this is happening by type right it is very simple in the setter injection right now when we are putting the edit or annotation this add auto add annotation first look for the first try to resolve the thing by using the by type and then it is going to go for the by name first of all over here what is the type of this heart I mean the type of the heart is hot right this is the class actually hot now is there a being already configured in the Binstead XML file which type is heart yes there is a type already configured where the class name is hot then there is a match found no need to go for by name by type is matching if the by type is missing means spring is going to use the by type approach over here very simple right so if I go to body tour Java and if I'm going to run it is it working I mean this is working you know as usual okay so all good but right now you might question me write that here we last I understand that this editor word annotation first look for first try to resolve the thing by using the by type approach right if the by type is felling then it is going to work for them I mean resolve the thing by using the by name approach can you show me an example where where is actually using the by name we're going to do that right now so right now this thing is working as by type because this heart and this heart is matching right so now let's tell the system okay what I'm going to do I'm going to I'm going to end the beam over here no problem with that and I'm going to create a new beam I'm going to create a new object for the same class heart again and this time let's say this is human heart and this is octopus heart octopus for us octopus heart okay right now how many hot class object I do have here right now I do have two object for the same class right this is hot this is also hot something like heart human heart equal to new hot hot octopus heart equal to new heart to heart class object I have right hot class object been configured for two times over here so right now if I come to human dot Java I'm giving the at auto air annotation first spring is going to redraw the thing by using the by type right so this comes to the Binstead XML file okay so what is the type of this particular variable the type is hot right the type is over here hot right so it is going to show the same heart over there in the Binstead XML file okay is there a beam configured over here we says the heart class well there is one but wait there isn't another one there are two different beam for the same heart class so I cannot resolve it which one to use because they're at they're two different object one is human heart one is octopus heart so which one to use spring got confused right now spring is going to do what spring says okay so by type is felling but I do have another one right I have a resort solution for this and I'm going to use the by name approach right now so what is your variable name your variable name is hot right your variable name is hot I can say right for this only yours are you now creating the setter right so your variable name is hot so this heart I mean this heart it is going to match with the ID name so is there a ID present here called heart no it is not here it is not here as well now this is also selling by type felt because there are two different objects for the hot class right by name paired because there is no name matching with heart over here in the bean ID right so this is also selling right now by type also paired my name also felt right now if I am going to you if I am going to run this body dot Java class then you can see it is giving me a lot of exception it is saying what let's try to understand right now it is saying what it is saying that error creating the bean name with human unsatisfied dependency expressed to set heart parameter 0 let's talk about the actual exception no unique bean definition exception now here we go no qualifying bean for type you know combat selenium expression should auto head at heart for this hard class there is no qualified being available now see the actual exception expected a single matching bean expected a single matching bean but found - ok where it is expected a single messing bean but found two so it is actually expecting one bean one objective present but there is two one is the human heart one is octopus heart there is two been available there is two object available in a bean store XML file so it is felling that's what spring is all has to say about this particular exception so right now it is our duty to handle it so our by name is also felling by type is also felling so first let us resolve this particular problem if I am going to go to human dot Java I am going to copy this name and I'll go to bins dot XML file and for this human heart if I'm going to paste it over here if I'm going to give the ID name and this one is same right now I am going to body dot Java right now if I'm going to run this this is going to work because it is by by name is happening right by type is failing because there are two different object that we are configuring over here in the pin start XML file heart and offset octopus heart so first of all whenever this Ottawa is looking for the by type it is finding two different implementation over here so this is felling then again detecting for by name wait this name and this name is matching this name in this name is matching right in the setter also it you can see the name is messing right this is hot and that is also the bin name is also hot okay then the mesh found my type felt but by name is happening right so it is going to pick this one okay and going to inject this particular object to here right and right now if I'm going to run this particular application let's see what is happening setter method your heart is pumping you are alive you got the point by name and by type all good right you're clear as of now okay so we have understood a lot of concept today right this is the only last thing which is spending is about the ED qualifier annotation and where we need it and why we need that at qualifier annotation okay or at qualifier thing why we need that in spring right so there's the thing that I am going to talk about right now and before I go for that let me come back to my bin start xml file and here you can see the tube in that I have set over here is one up one one is octopus hot and one is human heart the human heart name I have changed because I want the otter where to be happened by name because by type was failing because there are two different types is available over here for the same class so I have taken the name over here right now this name that I messed there I'm going to change it to human heart only because if I'm going to put something like this it is not that readable right so my boss told me hey make it human heart only right and if I am making it human heart over here by name all set is felling because this and this it is not matching and by type is also telling because there are two different class over here so what I'm going to do over here because my name is also felling by type is also felling so what is the solution over here I can give when I do have multiple implementation or multiple objects when I have for the same class and if the ID is also different how to resolve this kind of scenario okay so what I can do right now before I do any modification I'll go to you know hard dot Java in the heart let's add few properties okay let's have some fun here so what I'm going to do here I'm going to create a string called name of animal okay and I am going to create another variable here called int and for this in the variable M I'll give is number of heart okay so this is the to property also I want to configure sorry this is a variable okay these are the two property I want to configure for my hot object so whenever I am creating my hot object hot hot equal to new heart I want to say hot dot set name a set animal name is human hotdog set number of heart equal to two oh sorry for human we have only one number of heart so like that if there this is a different animal like octopus unless say the name will be octopus and our number of heart will be for octopus we have three hearts I think probably octopus has three heart and so what I can do right now to save this properties I am going to use the setter injection so source generate getters setters I'm going to generate everything this time then read both the getters and setters and there you go I'm going to select everything ctrl shift F to resize over here so I do have my two variable over here and I have all the getters and setters for all these two variables and as usual I have a pump method so right now I'm I want to say at this particular you know properties dependency of this hot class over there when I am creating the heart object so when I'm creating the hot object I'll go over here to the Binstead excel file so here I'm creating the hot object so I'm not going to explicitly end it over here rather I'm going to use last beam to end it and this this let's say this is a human heart right for human heart I'm going to define the property less their property and what is the name of the property the name of the property will be for heart name of any well I'm going to copy this I'm going to come over here to a control V n dog you know value for the name of the animal is less a human because this is a human class object and I'm going to end the property tag over here and I'm going to copy this and I'm going to fetch it over here and this time the name will be what the name will be number of heart I'm going to copy this I'm gonna cover over here going to paste it and the value will be of how many number of heart we have for human who are not only okay so you have only one number of heart don't break it so the next thing I'm going to do the same thing for the octopus heart as well so I'm going to end the beam over here and the beam over here I'm going to copy all this property from this one and for this heart object I'm going to set the different properties right this will be octopus and the number of heart will be I think for octopus the number of heart will be three okay there you go so you might be thinking here we'll ash here why were writing the property tag inside the heart okay in the heart class also has some dependency right why you are setting this dependency by using the property tag right you said right you are teasing or Serta wearing so you should not be using this property tag but while you're using it because these are primitive type these are primitive and string type right and for primitive and string type or to wear is not possible right somebody commented who the hell disliked this video Oh No thank you very much for saying bad but don't say somebody to like this because if somebody don't like a video you can dislike right okay so for the primitive type and for the string type Auto wearing is not possible our wedding is only possible for the object on you right so here these are the primitive types and the end types and this is what we have configured it over here right in the heart right now we have a human heart and we have a octopus art right so which heart you need in the human dot java class so you have two object one is human heart object one is octopus heart upset here you have the you have two in different implementation of heart so here for heart you have two different object so by type will be fell and name is also not matching with the variable name over here right so the auto where is going to fail if I'm going to do the auto where in the setter only I'm putting data where and if I'm going to run it it is going to fail for sure right because both the condition is failing so right now I'm going to explicitly tell spring that hey spring here I do have multiple implementation and for the multiple implementation I need to use in a notation called qualifier to let spring know what particular object it need to do the dependency injection right what particular object it need to inject to the heart so this is what I am going to tell spring so I'm going to pick the human heart from here control C and I'm going to paste the human heart inside the qualifier notation and I'm going to run the body dot Java and this time it is not going to fail C setter method is calling your heart is pumping and your ally right now let's call the property that we have set for the human heart for the human heart we have said this property right name of the animal is human number of heart is one so let's call this property so inside the pump method I am also going to print out let's say sis out and let's say name of the animal is and I'll write what heart is my reference target name of the animal Plus and I'll write here a number of heart present and I'm going to say plus hot dot number of heart phrase and said sorry get let's say get number of heart okay get number of hot there you go right why I'm getting an error because I missed applause shine over here and there you go so I have printed it right so what object that I am injecting over here human heart object right so if I'll go to my embodied or Java class and if I'm going to run this application you are going to say setter method is calling because you are using the edit / annotation before the setter method there's sy its using the setter injection so then the next thing is your heart is pumping you are alive the name of the animal is human and the number of heart present is one right I think you got the point why we are using the @ qualifier annotation right so when you have multiple objects right you have multiple implementation which one to use which object to use this implementation to use if spring is get confused to a at qualifier annotation after the auto where and put the exact object you want so suppose I want to inject the octopus object over here so I should not be doing it because it's a human class for the human class I need to only give the human class object let's say I want to inject the octopus heart to a human so what I'm going to do I'm going to come over here I'm going to copy this I'm sorry I'm going to copy this ID and I'll go to human dot Java and here in the qualifier I'm going to change this qualifier to octopus on right right now if I am going to run this application I'm going to save the bin saud XML file now let's say your heart is alive the name of the animal is octopus and the number of heart present is 3 right so right now we are using the octopus object that we've created inside of in start XML it is simple but I should not be doing it right it is a human class so we should be injecting the human class object right because I'm going to check in this code to gate and I don't want somebody to confuse to see this code why this human has a octopus act right human should have a human heart only right okay so oh why is still not working if I'll go tube instead XML file I'll copy this I'll come over here to the human dot Java okay I need to change it to human heart and let me run this again okay cool so the animal name is human and the hot present each one so right now probably I'm guessing you are clear with Auto wearing how to configure with XML what is the different type of photo wearing we have like my type by name by constructor and how to configure Auto wearing in the Java end by putting the auto word annotation and how the auto where annotation is getting used how it is resolving by using the by type and the by name and what type of approach it is so you know using first and then how it is resolving the conflict by using the by name and what is the at qualifier annotation when we need to use it okay I think you've got this concept clear right and there we go and yes one last thing that I want to tell you before I wrap up okay here I'm using the editor word and at qualify before the setter in the setter method right so I'm going to call this one okay and right now I'm going to use this before the dependency right before the variable only I'm going to use it so right now if I'm going to run this application if I go to body dot Java and if I'm going to run this application it is going to run as expected but wait one thing you can notice over here that you know inside the setter method you did put a in a logging statement here a setter method called but in the output console you cannot see their sentence so one thing we got to know that it is not logging our setter method statement here you so what if if I am going to remove all the setter constructor and the default constructor that I have configured over here I'm going to removing everything here I just have the dependency here right now if I am coming to body algebra and if I am running it you can see the magic well is running right so I do not have to generate a setter in this case if I am writing the the a Toto where before the dependency and you know if I'm removing this stuff over here and if I'm doing ctrl s and if I am trying to run this particular program yeah there you go you are dead because no object is getting ins acted right but thing is you know the whatever concept is same right first it is going to check for the by types and then it is going to check for the by name [Music] okay so I'm going to explore more about this in the coming videos but for now edit ahead first looks for the by type if there is any conflict then it is going to look for the by name okay so that's the formula that's the fundamental okay so this is the concept for you guys I think you already got it I'm also getting tired because it's a long session for me and I hope you guys have enjoyed this session now I think my energy is down I shouldn't be talking more because you guys will be bored so that's it for today's tutorial don't forget to like this tutorial if you really enjoyed it and if you don't then let me know okay what part you know you want me to improve what the improvement that you want to see from me right you can let me know there and if you want to tell me any new thing feel free to post that in the comment section or you can write me a mail at selenium Express at the rate gmail.com or you can just message me in the Facebook page I mean in my in selenium Express Facebook page which is Facebook slash selenium Express right so you do let me know what do you think about this tutorial please post a comment and let me know and that's it thank you very much for watching it and I'll see you in the next video till then happy coding [Music] [Music]
Info
Channel: Selenium Express
Views: 120,887
Rating: 4.9421082 out of 5
Keywords: spring annotation tutorial for beginners, spring annotation tutorial with examples, spring tutorial annotation based configuration, spring tutorial for beginners with examples, spring framework tutorial for beginners with examples, autowiring in spring, @autowired annotation in spring, autowire byname vs bytype, @qualifier in spring, autowiring in spring example, autowired annotation in spring, autowire by name spring, spring by abhilash, spring tutorial by selenium express
Id: aULu-QRagXI
Channel Id: undefined
Length: 69min 35sec (4175 seconds)
Published: Sun Oct 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.