Setter & Constructor Injection in SPRING : Injecting literal values | DEPENDENCY injection in spring

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another video of spring tutorial series and today we'll be covering dependency injection in spring and to be more specific we are going to cover how to exact literal values by using setter injection and by using constructor injection in Spring Framework so by the end of this tutorial you are going to have a good hold on dependency injection you are going to know what is dependency injection well you know how it works and also we are going to understand what is actually setter injection is and what is actually constructor injection is and how we are going to use setter injection and constructor injection in spring so a lot of good things in the store I'm really really excited hope you are too so let's put some more effort and let's understand this concept in exciting way all right guys so before we get started with today's tutorial there is a small suggestion from my end if you are if you're here for the first time then first you have to watch this tutorial called spring IOC so I have already created a video for spring IOC I mean dependency injection of course comes under spring IOC but I have created another video for spring IOC already there's already available in my channel so if you want you can watch it the links you can find in the description so that's the best there's the fundamental and that you have to watch before you come and before you watch this tutorial alright but if you have already watched it then you are welcome my friend you are ready to learn dependency injection in spring right so so without wasting any time let's get started with dependency injection concept and let's go with our tutorial today [Music] hello I think you are not feeling boring you are not feeling sleepy you are perfectly fine and we can proceed with our tutorial we can do some coding I am NOT going to bore you anymore okay so I have just created a project here just a phrase project here called dependency injection and just created a package here called calm dot selenium expressed di just to save some time you know so what I'm going to do here right now so this this is the spring IOC project that we have created in the last tutorial so nothing we are going to do with this right now okay so let's come back to this particular project and in this particular package I'm going to create a new class and let's say I'm going to say my class name as student right and I'm going to hit finish alright so right now I got a class here called student student class and here let's say in this student class there is a dependency there is a property I want to set here let's say you know don't don't just get you know surprise you this terms right property is something that you are looking at from the day one you started learning Java right something like this let's say string name because each student will have a name let's say student name right to make it more readable student name right so there you go so this is the property of this particular class called student this is the dependency of this particular class called students so in this student class I got a dependency called student name so what I mean by that so when I want to create the object for a student class I should expect that that particular student should have an M that's it that's the dependency of the student class right there is a student means he should have a name so this student does have a dependency called student name right so right now how we can set this particular dependency how we can insert the value for this particular dependency of course we can do this of course we can do equals two and do hard-coding but we're not going to do it over here because hard-coding is bad right so we don't want to change the source code again and again for each student right so what I can do let's say there is a different class here called I'm going to create a new class let's say exam so let's say this is this exam it's a different class and let's say in this example as we have a main method and let me do a control space let me hit it here and there you go let's say this exam people right now one to access the student class variables they want to set the student class name I mean they want to set the student name value right so what what they can do simply you know they can create the student class subject student student equal to new student and there you go and you know in the student class we got this non-static variable so what we can do we can do a student which is my reference and student name and I can set it to something let's say of a large B all right there you go so right now if you see over here what I'm doing you know now now what I did this is this student name is what this is my dependency I'm setting the value for the student nameless the velocity and if I were to display let's let's create a display method let's say public void display name let's display info listen display student info and I want to open it and close it and I will do a simple sis out here and we'll say stood student name is and student name right there you go good so right now what I can do in the insert the student class if I want to print the student class now I mean the student name what I can do again this method display student info is present inside the student class so I use the reference variable call this particular method and if I'm going to run it I and see that yet what about the value that I am providing for my student has been injected to my dependency or to my property here right to this particular student name the value got injected called a velocity simple right well but you know what this is not a good programming approach why because we learnt about encapsulation right here what I'm doing I'm accessing this particular property or dependency or variable directly here inside a different class so to a different class I shouldn't expose my variables so what I could what I could do encapsulation concept I need to make it sicker so I'll put it private right so now this particular variable is only accessible inside this particular class but so here in this class I'm getting an error right this particular code is giving me an error because the student name is private and I cannot access it so how to how to expose right now suppose suppose somebody want to set the student name suppose somebody want to create a new student and he want to set this name of the student so how we could do because we are not providing the direct access to the variable so what we could do we can create a setter method so to create a certain measure what I can do I can do right click I don't want to do it manually I'll go to source I'll do generate getters and setters I want to create only the setter method so I'll check this box and I'll do select setters and okay there you go right so I got a setter method right now so in the using this particular method whatever that should random that I'm going to pass it over here it is going to set this particular student name to this student name this star should I name is this right so what I could do suppose now this particular line is invalid piece of code so let me remove it and this setter method is present again inside the student methods using the student variable I'm in student reference I can use set student name and what I can do here I can provide the student name here of the last be okay type it out my full name of the last bunny craw he and there you go but why I'm getting an error here for nothing the matter oh sorry is set student probably I type it wrong sait student name there you go and if I don't remove it now that's fine oops something oh I haven't save it there you go okay so let me save this particular program and there you go so I use the setter method here using the set of suppose right now I'm going to run this particular program I got my value so using this setter method right now what I'm doing I'm injecting the value for my dependency what is my dependency here my dependency name is student name so I'm injecting the value for my dependency called Aviles panagra he this is the value for the student and this is the this is the value that I'm injecting for my dependency using the setter method right so how did I inject the inject the value for my dependency using this setter method right that's it that's what we call a setter injection is right so using a setter method we're injecting the value for our dependency right but wait again there is a problem no there is there is no problem right but you know what we're learning spring we're not we don't need to do all these things manually right I told you in the last tutorial spring do a I'm in spring actually does a whole lot of things for us right so in the last tutorial we learned that spring is going to create the object for us and it is going to Menace the object inside the ioc container right this this look at this particular slide right now so in the last tutorial we understand spring inversion of control concept and what we learned here that spring is going to create the object for us spring is going to Menace our object inside the application context which is our ioc container spring is going to help us to make our application configurable we learnt it in the first tutorial and the last thing that we are going to learn today or we are learning today is managing dependency spring is going to manage our dependency we don't need to hard code it inside our class we are going to inject a value to our literals to our Nancy's right by using the spring dependency injection concept so the dependency injection concept is one of the most powerful concept the spring providers and this concept you know is coming under spring inversion of control right so let's learn it how we are going to use spring to inject our dependencies to our string best values or to our literals right so let's get started and let's go back to my Eclipse so alright so I don't need to do the hard coding over here I don't need to create the object by myself I will let spring to create the objects I'll delay this stuff and now this piece of code it doesn't make any sense let me delete it right so what I'm going to do right now okay so okay so to inject the value or all right so you remember in the last tutorial we created a bin start XML file okay so I'm going to copy this particular file because you know pick more time of years and in the classpath inside the SRC folder so I got the bin start XML file here you can copy that out from your last tutorial I mean from from the from your last you know project that you have created and here I'm going to remove the old stuffs here and I'm going to do some press coding inside this bins and bins right okay so what I'm going to do first here so my first herb is I will let spring know that hey spring create a object for me for the student class I don't want to manually create it you create the student class object and managed it inside your application context inside your container so whenever I'll ask for the student object you give me the student object right and whenever you are giving me the student object make sure this dependency got injected so right now our student class got a dependency called student name and whenever spring is going to create that particular object student student object we should make sure that spring is not only going to create the object for us also it is going to inject the dependency whatever the student class help right I'm in the student classes right now only one dependency called student name so whenever spring is going to the going to create the student class object it it should inject the value for the student name I should provide some value to spring and spring is going to inject that particular values right to my you know dependencies right okay so how can we configure that I mean just tell me that how spring is going to create the object for us so too if we want to pour spring to create the object we need to configure the pins here so bean is what being is something I mean the objects their spring container manages right so this beam I had to set it off here and first of all I need to give it an ID so what would be my ID my ID will be less say I'll make it student let's make a simple student and then with class this is form which class object we want to create this particular class I will do a copy qualified name I'll come back to here I'll paste it here because I'm so lazy I cannot type it out alright so right now this is the bean that I want spring to create this is the object that I want spring to create so so how can i how can i how can I know that spring is actually creating the object so so let's test exam dot Java class and let me let me okay so this is vegetable I think and first of all I need to create my container object so I'll do application context context equal to new class path application context gnu classpath application xml application know it better so here okay so it's just and there you go and so here i want spring to read so what is my configuration file my configuration net file name is bin start xml right i want spring to read it so I'll write beans dot XML beans dot XML file hey spring read this particular file whatever the objects got configured over their career that of a particular object okay and once you create that particular object give me that object I want to have that object right now current I don't want to create the object I want to get that particular beam and who's been I want who's been always been I configured there let me put a semicolon here let me come back to here what is my because Ari I don't need to open it okay bin star XML so my being ID is student so let me copy this out let me come back to examiner Java class let me put my bean ID over here and well this student be I'm in this student ID is belongs to his class this belongs to student class right so I need to specify it explicitly here so that I don't need to downcast I told you in the first tutorial cell right student dot class right and here right now this particular beam is going to give me the object of student cluster right student unless say the object name is okay okay so right now if I'll do what obvious my student reference so this particular object whenever I'm getting obviously this is the reference but the object this is this particular reference pointing to I should expect that this particular object has the value for my dependency right in this particular so obvious to the student class right so this is the student class reference so you think this student class reference I want to call that method called display student info right so what I'm expecting right now okay so let me run this particular method and let me check it out that what is the name of my now variable what is the what I mean what is the value of my dependencies right as of now I haven't injected yet all right so here if I run this particular method right now if I'll do a you know if I run this particular program let's take it out spring is going to spring I mean spring is getting started the container got loaded and then it is saying there student name is now why is that very simple because I haven't I haven't injected any dependences right and if I am NOT injecting any dependencies what is the default value of spring I was I'm sorry what is the default value of his string you know the student name is of string type and the default value for the string type is not because string is the class and for an object the default value for a object is no right so that's why you know whenever I am running this particular program I am getting null as a output because I have been injected any value for this student class dependency so right now right now I told you that whenever I am creating the student class object I should expect that spring is also going to inject value for the dependency but right now spring is not injecting anything because we are not telling spring that he'll spring inject something for us we are not telling spring anything right so how can we going I mean how can we are going to configure this particular thing okay so right now this is this is our class right so this is the claw I'm in the class for in spring for what the spring is going to create the object and for this particular class when spring is going to create the object we are also going to tell spring that hey spring you do just don't create the object you also in Z is dependency so I'm going to put a space here I mean I'm going to hit enter here and here inside this pin and bean I mean inside here I'm going to let spring know that his spring whenever you are creating the object for this you make sure the dependency for this particular class called student there is a dependency called student name you inject this particular dependency right and how you're going to configure it okay so this student name is what this is the property this is the dependency right and how how actually you are setting this dependency previously used to use the setter method right so we are going to tell spring that hey spring we have a pregnancy call student name let me copy this particular dependency and I'm in this particular field and if I do Binsted xml file and here i'm going to configure let me zoom it a bit okay here i'm going to configure so this is what this is a property this is the dependency and what is the name of the property the name of the property is student name right the name of the dependency is student name so we're going to let spring know what is doc what is the dependency that we have and obviously what is the value okay what is the value for the dependency right let me put it inside the double quote and there you go so for this particular property called student name you inject a value so you inject a value called Avinash Bonnie craw hey there we go great so and let me I mean let me do some you know let me end it over here so I don't need to write that particular extra attribute there all right so it's done okay so there we go we are all set so what we are telling spring that say spring for a class okay give it a reference called student so we can track that out by using this ID and then whenever you are creating this particular object make sure you are going to inject the property you are going to inject the value for this particular dependency call student name and the value is going to be oval ash of the last panic right right simple right so so what actually happens internally here okay so what is happening internally here how spring is going to set the dependency right what spring is going to do internally right to set my dependency student name a value called a velar spanning rate how it is going to inject this particular value to this particular student name let us understand it okay so I'm going to show you a couple of slight you know within a minute but let's let's start it from here right so when spring is going to create this object called you know for the for the class called student it is going to say see that okay there is a dependence it has called student name right and so whenever is you see that it has a dependency called student name and we are using this tag called property that means you should expect that okay for this property called would be a setter method retained by the developer right so for the student name spring is expecting that there should be a setter method present inside the student class which is by which is written by us and obviously we have already done it good right so we have a setter method present here so whenever we are configuring this particular property or this particular dependency dot xml file using the property tag then spring is going to internally look for the setter method for for this particular dependency for this particular property or for this particular field spring is expecting that there is a setter method called set student name should be present inside the student class and using that particular setter method here spring is going to inject a value called a velar whatever the value that we are providing over here spring is going to inject this value to the dependency called student name simple right so now whatever I told you I'll se it with a small animation all right all right guys so this is what we have discussed we have a bean stirred XML file inside the bin start xml file we have written this piece of code we created the beam we set a property for this particular beam we set the dependency in form of the property here and whenever our application context that means in whenever our iOS a container is going to read this bin start XML file whatever the beam we have configured here it is going to create the object for that and what is going to be the name of this particular object how we are going to track the subject we have given this a ID call student so we are going to track this particular object using this ID call student right and it is not only going to create the object it is also going to set the property set the dependency whatever we have inside our student class so inside of student class we got a property called student name and we are giving it a value called Aviles and there you go it is also going to set the value for the student name called Avinash so this is what happening and this is kind of a high level overview okay so now let's say whenever we are doing get bin and we are asking for the bean called student inside this application context there is a bean called student here and whenever we are specifying that inside the get bin method so what it is going to give give me it is going to give me that particular object called I mean whatever the object it has over here it is going to return me that particular object and this particular object is fully constructed right you just can take this object and can get all the value the spring set it for you right you don't need to set it explicitly okay so this is what happening internally we have our bin stored XML file and here what is happening it is internally creating the student class object so why it is why it is giving a reference called student because this is what we have given here and why does creating the student class object because this is what we said it over here right inside the class and now what is the name of the property the name of the property is student name so spring is internally going to look for the setter method for set student name and what is the value we are passing here the value we are passing is Avila so this is what it is going to pass inside the set student named method right and that's it so whenever your application context is reading this particular Binsted XML file it is just creating the fully constructed object for you called I know whatever the reference that you are giving it over here and also it is setting all the dependencies that you have the concept is done all right so now let's go back to eclipse and so we learnt that this is the thing which is happening internally alright so now let's go back to your clips alright so now let me guess this particular application let me check that this particular value got injected to my dependency or not ok so I'm coming back to examiner Java class so here again I am going to run this particular program so what I'm going to do right now I'm going to click on the Run button and there you go student name is of a lass panting crying cool isn't it spring is actually injecting the value for the dependency which is student name how spring is injecting using this configuration by looking at setter method why it is looking at setter method because we are using the property tag okay so there you go so okay so let me say right now if I have a different property over here inside the student class now what is going to happen okay so now let's say I got another property here cause private okay let me put it on the top of okay let me remove some spaces here and here let me say that there is another property here called int ID because o is student will have a ID and I want spring to inject this particular dependency so how can I do it first of all I need to create a setter method so how to create a setter method I'll do a right-click code a source this is not how to create a setter method this is how to be lazy and how to use shortcut right I want to create this hetero method for ID so I check that box I'll click on select Staters and okay don't say that a village is teaching me how to create a settlement setter method by using the Eclipse shortcut okay all right so you have to type it out by yourself you are a beginner if you are learning it for the first time if you don't know what is the setter method just type it out okay so right now so right now what I can do so okay so now I got another property called ID and I got the setter method for this so I'll go back to my spring dot I'm in bin start xml files and here I let spring know that hey spring whenever you are creating this particular object called student you do a work you just inject to property for me so I one property I have already set it out the another property let me set it over here let me say a property the name is ID right the name is ID oh yeah the name is ID here and here the value I'm going to set here let's say that the value is one my roll number is always two that's bad so what I can do okay so now I got I got the property for this ID the value is one right so what I can do right now I can check this out but before that here let me put an entry right so let me drag it down to this end okay let me put it out over here okay so here so here what I'm going to do I'm going to write some more code here inside the display method I can type here the student name is and the ID is and let me do a plus and let me call this variable collide e okay so this is what we have ID right so I just modified my display method because I want to track it out my ID got injected or not all right so right now let's go back to exam dot java class right okay so right now what I can do if I run this particular program and let's see there you go the name is herbalist panagra he and the ID is one ok then spring is doing everything for us isn't it spring is doing the dependency injection for us is injecting all our dependencies right suppose right now let's say if I have a another another student object I want to create then simply you can copy this out control see you can do a control V it over here and suppose this time you want to create their student object you want to give it a reference called Ashish and the value you want to insert for the property is called let's say a Shh be and the listen ID is 2 okay so right now how can we track this particular object how can we how can we track this particular beam so what I can do right now I can go to my exam dot Java file and here I can I can again do a context dot get beam I want to get the beam for the ID what is the ID the ID is ahsha's right so let me copy this out let me come back to here let me paste it over here and and this ID belongs to the class called student class and this is going to return me the student class object obviously I'm mentioning student should enter class it is going to give me the student class object and the reference variable let me give it as Ashish ok and what I can do right now I'd lo haces dot gets to sorry display student info there you go let's take this out if I run days and there you go student name is Isis be end ok so let me give it a space here and the student of Java here there is no space if I will run it again now I can see it here ok I see the name is us is B and the idea is do all right simple you'll learn dependence index and already congratulation so right now let's say if I'm going to remove okay if I'm going to remove the setter method from here what is going to happen if I remove the setter method what is going to happen does my program is going to run or I'm going to get a error can you predict it so spring is internally using the setter method to inject my dependencies because I'm using the property tag here I'm using the property tag here right using this tag spring is actually injecting the value for my upset right for I mean my object dependencies so if I'm going to run it I've already removed the setter method already from my class if I'm going to go to the example Java file and if I'm going to run it make sure but it is going to give you an error and what is the error the error is been creates an exception it is going to say error creating the bin name with student because there's tidy there's the bean ID we have given so it is saying error creating the bean with the name called student defined in the bin dot XML file in the bin dot XML file whatever the name whatever the ID that we have given it it cannot is not able to create the bean cause student whatever because this is the ID that we have defined in inside the pin store XML file and it cannot create the beam because of the regime error setting the property values it cannot set the property I mean values for our property this is the exception that we are getting because invalid property student name of bean class bean property student name is not writable okay the student the property student name is not writable and why it's not writable because there is a invalid setter method bridge in there that means does the pass is saying it is asking you does the parameter of the setter method match with the return type I mean it is not finding the setter method right if it is not finding the setter method we got to know that internally it is using what it is using the setter method here to create the I mean to inject the dependencies if you want you can log it over here you can do a cyst out here you can say a setter method called okay we can give another let's say set ID and let me copy this out control C and I'll do here command V and here I'll say certain method cause this should track it out right inside the student class in such a setter method I just created I'm the abscess added a system dot out dot println right now if I go to the examiner Java class and if I'll do run this particular program what we can break up our value is getting injected and our setter method got called for set name and set ID this new setter method got called whenever is injecting the value for the first time again whenever we're injecting the value for the second time this to particular setter method got called that means we we are sure right now that spring is actually doing the dependency injection using the setter method whenever we are writing the property tag of here but is this the only way to inject the dependency no we do have another way and the way number two is constructor injection we can use the constructor to do the independence injection I looked at most time for that I'll just show you that now how we can do the constructor injection you know really really quick and in a really really easy way right okay so let's go for that so just give me five or six minute more so that I can wrap this tutorial out all right so right now we are done with the setter injection alright so make a note of it you okay so now let's quickly get started instructor injection let's go like a tracing bullet let's go super super fast and we can do it okay we can do it in the next ten minutes right within the next ten minutes we're going to learn what is a constructor injection and how spring is going to do the constructor injection for us okay so let's get started so the first thing that you need to do right now let's do some cleanup stops right so first let me go to the student does Java class and here if you talk about these things these are mine setter methods right previously I used my setter methods to inject the dependency but right now I don't need them I want a constructor to inject the value for my dependencies right okay so we're going to write that within a couple of minutes and right now if I go to the bin store XML file we configure two objects right here it is one and here is one to beam we configure let's remove one bin let's keep it simple okay and if I remove this particular pin if I go to exempt the Java for this uh she's I removed the respective in there let me remove it over here as well okay so we removed three of course right that what we don't need it over here right now if I go to student or Java class and here I can get started with my coding right okay first of all what I want over here I want these two values to inject by the help of a constructor that means using a constructor I'm going to inject these two values so how to write a constructor you know it better right so if I'll do a right click if I'll go to source generate a constructor using fails and I'm going to hit OK here and these are the two fields that I want to present in my constructor and there we go right so I have then rated a constructor here and over here I just need to God get rid of this super I don't want to confuse you and there we go simple there is a simple constructor that Eclipse has written for me right and the same thing I'm doing over here and why we need a constructor in Java we need a constructor in Java to initialize the know static variable of a class during the time of initialization during the time of object creation right so whenever we are creating an object right we can use this constructor okay we can do student student equal to new student and we can pass in a ID and student name and can initialize these two fills these two fills here with their value we can insect right and how we are going to do it using this right this dot ID caller ID this just student name equal to show their name you guys already know about it I don't want to bore you over here right so what actually the thing is instead writing a setter method and injecting the value for ID and student name here we are going to use and constructor to inject the value for this ID and student in it so normally tell me if you are going to do it normally how actually you are going to do it normally if I go to my exam de Java let's forget about this code for a moment let me come in this out file source ID block of comment and there you go so normally how we actually do it so I want to inject or value using an constructor so I'll do student or vehicle to a new student and I'll use this particular constructor I'll give it a ID called one and student name I'll give it let's say eyelash right and there you go so this one end of a lash will be injected to this two filled ID and student name by using this particular constructor right I don't need to tell you this you already know about it so using a constructor we're injecting the value for this particular fields and that's what the constructor injection is if I'm going to run this particular program I'm damn sure that okay so let me call that particular display method so this place to rent info there you go and if I run this yeah student name is over last and the ID is one how we're injecting this value over there using this particular constructor right using this student constructor that we have created inside our student class but wait again we are doing all these things manually right so we will let spring to do this work we let spring to call this particular constructor by itself and inject all these values right and how spring is going to do it right now let's talk about it right now and let's get started with constructor injection in Spring Framework so let me remove this particular code and remove this particular comment because we don't want to type it again and again we just want to learn the concept okay so nothing actually I don't have to do anything over here so the constructor I have already written right so I can go to my bean store XML file and here I have already created a bean right and in this bean we're using the property tag so property tag is used for setter injection but right now we're not going to do any setter injection right if I'm going to run this particular program the code that I have already written over here if I'm going to run this particular program it is going to give me error write again the same error bean creates an exception as you can see in my console if I run this particular program I'm getting bin creates an exception that means the bin is not getting created and the reason is very straightforward because here we're writing property property means spring is internally looking for a setter method called set student name and set ID inside the student class but if I go to my student class I don't have any setter method here instead our I'm writing and constructor to insect the value for the dependencies right so here I should let spring know that hey spring this is not a setter method you need to look for you need to look for a constructor right and so I need to just need to change this property to constructor on so there you go ok so now let me this is constructor Arg that means it should end with constructor arc but let me remove it and let me end it over here there you go you are all set you learnt constructor injection okay so what what is happening over here so whenever you are setting this particular beam you are telling spring that hey spring I have written a constructor which accept two argument one argument is student name and one argument is ID right so spring is internally going to look for an constructor here which has two arguments so do we have a constructor with two argument inside a student class yes we do have over here ID and student name and here we are saying for the student name you insert this particular value color balázs panic rahim and for the ID you insert this particular value called one right simple so if I if I come to example Java again and if I run this particular method then there you go your value not injected and how you insect it Spring has injected your value by using and constructor oh I'm getting so much of Facebook messages today here you can see the value got injected right so this is what we have done using the constructor way of injecting the literal values right just you need to remember we used a constructor arc over here okay so that's good we learned how the constructor injection works and string okay so now before we are going to do some hands-on on days I'm going to tell you a small thing so guys the older person of spring does not support this attribute called name right so if you're going to work on the spring persons before 3.0 probably you are not going to get this particular attribute called name over there so that previously it used to be a little complicated whenever is comes to constructor injection okay but don't worry we are working on latest person sub spring and we will not have any such kind of complication and I'll be covering also what are the complication that wish to have whenever we used to work in earlier versions of spring without using this name attribute here but for now let's forget about it let's talk about the simple thing okay so let's do some more coding with constructor injection right so if you if you talk about this student or Java class let's say I want to write it another constructor and let's say if I want to write a constructor which will take only one argument and we'll inject this particular field so how can I write it so I can simply write public student and inside the student constructor I can take only the ID and I can say this ID to the they stood ID right that's it so if I want to use this particular constructor it is only going to inject the ID right so now what I want to do let's say I have another student so I only know his ID I don't know his name so I'm going to use this particular constructor right so how can I do it so if i go to the exam dot java file out here if i go to bin start xml file here i need to you know create a different spring object right so first of all i don't want to write it again let me copy this fetch it over here let's say the student name is less at the leap there you go and i don't know the student name so let me remove it so i will have only one thing to configure here the ID and this is the constructor so i put it the tag here called constructor art so I'm telling spring over here hey spring whenever you create the object for the student make sure there is a dependency called ID and this particular dependency I'm going to inject it using a constructor we stack only one argument because I do have only one constructor Agro for you and the value for this particular ID I want to insert one here right so spring is going to do what when it is going to create this particular object and give it a reference called the leaf if you are going to track this particular object you are going to see only one particular dependency got injected because here we are calling the one argument type constructor so inside the inside the student of Java we have a constructor which takes only one argument right and here we are passing one argument so this particular constructor will be executed and for this particular object you are passing two values for the constructor so spring is internally going to look for a constructor which text to particular argument okay so now let's compute come to examine our Java file list - let's find out that particular student context dot K tidy sorry get beam and here I need to pass the beam which is to leave and student dot class because this particular ID called the leap belongs to belongs to the student class right so if I if I go to the exam that Java this belongs to the student class I need to put a semicolon and I need to take the reference student let's say the leaf n equal to and there you go oh I'm sorry ok so right now what I can do I can call the leap n dot display student info so if I call this display student info what will happen if you go to the display student info method it is tracking both the student name and the ID right but if you talk about this particular upset I'm only passing the ID that's so the name I'm not passing right so for the ID the value will be inserted and the name I am not passing that means this particular value will be by default in all right so I'll be getting IDs 1 and name is know so if I go to example Java file if I run this particular application there you go student name is null and ID is 1 and how I've injected this particular vehicle ID I did it by using the constructor injection I did the injection of literal values which is ID in a constructor way because I used the constructor art and internally split spring is using the constructor to inject the value for ID right simple now before I wrap up this session you might have one doubt right so if you talk about inside the pin start XML file I'm passing the value as 1 2 for the ID the value I'm passing is one and this one I'm passing it as the string format right but wait if you talk about my constructor here it is taking an int so how it is resolving it here I'm passing it as a string here it is accepting as the int so how actually the conversion happens that's the right question if you are thinking about it then that's a good question so if you talk about inside the Binstead xml file whenever i am passing this thing as a string spring is going to internally convert this particular value to in type right so this is a string so spring is intelligently going to convert it to in type right implicitly it is going to convert the ID which is string type to in type then it is going to pass it over here so spring is so intelligent buddy you need to trust spring okay so and it is calling this particular you know constructor because here we are passing one argument over here and inside the student class we do have only one argument type constructor okay so there you go but if you don't trust spring you want to say spring hey spring you don't need to use your intelligence I am saying the type and providing over here is integer type you can do it over here as well you can explicitly define the type that you are passing okay to this particular ID you know that to this particular ID the type is going to be int so this one is going to be in type so that's why you are explicitly providing the type you're letting spring know that this particular value that you are injecting over here this is of in type now spring you know but you know that okay the programmer want to inject the value one as in type so let's do it okay so spring is not going to else intelligence so much because you made the job easy for spring so if I run it so there you go you know you get your output so nothing changes but what about okay so now let's test this out if I make it let's say let's say long okay now what is going to happen so here it is taking in type right now I made it this particular type is long so long value cannot be stored inside the int value so if I go to exam dot Java and if I if I am going to run this particular application make sure you are going to get an exception right unsatisfied dependency exception right okay so we got exception over here and we'll be talking more about this particular exception in the coming tutorial so whenever you are defining the type by yourself be careful and let's bring now the exact type that you have there inside the constructor for here it says int so I made it int so for an example over here inside the constructor argument I am passing two particular values student name and ID so here if I want to define the tie by myself then this is not at all needed but if I want to do it in some cases it is needed in that bed some cases I am going to cover in the later tutorials okay but here let's say you know I want to define the type a myself so I can do it I can write this I can use this type attribute and this is the string type so I can put here Java dot long dot string because string is a class right and that's the type of this particular value this is a string right and for this one this ID is of n type so if I if I want to explicitly define the type of it then I can write it in type right and there you go so if I go to the examiners Java class again and if I'm going to run this particular application again there you go nothing census you caught your output but you are making spring stuff easy right so it is a good help it is kind of good okay if you if you always define your type it's a good programming practice because you know there is less chances of ambiguity problem okay so we'll be talking more about it in the in the coming tutorials okay so that's it congratulation you understood how to insert the literal values how to insect the literal values by using setter injection and constructor injection in spring way right so you are awesome you did it and congratulation for completing this particular session I really appreciate your effort and guys this is this is not everything that we learnt already about dependency injection but I told you right dependency could be in form of anything it could be in form of literals just like this and also it could be in firm up a objective might be it could be in firm of a objective type just like this right it's like private family or address you can say art treasures art right here assume that this address is a class so dependency could be in form of this and this is the most important thing that you have to learn in the next tutorial once you are done with this then we can go super fast right I can help you to learn Spring Framework in really in a very no faster way okay so we'll be doing it don't worry we're going to do everything over here and I'm looking forward to cover all the tutorials for spring spring core right so let's see how long I can go and thank you very much for your support for your motivations in the end the comment I'm getting a lot of positive comments so thank you very much for that guys and that's it okay so I'll see you in the next video but before I sign off I'll tell you one thing okay so if you if you come over here to the exam that Java class if I if I run this particular application is running fine right but wait let's say for this particular student Dileep okay this particular student called the leap I am only setting the ID right but if you talk about in my student class each student have the ID and student a student will have a student name so ID and student claim for a student is compulsory but wait what about if I'm only setting the ID if I'm not setting the student name then also you know I'm able to create the object right I'm able to create the object over here but only I'm getting the ID because this word I have inserted but the name is coming now what about if I want to restrict the user if I want to restrict some if I want to restrict the program he program if both the ID and name are not get set just like this if for the leap both the ID and name both are not inserted then don't create the object give me an exception if I want to do it if I want to do some validations then how can I do it okay can you can you figure it out by yourself I'm definitely going to cover it I'm just going to give you a task okay so that you can you know go to the internet and look for it okay so so that's it so that's what I'm going to cover in the next tutorial as well okay so that's it guys thank you very much for watching this tutorial I'll see you in the next video with me any queries any difficulties or any challenges if you're finding in your a while you were writing code using the setter injection or constructor injection put that comment in the comment box so that I can help you out or if you can help somebody over here who is having any problem you know you can you can visit to his comment and can help him resolve the issue that would be great as well we should be always ready to help right we have knowledge that means we should be helping now you have the knowledge and constructor injection and setter injection then you are ready to help somebody who doesn't know about constructor injection or setter injection okay so let's let's make it a goal let's put some comments okay if you're if you are having any problem if you're finding any challenges put that in inside the comment box and I'll definitely assist you to resolve that issue okay so thank you very much guys I'll see you in the next video [Music] [Music]
Info
Channel: Selenium Express
Views: 105,761
Rating: 4.956408 out of 5
Keywords: spring dependency injection example step by step, spring dependency injection xml example, spring dependency injection annotation spring setter injection, spring setter injection xml, spring tutorial for beginners with examples, spring by selenium express, dependency injection in spring framework, what is dependency injection in spring, spring dependency injection tutorial for beginners, spring ioc and dependency injection, spring coding tutorial, selenium express, spring di, spring
Id: F0oK42CwCuo
Channel Id: undefined
Length: 57min 57sec (3477 seconds)
Published: Fri Aug 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.