Spring annotation tutorial for beginners |All In1 spring core annotations |spring framework tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back so welcome to the episode number eight of spring coal framework tutorial series so today we will be talking about spring annotations well you already know a little bit of spring annotations and we are kind of using the spring annotations for the last two videos but here in this video we'll be talking pretty much about spring annotations okay and I'll be covering a whole lot of different annotations which is available in Spring Framework and particularly which is belong to the spring core framework okay so the intention of this particular video is to make you familiar with the spring code annotations and also I'm going to eliminate the spring XML file completely and will be only a writing code in Java so this and another bonus that you are going to get with this video so we'll be doing a whole lot of things today and it's going to be pretty simple because you already know a whole lot of things about Spring Framework and you already know about dependency injection Auto wearing and all the stuffs right so before you proceed with this video I definitely recommend you to watch the earlier videos of my series so that that will give you a kind of good fundamental on Spring Framework spring IOC and all the stuffs right and after that this particular video will be pretty simple for you right okay but if you are already following my tutorial then don't worry this particular video is going to be very much simple for you but again it's going to be a lengthy video because I'm not getting pretty much time for myself to shoot videos but I'm gonna have occasion right now I got pretty messed up time so let's have a lengthy video but whenever you are you are filling okay is too much of information for me then pause the video take some rest come back to the video again and go with the video right which talking you to Lourdes okay so let's get started and let's go with the video [Music] so right now I hope you guys can see my screen here and here I have just created a very simple project called spring common annotations and I have created a very simple package as well called comrade selenium Express start college and we want to go back to our college days today so I'm going to create a class called college right so let's create a class you're called college ok all right and let me hit finish here so right now I got a very simple class called college here ok so now tell me one thing ok so as of now we are just you know dealing with our configuration file when I say configuration file is exactly these files right bin start XML file ok so this is a particular XML file that I've copied from my previous projects and from my earlier tutorials always we follow this approach to configure any beams so whenever we want to do any type of configuration like dependency injection like being creation like you know activating any kind of annotation tag or whatever right we are configuring everything over here inside this bin start xml file right so now today i want to give you a task ok first of all ok so right now let's say i have a class here called college ok let me maximize this class here ok so right now I have this class right class College ok so right now I'll just give you a task that hey you create a beam for this ok I want to configure this college class in the bin start XML file so if I tell you to create the beam for this particular college class is pretty simple right what are you going to do I'm going to copy the qualified name and here you can simply create a beam by using the beam tag here ok and here first of all you want to give give an ID name here and you are going to provide let's say college team as the ID and I to that you are going to give the class right the class here and you're going to control V here so here I'm just basically pasting in on my qualified name here and this how we use to configure the beam earlier isn't it okay so we know that right whenever we were to configure the beam we're going to come back to our bin start XML file and over here we are going to create the beam by using this way so this particular line of core tail spring that hey I want to create the object for this particular class called college right so this is the class and for this particular class I want to create the object and I want to give it an ID called college beam so just like so spring will create a object like college call is being equal to new college and once it's create the object it is going to register that particular object inside the application context or what we say our iOS a container right so inside the container so once now spring is going to create the beam it is going to register that particular beam odd it is going to by now register that particular object or we can say it's going to put that particular object inside the iOS a container right so once this particular object got present inside the ioc container we can just say ok get beam get beam method and we can pass this particular ID called college beam and we can get back our opposite right and once we get the observed we can do whole lot of stuff right with that particular upset ok this is a pretty simple thing but what if I'm going to tell you ok right now this is a way to create a beam this is a way to configure a beam but I don't want to configure a beam just like this I'm going to remove it from here so the way I want to configure I don't want to use the XML file over here rather I want to configure the beam here inside the Java side so you got the requirement I don't want to configure the beam inside the bin start XML file just like this rather I want to configure the beam inside the College dot Java in this particular file right inside the class only I mean here in this class only I want to configure the beam so how can I do it ok so if I want to figure the same thing this thing over here inside this particular class what I can do is I can simply write an annotation called eight component right remember this particular annotation is called eight component right so this eight component annotation basically does the same thing as this right so this thing does what this thing creates the object for us and going to register that particular object with the ioc container and if i'm going to remove this this particular annotation is exactly going to do the same work as that okay so whenever spring find any classes got a annotation called eight component that means spring thought that okay that means this is a component for this particular class i want to create the object right this is actually a beam i want to create the object for this particular class and once i create the object i am going to wear the wear spring is going to store that object inside the ioc container only right inside the application context so once spring create the object for this college class this is going to put that particular object inside the application context pretty simple right so this particular component annotation does the same work as this okay right now definitely you will get a question okay so a village so here we are putting our class name inside the bean tag that's fine right now we are putting the add component annotation we further before our class that's fine but here we used to give the ID for a particular beam right so here we are not providing any ID right inside here in the component we are not providing any ideas so what's gonna be the ID name of that particular beam and that's the right question there's a valid question so what i'm going to do right now so this is this is the ID right so i'm going to copy this ID so if you want to provide a ID to this particular beam right so to this particular class right I put this add component annotation right now this is going to be a beam for us spring is going to create the object for this particular class and the way I can give the ID over here it's just like this right so right now here for this particular class called colas spring is going to create the object for this particular class called college and the name of that particular offset the name of that particular beam is going to be called spin so it's going to do the same thing right college college pin equal to new colas right and once the object got created obviously that object is going to be stared inside the application context or inside the container and we can do our get bin method and can get the beam for this particular College right and we can do our business logic business operation whatever we want right ok so right now less less clear the doubt right first of all what we are going to do we are going to taste this whether this is working or not so particularly what I'm going to do right now I'm going to go to one of my old project here and I'm going to copy a simple client class and going to put it over here so that I will not waste my time and waste your time as well your time is so precious isn't it and obviously my time as well so what I'm going to do right now so over here you can see let me maximize this client class ok so here what I did I actually I'm actually creating my application context object so application context context occurred to gnu classpath application context provide your configuration file name so here in this case my configuration file name is bin start XML right so let's say bin start XML and let's put a simple debug line over here just to track whether our context object is getting created properly and after that we are going to get this particular line inside the console right so right now what I'm going to do once the application context of that got created we are going to say okay so my container object got created and from my container so context is my container I'm going to give the bean I'm going to pass the ID of the particular bean so what is my bean ID right now call it spin right so from here I already removed it right ok so let me remove it because I don't need it right now because - stop my component annotation is going to - and this is my college bean that means this is my ID so I am going to come back to my client class going to provide my beam ID and I'm going to say this is going to it on me the college dot class I'm going to basically put the type of it and this is going to return me the College object right okay okay now if this thing is going to work fine okay why it is giving me an error okay this gun okay so if this thing is going to work fine if I get back my college object that then we can you know understand that this add component annotation work same as this okay okay so right now what I'm going to do I'm going to remove it come back to over here and we can actually have a simple you know println over here we can print something just to make sure that our college we can print our college object right so we can say the college object created by spring H and I'm going to concatenate this college object pretty simple so right now if I am going to get back my college subject then I can make sure that okay this add component annotation is working as expected so right now let's go back to my client class and let me run this particular application and let me check what is going to happen and you can already see in the screen my bean start XML file loaded that means this particular file is getting loaded perfectly over here this is loaded then this particular line got printed now here we are having some problem right okay so what is the problem spring could you please tell me that okay so no such bean definition exception so what it is basically saying is that there is no such bin available called college pin what the heck I just told you right this this add component annotation is work same as you know that bean tag that we used to write in bin start XML file right but why it is not working why why we're getting error just like this that okay there is no sure being available just like always been but here actually we are you know defining that component annotation with with this particular beam ID right College Fein okay so here we have done a small mistake okay so what is that mistake so whenever so here spring is reading this particular bin star XML file and it is not finding anything right so it says okay so there is no such being available over here like college pin right here here nothing is present so here we need to tell spring that hey spring we are not defining the beam over here but we are defining the beam somewhere else and where we are actually defining the beam we need to tell - spring so that spring can go there and can scan for those particular beams whether it is available or not and once it is available it is going to create the beam for you and going to you know give that beam back whenever you need it right so how we can tell that thing to spring the way we can tell is we can use and you know tag over here called context column components can okay so here we are going to scan for our component so here we we are defining our component over here this particular class is a component right this is this is you know this is a special class third present which is going to be which is going to be instantiated by spring okay and to make that happen we can do one thing we can tell spring that hey spring this particular class college is present inside this particular package called Comte or selenium expressed or college so what I can do over here I can come back to my bean store XML file and can define my base package over here so in which package this particular college store Java present inside this particular package right so let me do one thing let me write my package name comma dot selenium express.com right test done and let me end the tag over here there you go so right now what will happen right now whenever okay let me maximize it again okay so right now whenever spring is going to read this bin start xml file okay at that time what will happen it is going to find that okay there is a component scan available over here so that means spring is going to scan through this particular package called comm dot selenium x phrase start college and inside this particular package whatever the class is available right now there is only two classes available but whatever the class is available inside this particular package called college here if spring is finding a annotation called at component spring is going to create the object for that and also it is going to register that particular object inside your ioc container so right now spring spring is going to come over here is find this particular package come over here is find this particular class having this particular annotation called add component so it is going to create the object for that and once it has created the object for that it is going to put that object inside the inside your container right you so what is my container object context right let me maximize it okay this is your contact context object so right now I can say inside this context of set my being is present right so right now I can say context that gate beam so give me particular being called call as being this particular beam right give me that particular beam and I'm providing the ID and providing the college start class right now I'm getting back my beam storing it inside this particular College right now I'm just trying to print that particular object which spring has created right ok so now if I'm going ahead and running this particular application and there you go so you are getting ok this is your college subject right so call is at the rate something right so this is the object right now spring is creating for you so here what we understand that right now spring is going to create the object by itself whenever is fine this particular annotation before the class right called it component annotation right and here where this providing is that beam ID just like we used to do it in in XML kiss right just pretty similar to that simple looking good so you understand how the add component annotation work good right ok so right now we understand how the add component annotation works right so right now we're going to understand one more thing and this is pretty important ok and you are also looking for this particular thing right so what about I don't want to you know hit this bin start XML file so right now this component scanning stuff right so this thing I am doing it from the XML end right from the XML only I am defining the base package right but I don't want to do it from here I want to do it from the Java int right so what if I want to completely delete this particular file called bin start XML file I'll just hit control till it I'll just delete this particular file right so right now I want to activate the component scanning from the Java end and for that what I need to do right that's the thing that we are going to right now but before that let me hit a control jet here just to get back my beam start XML file okay so this particular file is what file this is a configuration file right that means what kind of configuration that we are doing we're defining all those configuration inside this bin start XML files so this this is what we are saying a configuration file right so what so if we are not using this bin start XML file okay if I am deleting this particular file and if I want to you know you know you know do everything from the Java side then first of all I need to create a configuration class right okay so let's create a configuration class so I'll create a class called as a College config let's say configure and let's say finish okay so right now I have created a configuration file isn't it so how to make this particular class as a configuration class we put the name here config in this became a configuration class right just kidding so to make this particular class a configuration class what we need to do we need to use the annotation called at configuration right so I'll write config configuration ok there you go so this particular annotation called at configuration this will tell spring that hey this is a configuration class right so this particular annotation just going to direct spring that ok this is a configuration class and ok and all those other stuff is going to be taken care by spring yourself ok so right now this is a configuration class and from this particular class we need to define our component scanning right so why we need the component scanning I already told you right so we need to scan through all these components ok in this particular pack is that we need to define our package in that particular package whatever the class has has the complete component annotation spring is going to create the beam for that and to do that inside this configuration class I need to define my base package right this is similar to our XML file so base Becca's I don't want packages okay okay so this wanna be the best package this only because we can define multiple packages but right now we are going to define only one package right so what is our package name comm dot selenium okay let me maximize it again okay selenium express.com edge right so this is my best package so right now spring is going to read this particular class previously we used to read our XML file and we are defining our component scanning there but right now we are going to make spring to read this particular configuration files and whenever spring is going to read this particular file it is going to scan through the best package which is combed or selenium expressed at college and from this particular package it is going to find whatever the class add component annotation it is going to create the object for those particular classes so to do that I'll be coming back to my client a java application right now if I am going to run this particular class what is going to happen obviously I will be getting an annotation oh sorry I'll be getting an exception so if I will maximize this console window the X the exception is pretty common right io exception right why we're getting a IO exception because right now we do not have this bin start XML file right so it's saying class path resource bin start XML file cannot be opened because it does not exist because we have deleted it right so right now this class path XML application context is not going to work because we are not dealing with the XML rather we are dealing with the Java classes right we are defining our configuration classes not the configuration XML file right so what I'm going to do right now rather using this a new new class path XML context I'm going to delete this one right up to this I'm going to delete it and here right now I'm going to use a new in a class called annotation configure something a notation let me check that out a notation config yep I think this is the one annotation config application context okay and over here I need define my configuration class so which is my configuration class this one is my configuration class called College config dot Java let me come back over here and let me define the class over here College config dot class there we go okay so right now application context context occurred a new annotation config application context where name last name but that's fine right okay so right now we are so right now we're actually defining our configuration class over here you know in the line number eleven right so you know so application context and you know that this is an interface and xm+ path XML application context annotation config application context these are the implementation classes we says they're specific uses and you are just seeing it right now okay so obviously all the other things will be same so right now I don't need this system router being stored XML file loaded let me remove it the line number 12 and there you go and after that from the context so what is going to happen it is going to read this config class in the config class we are just saying that okay do a component scan from the base package this college package right now it is going to go to the college package and whatever the class is over here inside this College spec is with a eight component annotation it is going to create the object for that and register that particular object inside the application context or inside the container so right now a college object got created inside the application context and whenever I am going to in the client of Java whenever I am going to ask for that college of debt by providing the right bean ID then it is going to give back my college object and actually I am just logging that college object right over here so right now if I am going to run this particular application be sure it is going to run as expected so right now I can see right call is at the some hexadecimal value you know the high score representation of hexadecimal value right sorry the hexadecimal representation of the high score value sorry so you know right now now this is how the things work so right now we have completely removed our XML file and the way we did it by introducing a new configuration class over here right rather rather doing rather creating a XML file as a configuration file very simple I have you are looking good oh sorry I hope our code is looking at okay so right now no we learned about a component annotation at configuration annotation and add component scanner notation but right now this is a pretty simple class pretty empty class we are not doing any kind of configuration over here but what if let's say whenever we are actually dealing with our XML file right so we used to define our beams we need to quit new we need to define our beans and everything inside the configuration file only inside our XML file only we used to create different beans so how to create different beans over here okay so I told you one way to create bean is using the bean tag and another way and another way to create bean ears by putting add component annotation right but what if I want to do something else right I'll just tell you that right now but before that let's say inside this college class let's say this particular things are working or not so to test it so what I'm going to do I'm going to define a public void test method just to make sure that things are working fine and let's have a simple sit here let's say testing this class method ok so right now let let's call this test class and let's check it out everything work as usual or not right so let's go to our client class so right now let's say college object reference dot paste method right if I'm going to run this everything works fine most probably yes testing this call call matters right so everything working fine right so what if right now I'm going to tell you that hey you know what you don't need to use this add component annotation as well right do not create a beam by using the add component annotation so now I must be thinking that okay first you told me about I know the bean tag right beam element right then you told me don't use that now you told me at composite component annotation and at components can right now you are saying hey don't use this as well okay so yeah so I I basically told you about these two things these are two different ways to create beams so there is an another way as well so if I don't want to use add component annotation as well if I'm going to remove it okay then also I can create B that is another way to create pin by using at beam annotation right we are going to talk about that right now previously if I if I do a command set I will get back my code changes okay so this add component is a spring annotation right so if I do not want to use any kind of special annotation in my class and I want to you know create the beam how can I do it right so in my college class this is my class okay in this particular class I do not have any special spring annotation but I want to create the beam for it right and just to make sure I do not have any bin start XML file or any configuration XML file over here as well I just have a configuration class here right and I want to define those beams inside this configuration class previously we used to define pins inside the configuration XML file right now we are going to define everything inside this config class and let's check it out how you okay so let's start creating our beam by using a 8-pin annotation right so I have already removed the add component annotation that means spring is not going to create the beam for us and also I do not have any you know bin stored XML file or any kind of configuration file over here inside this SRC so that there is no chance that spring is going to create the bin for us so so right now we are going to do we're going to configure the beam by ourself right so how to do that there is another way to do it so let's check this out so right now first of all we need to go to our configuration class right you so inside our config config class right inside our college config class we are going to define our beam okay so beam for what beam for what we need we need our spring to create the beam for this college class but not by the previous two approached by a new approach by putting the ED beam annotation so before that okay so what what do you what do you mean by beam so we know that right well you know is just going to create the object right okay fun for now what class object we need we need the college class object right so we'll go to this config class and in this config class what we need we are going to define a package here public let's say okay so we need up we need to create a method which is going to return the college class object right so let's come back over here it is going to return the college class object so let's take college and let's say no college be okay so unless the college beam is my method name and here I want to give you an another information that this College beam right this is what this is the method name right so the method name is going to be the beam name so or the beam ID so the college beam is actually the ID of this particular beam okay okay I'll be coming back to this particular part or later right so right now here this particular method is going to do what it is going to return me a call as upset so I'm going to create the college upset College College equal to New York College so we are going to talk about a lot of college stuffs today so it is going to return the College subject right return college or we can do simply in one line also we can do what new college ok and we can remove this particular line right so this particular method does what it is going to return the college class object that's it we just need a method right now we want to create the beam for this we want to create a method which is which is going to expose our beam so to expose our beam I have created a method over here called college bean and this particular method is going to return me the college object that's it okay so let's stick to our previous approach you know just I want to do something kind of old-fashioned stuff okay I just want to do coding like a dump because I am dumb okay am ia dumb whatever I don't care so so right now Here I am returning my college beam right so right now to make this method okay a beam so what I can do over here I just need to put the beam annotation here that's it right now you have created a method to expose the beam and this is a another way to create a beam that's it well done you have created a method called college beam and this is going to give you back the college class object sure great so right now as I said in this method this College beam the method name this one is going to be my pin ID be sure about it okay so the method name is going to be the ID of this particular beam so right now let's do one work so here I do not have any a notation called add component so I will go back to my client or Java this is called as pin right this is my ID okay so here I got this ID let me copy this again this is gonna be my bean ID and let me come back over here let me replace this again just to make sure there is no typos okay again this is a college class I am getting back my college object and to test it out I have created a very simple method called test which is present inside the college class so once we get our college class subject using that college class object we are calling this particular method and that's what I'm going this what I'm going to do over here as well right I got my college class object I'm calling this test method and let's run it and finger crossed and hopefully it is going to work and it worked okay so you know you are getting your college class object here which is created by spring and it is a spending testing this called method what does it mean okay let me go back testing this called method okay what does it mean testing there's you know call this method testing testing this method okay just s satisfied okay so right now okay everything looks good right so you know how to create a beam by using a beam you know annotation right right now if I'm going to remove this now what is going to happen if I'm going to remove this right so now if you come back to you over here and if I'm going to run this particular in our class it is going to fail again it is going to say there is no such being called College being available so you know you are asking for the college beam over here which is actually the method name but over here I have commented out sorry over here I have commented out Idina notation so spring is not able to recognize the you know college beam as the beam ID right so make sure this a normal method so to make to make it work like a beam you need to define this Idina notation before this particular method and this is a method which is which is just simply a method we have created to expose our beam that means just to you know which is just returning our desired beam object I mean these are class object okay good so I don't go by my worse you understand the concept okay good okay so now now everything is looking good so okay so this is looking good but right now can I give a different kind of beam ID to this particular method I think probably I can let me check this out destroy method auto where name okay this is the name I think you can give multiple name to this particular beam okay I'm going to tell you that later okay first of all let me give one particular name to this particular way so I don't want to have this college beam I want to have something like call bin okay this is going to be the name of might be right so right now if I'm going to go to my client or Java if I'm going to run this application it's going to fail yep so it's right now it is not recognizing the college pin because we have overridden that in the config file so let me take this and let me go to client or Java and going to replace this stays and that's how if I'm going to run this okay now this is working right so then that's this how you can overwrite this pin name by putting the name of her here I think you can also give multiple name to this particular beam as well so for that I think you need to define it inside the you know Kali places and you can let's say call Ben let's say another College being another ok let me test this out with call bin first of all over here the name is call bin let me run this out okay this is working right so this is my college object right and testing this method that method is getting called properly this this is working fine so let me copy this as well let me go back to my client the Java class let me change this to this let's do control is right now let's run it again ok this is also going to work and it's working right so you can also provide multiple alias name to a particular beam as well ok that's good so right now let me remove all this stuff and let me go with the default name ok this is going to be the method name right simple ok so ok one more thing I haven't told you that so let's say over here right now I have I'm doing everything with this annotation stuff right okay so let me comment this out for a moment there you go ok so why I have commented it out so let's say in the college tour Java right now I want to define a bean by using at component annotation it component annotation and here I actually want to do the component scanning ok ok and one more thing I forgot to tell you okay for this Idina notation okay if you if you are defining a beam by creating a method like this you do not need the this add components can right I'll just show you that I forgive me for that okay I just I just skipped it okay no problem will be going way more detail in the coming time so don't worry I do have a whole lot of time for you today so so right now what I'm going to do let's say I'm doing a component scan over here I'm scanning this base package this College and I'm putting this component at component symbol over here but I am not actually providing a beam ID right if I am providing it be an idea bin ID over here why if I'm providing a beam name something X Y is it so that's fine okay I'm sorry it's not named okay if I am providing a beam name of anything this is gonna be the ID of this particular beam so call is this equal to new colors right so this is going to be my beam ID right so if I'll be coming to my client class and if I'm putting this if I'm running this application they should run fine right because that is my ID but if I am not providing my ID so what is going to happen so right now what is going to be the ID of this particular beam okay so right now this is called as class right so make sure if you are not providing any beam name over here by default the name of the component is going to be same as the class name but the first letter will be obviously a small letter right so the meaning of that is so if I'm not providing anything by default that thing will be just like this small C the name of the component is going to be this so so if I am going to remove it let me copy this and if I'm going to remove this right now okay right now my component does not have any any beam ID so right now it is going to have a default beam ID which is same as the class name is you know this College so right now I'm going to go back to my client application and going to be paste my ID over here make sure that this is starting from a small letter right okay so right now if I'm going to run this particular application it is going to work fine as usual you got it okay so default bean ID okay default bean name if you're not providing it if our name will be same as the class name so easy okay okay so the next thing right now I don't want the add component annotation let me remove it again I don't want okay I want to go in this way okay okay so if you are defining a bean if you are defining a bean by using using this approach by you if you're writing a method to expose a bean by providing a Idina notation at a time you do not need the components can write you can remove this thing as well right you need only at configuration okay so right now let's just take it out right here you do not want this import as well so this important specifically belongs to the component annotation let me remove this as well so right now this is a very simple class right it does not have any kind of annotation so everything right now you are configuring inside the inside the college config class right so here you do not need the component scanning okay let me remove this important job okay so you only need the admin annotation and you need the add configuration annotation before your config class just to make spring know that this is a configuration class right so right now I remove the components can interpretation from here because we do not we are not using the component scanning and if I am going to run this as well so okay no charge beam definition why okay so this is college let go to our configuration class this is going to be our ID control-c and let me do a control V here control s and let me run it oh yeah this is going to work okay so this is how you need to define a bean by using the it'd been annotation okay this thing is also clear so right now okay but what is okay this is a very interesting thing what if I have a dependency okay so let's say if I have a dependency over here let's say write it or what kind of dependency call it says color should have a principal right every college should have a principal otherwise the student can do anything that they want right so the college would have a princey oh right principal okay Bri NC IPL okay so right now so I got a dependency over here let's say there is a class called principal so let me create a class so right now Eclipse is giving this error because we do not have a principal class so we're going to say eclipse that hey create a class for us called principal because we want to create a dependency and we're going to learn the dependency injection using the and week class using dot eighteen annotation okay it is good so let me create a class over here called principal and let's say in this principal let's create a simple method called public Lloyd principal info okay let's say she's out hi I am your principal okay and we can copy this and going to paste it and going to say that my name is who's the principal name my name is James Bond okay that's a cool right so right now okay if if in the college the principal will be James Bond it'll be so much fun isn't it okay so so let's say right now this college right now does have a dependency called principal and we want to do the dependency injection by inside this configuration file that we have created right so how to do the dependency injection let's let's check that out so first of all let me generate the getter and setter for that okay so so right now we we want to do the dependency injection right so we could have do the dependency injection by using a setter or a constructor okay first of all let's do with a constructor so I'm going to go to source a let's go to constructor using fields okay let's generate a constructor over here and there you go I don't want the super let me remove it there you go okay you looking good okay so what I did actually over here I just created a very simple constructor here for to inject this principal object to this right and inside this test method what we can do is we can call this principal class method called principal in for right this is the one okay so right now what is the value of this particular principal so right now the value for this particular variable is null obviously this is null rights that we need to insect a object like new principal okay so once we provided object to this particular variable just like this then this is going to run okay and if I'm not going to provide the object to this then we are obviously going to get a null pointer exception because the value for this right now is null right so first of all let's do a constructor injection in in our college config tórshavn right right now you can see this particular constructor right now it is giving me an error because inside the college class we do not have a default constructor right we have operated this particular constructor and we do not provide any default constructor to this so we are getting an error but this doesn't matter because we are going to use this particular constructor of college class to insect the principal object to test okay so first of all okay so right now how many object we need to create a look at this particular class we need to create the object for college we need to create the object for principal right we need to we need to deal with two objects over here so let's come back to over here we have already created the object for the college dean for the college we have already clear that the object there is some error but we are going to fix it okay so right now what I'm going to do over here I'm going to create a new method to expose the principal beam right so to expose the principal object right let's create a method let's make it a public method and this particular method is going to give me the Schiphol objects of principle let's say princi-pal bin okay and here this particular method should return what let's say it should return the new principal object okay new principal Telekom right and right now we have created a method which is which is just going to give us the principal object back right and we want to make it pin so if you want to make this as a beam then we need to use the and beam annotation we already know about it right okay so right now we have to been over here but inside this College beam right there is a dependency this College has a dependency and dependency name is principal so we not only going to create the object for these we are also going to create the object for this and we are also going to inject that object the dependency object using the constructor we need to inject the object for principal you know do this right to this particular variable so to do that what I'm going to do right now so my principal beam is ready so here this new College constructor this call is constructor want what object this caller is constructor want what object this one this is wanting a principal object so we have already created a method and this particular method is actually giving us back a principal object so we can only need to call this particular method inside this constructor argument so what I'm going to do this is the method right so I'm going to copy this I'm going to paste it and let's call this method okay this is what I want you to do so here what I want to do actually I'm just calling the principal beam method to this College constructor right so this principal beam method does what it is giving us back the new principal object right so it's just similar to this control P is there similar to this right so right now you know instead of this I'm going to do command cheat okay there you go so we want to do it to do it in this way and calling this particular method to this and this particular method is actually giving me this object right so right now it got what the new principal object over here so right now this particular constructor get called right it got the principal object it the nuke new principal come over here and it is actually setting it to this so the new principal come over here right okay now this is how the things went right so this is just to show you will let me remove it again okay and if this will be also initialized this particular method will be successfully called right if it'll be if the object will be not injected to this we have already seen in the earlier cases will be getting the nullpointerexception right so now let me go to my client ajapa right let's check it out this let's say it is working or not okay so there you go so hi I'm your principal and my name is James Bond okay so that means that that means this particular variable is getting the object of the principal that means our dependency injection is happening and now this particular method is getting called successfully right which is belongs to the principal class command click this method belongs to the principal class call it and this is this is the way we are defining a couple of beam over here inside our configuration class right we go we're learning a whole lot of thing we're doing good right I'm feeling really excited and I'm looking forward to do some more stuff today so do you have the energy and if not take a break have a cup of tea and he will be all good I know you are very keen to learn new things and we are going to learn a few more new things today as well all right okay so okay so the next thing that I'm going to do right now is okay so here in the windows in the college config okay in the if you talk about this particular beam this College beam right so here we are actually injecting our beam by our principal B by using the setter injection what about a constructor injection right is going to be pretty similar as this but okay let me tell you that also it's not going to take more time so I'm going to comment this constructor method and right now I'm going to generate the setter for this because I want to do a setter injection instead of a constructor injection so I want only the setter and I'll just hit okay so I got a setter method right now we are going to do the injection right to this variable by using a setter instead of a constructor right so right now let's go to our colors config right now and let me save this particular file if I save this alligator error over here because right now there is no constructor present over here which will take a principal object because we have commented out this particular constructor right now let me come back to here and what we are going to do we are not going to do it in this way instead I'm going to do what in this college class okay I have a setter method present right set principle and Here I am going to pass the principal object so this method is actually giving me back the principal object so let me copy this patient over here and there we go okay right now let me go back to my client or java class let me run this particular application and this is come to learn fine and then this time this is using this particular setter write this out using set this particular method set principle let me copy this set install method okay control s let me come back to here sorry and inside the client or Java let me run this again and we are getting the same output and in this time we are using a setter instead of a constructor when we are doing a setter injection that's good so we are learning a lot of thing right so let me remove this import this this important actually for the class path XML application context these days we are not using it we are refusing annotation config application context okay so can you guys guess why I'm getting a warning over here in the in the in the context I'm getting a warning right why because this context is not closed yet right so to close it I need to say context dot close okay I'll not get this close method I know because this context is there is belongs to this particular interface right and this interface does not have this particular method so let me type cast it to annotation config application context now I can do dot close oops okay let me wrap this out I'm sorry let me wrap this out mean can I do it right now close yep okay so now I can see the warning is gone from here or you can simply do this thing okay let's not do this down casting let's not confuse you guys let me just simply create the object like annotation config application context context equal to new annotation config application context right instead of the interface reference let's take the class reference and if you are taking the class reference so we can directly say our class variable which is context this is my container variable dot close okay right so let me remove this application context also from here looking good so right now they're bonding gone right because I'm closing the container yes okay you all right so so things are going good right now so the next thing that we are going to do is okay so guys this is what we know that how to do the dependency injection by using admin annotation okay so you might be also getting some some interfaces right let's say okay so let's do that thing as well so let's say know what if what kind of service my colors can provide the best service our college is providing is it is giving us education which is so precious right so unless they're right now I have a interface called teacher and teacher okay so right now let's say this is the interface right let's create a new interface create a interface called teacher and let me finish and there you go okay so right now this is a interface and let's say teacher is basically going to do what he is going to teach right all the interface methods are public then I'll write void then I'll just define a method called pitch okay okay so you know okay we're missing our teachers isn't it I I miss my DS is pretty much okay so right now we're going to have some implementation classes as well for this teacher right now let's say let me create a new implementation class called math teacher math teacher the CH ER okay and I need to add interface over here lest the teacher interface okay let's say finish okay so I got a fully I know implemented class over here generated by eclipse okay so let me override my teach method over here because my math teacher is implements the teacher interface and inside the teacher interface this particular this method is present right so right now here let's say but I had met so how many of you had met let me know in the comments section because I hate math pretty much so then might be wondering what I'm doing over here right okay I cannot do anything else rather than you know doing coding I know a little bit of coding I am I'm a little good in coding so I think I can only do this job and apart from this I don't know anything so so right now let's say hi I am your math teacher right and let's say it's is out let's say my name is sort of right okay because I love sort of ganguly right so let's say my name is Soros okay so right now let's let's let's go back to our college stores Java okay so right now for this teacher let's generate a getter and setter let's in rate only Gator for this because we want to sorry let's then rate only setter for this because we want to do a setter injection so generate the setter for these and I'll hit okay there you go and let me maximize it okay so so right now in my college there is two dependency when it's principal one is teacher right and for this teacher I have written a setter method to inject this teacher object to this right but remember this teacher is what this dessert is a interface right okay so right now let's come back to here and inside this test method let me let me write teacher dot teach okay so because this this method is present inside the interface right and based on our object based on our runtime object like let's say for writing new math teacher right math teacher is the implementation class for this particular interface so interface variable is equal to nu plus name okay core is our stuff so right now let's say okay this thing we want to do it we want to configure it from this particular class call its config right we need to define another beam but this time make sure that this is a interface variable this is not a class variable this is a class variable right because this principal is a class right but if I will come back to here okay and if I go into this teacher this teacher is an interface right so the exact same thing we need to do inside our configuration file okay so now what I need to do let me remove this because we want to do this thing from there okay so now let me remove this constructor this is just not looking good to me okay let me remove it okay so right now let me go back to our our College configure Java over here I need to define another beam okay for my teacher so let's say let me post declare a method public and this is going to return me the interface teacher a cure okay teacher and let me give the beam name let's say this teacher is going to be my math teacher beam okay okay you can give any name you want okay right now what I need to do I need to return that teacher object right I need to do it de ses you are like this like this dude this is a interface variable this is the interface right for interface we cannot create object right so we need to do what we need to create the object for the implementation classes so who is the implementation class for teacher for teacher there is there any implementation class let me do right click quick type hierarchy okay this is the implementation class math teacher right so for this we have already given the beam name so what I'm going to do I'm going to return the math teacher but is that okay Matt teacher this is okay I'm going to say new math teacher okay so right now this is just something like teacher teacher required a new math teacher right I'm a teacher is the class these are attentive s simple right so right now this is going to give me back the teacher object so I'll come back to here so I have already written a setter method over here inside this college class I have a set teacher method okay so let me call that particular method inside my college configure Java so in my college there are two dependency first I have created my college object and I have set my principal dependency then I have to set college dot set teacher and let me pass my math teacher beam over here okay I just simply call this particular set or method and I don't want to define it as a beam I want to expose this method you know to expose my beam I have written this method for that okay there you go I would you find couple of B I think I've defined three beams over here right okay and this is our main class and here we're actually setting our dependencies right pretty clear right now let's go back to my go back to my where my inclined or Java let me do what okay everything looks good over here let me just run this application and let's see our math teacher stuff is coming over here or not and there you go my name is syrup okay and I'm your math teacher okay it's working fine and that's how you need to actually do need to create the beam for your interfaces right make sure that this teacher is your interface it's not a class simple done okay let me wrap wrap this thing up but I'm not going to stop over here I'm going to talk about few other stuff right okay so this is looking good right now you know how to configure beam you know how to use that configuration at component at component scanning it pin annotation okay looking good so right now right now let's do some other stuff here okay so right now let's say okay so how to okay right now we do not have any XML file right we are doing everything over here in this College configure Java right so now let's say okay this is pretty much enough we are getting everything okay let me okay let me do one work let me comment this all these things out and let me go back to my previous approach okay everything commented out you already know how to do all this thing let me come back to my college tour Java and now I want to use my I want to go back to my component scanning and I want to do some more stuff I want to explore some more stuff right so that we can talk about some more annotations in spring are you getting bored am i boring am I talking too fast I do not know so actually I was I was I was in I was in kind of in heavy pressure but right now things are going well I'm in a vacation I do not have any pressure so I can talk as much as I want right as long as I I'll just make you poor today so I'll do what I'll go back to my my college class okay so right now I want to you know define bean by using at component annotation so let's say add component here head component and he always say let me give it a bean ID as a college let's not give any bean idea because we know about default bean ID right if I'm not writing any bean ID over here then the by default the ID will be college right as six a small letter okay control G okay there you go so right now let's let's activate the component scanning in in over here College config class let's do it component scanning eight components can I'm sorry that components can let's put the best package sis over here and let's keep the package says let me maximize it okay right now my package C is calm dot selenium X phrase dot college so this is my package name right to make sure this is my Pakistan okay my component scanning is activated right now so right now let me okay now you have to tell me right so right now I have activated my components can write so whenever spring is going to scan this particular package how many objects spring is going to create so in your over call is we have bitten at component right so spring is going to create the object for college right but is that enough for us here we are dealing with so many objects in this college we need the principal object as well in this car in this in this College we need also the object for the teacher right teacher agent of this book but we want the object for the math teacher which is the implementation class for that teacher interface right so here if I'm going to go back to my you know client or Java right now okay this is college pin let me give the bean name as this let me go back to my client or Java let me change it to this and I want to make it a small letter and there you go now if I am going to run it is not going to run at all okay now point at exception okay it is creating the college object because we have Depak we have defined the admin annotation before the college but for this for this we have not defined any bean we have not defined at component annotation before them okay and inside the college config also we have commented out of everything right so it's not creating object for this object for this as well okay so right now what I'm going to do here I'm going to go to my principal class so this this is this is the object we we want the object for the principal class as well so we want spring to create the object for this so we will annotate it by using the add component annotation so right now the spring is when spring is going to find whenever it is going to scan through the best package which is our college package so inside the college inside this particular package whatever the class which classes the eight component annotation before it it is going to create the object for that right now we are putting the add component annotation before the principal class so for this class also spring is going to create the object and also in our college class there is this teacher interface and this teacher interface if I am going to do what quick type hierarchy okay right now there is the implementation class called math teacher for this class also we need to write the component annotation because we want this object as well we want this object also because we want spring to create this particular object for math teacher and is also need to register that particular object in the I mean inside the application context which is our which is our what which is our container okay so right now let's do one thing let me come back to our there you know what client dot java okay let me run this application and we are going to get the same thing now to point out exception because right now okay spring is going to do what spring is creating the object for college okay for college is got the add component annotation it is going to create the object for that for for math teacher it is going to get the add component annotation it is going to create the object for that okay it is going to create the object for the principal as well but it is only creating the object it is not injecting okay to inject what we need to inject we need Toto add annotation okay so what we can do it ortho where do you remember it at whatever or you have already forgotten if you're forgotten was that okay I mean I have already done a video for it at Auto where annotation could you please go and watch that okay edit our annotation okay so right now we have it in the editor word annotation over here okay so right now for this principle okay spring is going to do what okay for if I am writing the eight or toward annotation before the field then I do not need the setter method do you remember it okay for now let's not write the eight or to add over here let me write it before the setter method okay this is for what this is for principle where is principle setter method here okay cut aware so that spring is going to you know find the object for the principle and it is going to insert the object to this and also we are going to write the edit our annotation before the teacher also write so at Auto add could be could be used before the class and before the interface variable right or before the interface setter method as well okay so right now we are using a data where to inject the object to this end to this so right now let me go back to my client or Java and let me run this application and hopefully it is going to run and it is running fine but please do not forget about the auto wearing I am NOT going to repeat the concept over here right don't be confused if you do not remember it please go and watch my previous video about it away okay okay okay good right now I know you remember it guys I know I have checked your assignment and you are doing really really well even more better than me whenever I was learning spring I was not that smart like you right I'm seriously saying this you are learning very well congratulation I'm really proud of that okay so right now you know the next thing that I'm going to do is okay the editor while I'm writing over here okay guys can I move the editor where to this level okay for the teacher I'm moving the editor wet before the field and for this also I'm going to move the editor where before the field here and I do not need this setter if I am writing the waterwear before the field I do not need the setter method right so if I'm going to run this application client or Java it is controlled it is going to work as expected I think you remember it right okay good so right now the next thing that I want let's say over here I need I have a string type variable let's say a private string okay another dependency let's say I call its name okay let's take all its name okay right now this particular dependency I want to insect by using an annotation okay because right now I do not have any XML file right I'm doing everything by using the annotation so the way I'm going to do it I'm going to do it by using the ate value annotation I add value where is that at value okay there you go and here I'm going to put the college name unless the college name is selenium Express selenium expressed I'm sorry for that noise my the power actually is gone and come back my SD card on done I'm sorry if you're getting some annoying noise okay but I'm feeling really good that I'm talking to you right now because for the last few days I was so busy I was not having enough time okay so this is the way that we are actually need to inject the value right and we understood in the last video how to use the add value annotation and if I am using the add value annotation before the ferry name I do not need a setter method also right okay so right now let me log this College name also over here let's say this out let's say my college name page okay let me maximize it okay my college name is plus college name let me give some white space here to stretch out the things okay so do college name okay looking good I'm just logging this College name over here inside this test method right so right now let's go back so this is the add value annotation and value annotation is used to inject the value for the literals for the string for or the integer and for all the stops right okay so now let me go back to the client of Java and over here I'm going to run this application and okay so my college name is selenium x-rays cool you are getting it you are getting it right so right now I don't want to use the things over here by hard coding in the last video where you understood about the property right how to how to load values from a properties file or a property file so I'm going to do what I'm going to create a new property file so I'm going to do what I'm going to do what I'm going to do a deal and I'm going to go to other I'm going to go to file okay and I'm going to create a new file called si college - info dot properties okay hopefully the Spelling's are correct properties unless hit finish here and here let's say I am going to give my college name age let me maximize it let me give the college name as let's say college dot name equal to let's say selenium josslyn's those selenium x-rays right now this is this is how we need to define the name value pair this is the value this is the name pair right I need to copy the name I need to go back to my college - Java and over here where there hard coding the value I need to lower my dynamic tell you by putting a dollar symbol over here and going to face the name over here okay now if I am going to come back to my client Java they come later on this application I am sure it is not going to wrong it is going to come like this so how to get the exact value for that so to get the exact value I need to define where it should be my property file is present I told you in the last tutorial but but this time we do not have a will not have a configuration file but we have a configuration configuration class right we do not have a configuration XML file but we didn't we do have a config class and here we need to use the annotation called property ok this with this one properties source and property sources if we have multiple property file then we can use property sources but here I have only one so I am going to use this one so this one will be talking about that later right same as this so I'm going to use this one okay and Here I am going to define the factory the name ok let's define the name here so the name we are going to define like this property file is readiness at the class path in the name of the property file so this is present inside the SRC inside the class path and the name of this property file is college college oops college info okay there we go let me go back to my language Java okay let me run this particular program oops file not found okay probably I have spelled that out wrong I'm going to do what right-click go to the source ok so record rename going to copy this thing because I don't trust my spelling and I'm going to minimize it and I'll go back to my college config so here I'm going to change the name of the file and hope you say that was wrong so I'll go back to my client or Java minimize this as well okay now let me run this class ok those selenium Express but why I'm getting it inside a double quote because I must have given in double quote over here yep we do not want it this is not Java right this is the property file ok so let me run it again we go the college name is the selenium Express call it you call it how to use property file load value from the property file and this time we are loading everything we are giving the instruction inside the config file by providing this particular annotation called add property source looking good right okay okay so right now let's learn about at the qualifier annotation I know your lit is not about it whenever we are talking about a tower where we also learned about it Poliquin rotation but at that time we are defining everything you know inside the configuration file which is our bin start XML before which is our XML best approach that we are following at the time but right now let's say this particular interface teacher right now if I show the quick type hierarchy it has only one implementation right so this why were using a data where and for that particular teacher interface or whatever the you know the class implementation class that we have for this particular class you know the object got created because we use head component annotation before it and we are using ENTER to our notation before the you know before this particular type to inject that particular object right okay that's fine because we have only one implementation class right now but what if we have multiple implementation classes for example right now I am going to copy this math teacher class and I'm going to paste it over here and let's say right now this is science student science teacher right and right now let me maximize this science teacher class first our science teacher Java let me open this class let me maximize sorry okay maximize it and let's say a science teacher implements teacher and we have provided each component adaptation before 8 and let's say I am your science teacher of and my name is the scientific name is Tony because because 20 is also one of the greatest captain in India team and we are talking were so love is our meta shirt on say Tony's and Stitch the next time if we have any limitation class we can say couple death or do not go holy ok so right now let's say right now we are doing Auto we're over here but right now if we are going to see the hierarchy for this teacher we can find there is two implementation one is my teacher on a science teacher so right now this is my science teacher class and also we have annotated it with at component that means spring is also going to create the object for this whenever it is going to do the component scanning right because we it is going to find this add component annotation before this particular class called science station ok so right now what we are doing over here we are doing enter the word for it just fine but if I am going to run this particular application do you think this particular object it is going to insert it to this particular variable it is going to be view manager or it is going to be new institution because there is two implementation right so which one is going to be then right you are getting my point so if you already understood my earlier videos in our in eight of the waiting time then you you are most probably predicting it that okay right now we are going to get it whoever to get it accepts right okay so right now I come back to my planner Java class and I'm going to Don this particular application then okay there you go so if I go to the exact definition it is going to say okay expected a signal matching mean okay okay here we go expected a single missing beam but found to my teacher and science teacher so actually there are two implementation reject for the right one is meant to show on his side stitches so which wanting what we'd use you want to use the math teacher one what do you want to use the science station one so in that particular case we need to viewers at qualifier notation but before that okay you know about that qualifier of notation I know but before that right this annotation you do not improbably I haven't told you it so if I'll go to this teacher right now this teacher is to implementation right click with type hierarchy of it has to implementation let's say I have two three or ten implementation but I want one particular implementation to be used as a primary injection at that time what I can do okay let's say for this teacher I want this math teacher to be injected right if I have multiple you know multiple implementation I want what I want to pick one particular implementation and I want to exact that then I can use it primary it primary annotation okay this particular animation head private so right now this doesn't matter how many limitation you have you might have 20 limitation but you are making this implementation it's the primary okay that means right now I forgot to my client or Java and if I'm gonna run this then my math teacher method will be get caught you can see this okay there you go the meta should be get caught because that's the primary right so now let's say if you want to use the science teacher okay if you want to use this particular implementation class method or you want to use any of the other implementation class method at the time what you can do so we know about it right so we can use it for this at qualifier notation right it qualifier and our here you need to provide the p9 right now which particular pin you want to use you want to use the math teacher or you want to use the science station if we bring it to this place okay let me bring it to this place going to use this or you want to use this okay let's say I want to use the science station right so for this what is the beam ID same as the classroom right because we are not providing anything over here so what I'm going to do I'm going to come over here and I'm going to push it over here and I'm going to make this as small ready right and let me run my client application and I'm sure this time is going to call the science teacher in saying hi I'm a science teacher in my name is Tony right got it oh it works okay don't be confuses the a primary ed primary is something to make it prime it okay but if you have 100 if you have 100 you know implementation you want to pick one then you can use stuff at qualifier augmentation is it fear even if even if Yuma if I'm going to comment if I go to my math teacher if I'm going to comment this primary also right at that time also right now we have two different implementation math teacher and science teacher but over here inside the college class I am using a qualifier in science teacher I'm giving this ID this benighted to here so if I am going to run my client third Java it is going to obviously called the science teacher method because we are using this at qualifier notation simple okay that's it so okay one more thing okay before I wrap up right now I'm going to wrap up this particular session because they're all so few are it isn't spending but we'll be talking about it whenever I will be completing the bin lifecycle and also the defense scope of the beam like single down prototype you know the stuff right then we can talk about some more annotations but before that let's say if I'm not providing the value over here for this particular College name I am not providing any belly so this College name would be not right so whenever I am going to tab over here this will be not right because I'm commenting this out right so my client or server I'm gonna running this application okay this is coming us now but I told you already if I not want this thing to be now if this is not a dummy I don't want my object to be created I've not want my father's object to be created then I can make it record right I can make it in record but but I think it required is not allowed in this position let's check it out hit record okay I think it is not allowed yep so for this I think if I can use this thing over here then I can do what for this college name I'm going to generate the one setter so source generate getters and setters I'm going to generate only the saturn's and I'm going to hit OK okay so here it is my setter I'm going to move this record to before the second okay there we go okay let me cut this thing and let me put this thing after this because this doesn't looks good okay nice that's fine okay so right now this at record is present before this that college name right so if this is present right now so if I am NOT putting any value to this particular field then it is going to give me an exception that hey this is the quad you have to provide the value for this right so I can do what if I go to climate Java if I'm going to run this application I just get an exception over here yeah there you go so now it is saying property colors name is record for the P call it too in order to create the college feel you need in order to create the college of check you have to give the value for college name right so I can do what I can this uncomment it okay and right now if I'm going to draw my client application you so draw it expected okay error creating being this this stays colors name is required for this but I am providing the college name probably is setting to move it out to displace let's take this out now let me go to my client West Java and let me know in this application and you should run right now yeah there you go right so right now this particular value is coming from my property file whatever stopped today right I'm gonna tired of talking it's like in that world so I talk too much so right now I'm wrapping this session over here okay if you have any question any doubt you can comment that below and also I can write a mail to me at selenium explains at the rate gmail.com or I can also ping meet my facebook page called Facebook / selenium X fritz okay so I'll be I'll be coming back with a new tutorial probably I've been talking about the different bin scope that we help like single dong prototype and all these things in the next video and we'll be talking about some more annotations as well so it's already a long session for you and for me as well they are set for today's tutorial thank you very much have some so it's because it's Diwali time thank you very much for watching it and I'll see you the next video till then bye bye Cody [Music]
Info
Channel: Selenium Express
Views: 205,382
Rating: 4.9003444 out of 5
Keywords: spring framework annotations explained, spring core annotations, spring annotation tutorial, spring annotation based configuration, spring framework tutorial for beginners with examples, @component, @bean, @autowired, spring important annotations, @qualifier and @autowired, spring framework video, selenium express, spring by selenium express, spring framework tutorial for beginners with examples in eclipse, @primary and @qualifier, @value, @required, @configuration, spring bean, abhilash
Id: x_-Tx6OFYLg
Channel Id: undefined
Length: 95min 37sec (5737 seconds)
Published: Tue Dec 18 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.