How to achieve loose coupling with spring || spring dependency injection - Lets practice together

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so welcome to the episode number four of spring cold framework tutorial series so in the earlier videos we understood about loose coupling and we understood that spring supports loose coupling and in the last video where you understood that with spring supports dependency injection which is one of the most important feature of Spring Framework right so two of the most important feature if you talk about spring IOC then we can say it's helped us to do the dependency injection and in the same time it helped us to achieve loose coupling so in this video this video is going to be a little special why because we are going to learn how to use spring in a enterprise-level applications so the loose coupling and the dependency injection I'm going to pull everything together in this video and going to create a simple little funny kind of up and that will make you feel better and you will understand that okay this is how we can achieve loose coupling in spring okay and we will understand okay how to do that right and yes before we go ahead with this tutorial there is one more thing that I want to say you stay tuned with me till the video end right because I'm going to give you a simple assignment right that you are going to do that and you are going to stand that to me right and once you are done with that we can go ahead with some advanced level concept like Auto wearing and all the stuffs right but this video is going to be really really important whatever that we are understanding from the last four videos we are going to wrap everything within this single video and you know we'll see okay how it works [Music] you so as usual I have created a very simple project here called spring loose coupling okay and I have done some basic thing over here so that I will not waste your time all right so if I go into this project you can see I do have a folder called SRC I have created a package here called com'd or selenium expressed or di and I do have few classes and I do have a bender XML file okay so before you worry about this classes then bin that XML file I just want to tell you these are the same classes right that this is these are the same files that we have developed in the last video which is a dependence injection object type this project we have created in the last video right so these are some basic classes that I do have so now let me give you a recap that what we did in the last video so that if anybody is watching this video for the first time then he also can follow this ok so what I can do right now I'm going to create a sorry I am going to open this class called student or Java and let me maximize this class and I'm going to zoom it a little there you go alright so this is the same class that we have created in the last video right so we have the classical student let's say this student is me I am a dull student I don't study so I depend on sitting so this student is me and I do have a dependency my dependency is you know I depend on match it ok so tomorrow is my exam and you know I just want to do the cheating and tomorrow I have the math exam so I have created a math cheat class over here so if I open this math class so you can see there is a class called match it and this particular class right now does have a method called match it so this particular match it method does nothing I mean I just look something over here called math sitting started as simple sister which prints match shooting started that's it math class it has a method called match it simple that's it right so now coming back to the student class so here what I did I just add a simple in our dependency here called match it and this particular Matchett object I am in I'm just injecting this particular Matchett object by using a setter injection right and now let's talk about this particular method called cheating and this sitting method does what is just called the math cheat method of match cheat class so you can see I am using the Matthews reference and using the method method reference I am calling the math cheat class method which has matched it as simple as that right so in my student class I have a dependency called match it this is the one thing I want you to remember because I am going to modify it so much as we go progress with this tutorial right so how many dependency that we have inside the class one dependency what is the dependency name my dependency name is match it simple and what kind of injection that I am doing over here set our constructor it is a setter injection right and that's it so right now let's up on the bin start xml file and let's see how i have configured these things right okay so now if you'll see this beam configuration then can you tell me how many objects that I do need to configure over here inside this beam and B so if you see in the student class then first I need to create the object for student and also I need to create the object for Matchett so there are two different classes there are two different objects that we are dealing with over here in the student class the first class that we're dealing with is called student class then we do have also a inner being present over here called match it okay clear right so we are configuring it inside the bin store XML file so first of all we are creating the student class object so here you can see I have given it an ID called s tu and this is the class name student and then this particular student has a property called match it so this is the property that I am configuring it over here inside this bean and bean and this property is not a normal property this is a class property right this is a reference type so I've given it a name and I'm also giving it a reference that hey math cheat object value this is actually the actual object that you need to reference to so if you don't understand this what's my last video so this is the thing that I have discussed in the last video right so I don't want to you know explore more with this because these things we have already discussed and I don't want to bore you guys so this is the quick recap from the last video so I think right now the things looks very simple to you because if you have already following me for the last four videos this will be very very simple thing for you but okay Samms is asking you one question I know you can answer it why I have written it property over here I know you have already answered me you said hey Aviles don't bore me I already know you told me so many times that I do have a setter method over here and I want to do the injection by using the setter method that's why I have written it property because I want to do a setter injection so as simple as that and that's it right so make sure that you understand these things right before you go ahead so this is nothing right I am just creating the math stewed object over here because my student class need a match cheat object as my student class is dependent on am a cheat object right so here I've created a end of the bean I have created another object over here and the object ID is math should object value and this particular object value I am just putting it over here inside this wrap attribute because the reason why I'm putting it over here I've already told you in the last video I don't want to repeat it again right so match it is the dependency and this is the ref to this particular object you know which I have configured it over here so as simple as that right now I can go ahead all good okay so let's go ahead so right now okay now now lest the show begin okay so now let's say so I I had my math exam already so I have written my math exam I have done the cheating this particular method works fine for me I just went to exam I just turned on this particular method I just call this particular method this particular method on the whole shooting for me right okay that's done tomorrow or less a day after tomorrow again I do have a science exam and I'm a dull student right I don't I don't study so I don't know science also so if I don't know science also then again I do need to do the cheating right so if I need to do the sitting again then I need to create a class for science state okay so let's create a class here called science state again so because I'm done with the match it right now I do have a science exam science and let's do a finish over here and there you go alright so we have successfully created a science class right so right now we are going to create a method and this particular method is specific to science it and that right public void science chit and there you go alright ok so there you go so I do have the sign state class ready right now in my science ed class I do have a very simple method called science state and this particular science state method actually going to do the fitting job for me ok so if I come back to the student dot Java file now instead of the matchy to object I want to deal with the science state class right now I just want to do the science sitting right now so how can I do it so here in the math cheat class we want to call this particular metric method that's why we have created the object or the reference over here for the Matchett but right now I don't want to do this I want to do this I want to do the science sitting so how can I call this particular method now I know you already guessed it so you are going to say that ok oval a so right now I am going to remove this I'm going to remove the line number 5 over here and I'm going to change the match it to sign straight right so you are going to do something like this try something like private sign state and let's say science and there you go anyone want to remove this right we are going to tell that but wait that is attacks for you I am NOT going to you know I'm not going to let you do that so my requirement is I am NOT going to touch this class right this class coding has done I'm not going to touch this so this particular class need to support for the match it for the science set tomorrow did less a day after tomorrow again I'll have the History exam it should support the history cheat as well so how this particular variable is going to support all kind of checked now that's what you are going to answer to me it's very simple guys think like right now this particular variable this is Matchett variable right so this variable is of what type math cheat type so variables are just like pockets right just like a pocket in the short let's think like okay I do have the math exam esterday right so this is my shot I do have a pocket here in that pocket I have taken the match it but wait my exam is done right now I want to take the science state so in this pocket right now if I am putting the science state it is not going to support it so how does it feel so just to carry the science state do I need to create do I need to you know go and purchase a new shot because I want a new pocket here just like that this particular variable right now only going to support the match it object it is not going to support the science it object can I do equal to new a science state is impossible right it is going to give you compliation error right because it is only going to support match it object right simple this is not going giving any compilation error because this match it of match it type there spell it right we know about the object creation so what I need to do so that my pocket is going to support every kind of I can I can pit match it into my pocket I can fit a science state into my pocket you understand what I want to say so my variable need to support you know any kind of implementation right so what I can do you need to guess it and you have guessed it right I need to take the help of interface right because directly if I am putting the implementation class reference over here it is not going to work for all the implementation right so we need to think for a solution and the solution is take the interface variable right use the interface variable over here and any kind of implementation you want you can you can add that particular object to this particular reference right now we are going to see how to do that right now okay okay so before we go to all those stuffs and mess mess with all the stuffs first I want to do do a basic thing over here I just want to see that you guys are on the truck right now and you guys can follow whatever I am saying so you just need to make me believe that you are on the truck so first of all can you do me a small favor I am just removing this line number five over here okay so first of all let's think simple so I am also removing this setter method over here so I want to inject this science state object over here inside the student class so that I can use this science state method so how can I do it it's very simple right so before we go for those interface and all the stuffs let's do it first let's simply call this particular method okay into this particular student or java class let's see we are able to do that thing first or not okay so how to call this particular method over here to call this particular method we need the object of science state so what I'm going to do over here is first of all I need to create a private variable and science treat is my class name science shaped and I'll write and there we go if I want to use the science state plus I need to remove that older variable that I used to head for match it I need I need to have a different cetera method I had to remove the previous setter method that I had and again in this particular method also and I just need to make changes because right now it is giving me an error because this is the old reference that we had so right now we need to update it to the new reference and we need to call the you know what our science check method so what is the method name of our science state science state right so I'm going to do science there you go I am done so I'm going to do control a and control shift F to resize my code and I will do control s so there we go so can you see the problem here so just for a new implementation to my student class a new thing I want to implement or or a new thing I want to get in my student class I need to do I need to make all this changes right this is not good so a lot of lot of code changes right this is not good nobody is going to do that in the real time application right so that's why we're going to use the interface reference just after some time right but first of all let's stick into this first of all I want you to configure this thing okay in the XML file so can you guide me to do that so in the bins dot XML file right now I do have a new bean I need to create so let me do a control C on this and okay so right now let's say let me change the math to science okay science state object value and the class for this is going to be right click on science science to eight class click on copy qualified name come back to bean start XML file and remove this thing and update with the latest copied stuff so command V and I got my science state fully qualified path name so right now my student class this is my Plus beam okay this is my student class object here I am creating and for this object right now what is my property name my property name is this I'm going to come and see it over here I am going to come over here to the bin stored XML file going to do a command V over here and I'm going to remove this reference and I'm going to update this reference with the sign state you know reference over here so Rafik you'll to this so this is holding the actual sign state object right so over here you can see with this particular with this particular ID sign state object value I am configuring the sign state class right as simple as that so right now if I do a command S so this is actually not required right now but let it be over here right if I go to it go to my client or Java class right now because I want to run this application I want to see this that this particular method is getting called or not this particular method is that if this particular method is going to work fine then I can assume that okay my three-times state object is getting injected so that this particular method is working fine right so what I can do right now I can go to my client or Java class and inside this application context my bean file name is correct bin store XML and this is also being stored XML and over here I have done a simple system just to make sure that my bean stirred XML file is getting loaded perfectly just to log something over here and after that Here I am calling the GATT beam method passing the ID so what is the ID for my student s T you might so that's why I have written s tu over here and I also want the object for student dot class right so this is what I can't figure out over here this is going to give me back my student object I got my student object and I want to call the student class cheating method right so once I got the student object over here over that particular object I'm calling the cheating method now what the method is going to do this cheating method is going to take me to the science ship method so I do command click over here Here I am I'm inside the science jet class right so this particular method is going to be called and this particular line will be get printed into the console so let me first do that so if I go to a client or Java class and if I do run this particular program you can see my container is getting started my bins dot XML file is getting loaded because you know this what the log that I printed over there and my science sitting is getting started right so this is simple dependency injection concept right so we are good in this right so right now we can go ahead and we can do something over here in the student class so that we will not do the code changes again and again based on our implementation right so my student class right now need to support for any kind of cheat he can take any kind of cheat right so he can take history Jade science state whatever but he don't need to change this class right we need to do something over here and we are going to take the help of interface because interface help us to achieve the loose coupling in Java right so let's do it right now we are going to do something of rehearing to this particular student class so that right now a student class this particular reference is going to support any kind of implementation and for that rather taking a implementation reference we are going to take an interface reference and that's what we are going to do next why don't I create an interface something like okay this would be good right so think like I have an interface called sheet so this is a new trick to create an interface I hope you all didn't know it so chit so there is nothing present something called sheet right so it is actively saying me create a class called sheet create a interface called shape I said hey create a interface your call sheet and here I'm going to say is a modifier is public and there you go everything looks good hit finish and I got a interface cool right so I know that any kind of cheating the student is going to tech or I am going to tech there should be a functionality that that I should have a method called cheat okay that cheat method if it is if it is math class to it method it should do math shooting it is science class to it method you should do a science jetting but one thing is coming there should be a cheap method present right so here what I what I'm going to do I'm going to create a method called public void right and this is the abstract method obviously right so right now what I'm going to do all my implementation class that I do have like match it all science it I'll go to the match cheat I'll do implements cheat interface right implements cheat interface there we go so right now it is going to give me an error anybody guess why you are very right because in the interface whatever to tease our interface right so in the inside the interface whatever the methods that we have if our class is implementing that interface it should override the state method right so I'll go to my math you don't you have a class which implements the chit interface or what I will do I will now change this method match it name to chipped there you go the error is gone yeah well and I will do the same thing with the science state I'll change first of all I will implement my science state class is also implements the chit interface there you go and right now straight away it is going to give me an error because it is saying me add unimplemented methods so what I'm going to do I'm going to change this method name to chit there we go and this is done all right okay so now that looks good okay so we have two different classes right now which is implementing our base interface right so that we can make sure that this cheat has the interface called sorry this cheat has the method called cheat means all the classes which is going to implement state interface they must need to provide the cheat method so right now that's what I say the actual programming so right now if I come to student at Java right now I'm going to remove this thing okay I'm going to remove this thing for for a moment and right now what I'm going to do I'm going to remove this setter method right now and just to confirm that this cheat is right now the interface variable this cheat is the interface if I do command click this is an interface right so right now what I'm going to do I'm going to generate the setters setter method for that there we go I do have a setter method generated by the Eclipse I'll do command a control shift F so that my code will look good right now and right now simply inside this particular method called shipping what I will do I'll do this I'll do chip ok cheat is my interface reference and I will call the interface cheat method right so right now this with sheet method is going to be executed right that is going to be decided in the runtime based on the object that we are passing if you are passing so right now this cheat method is present where it is present inside the interface right so right now if I will do the open implementation then it is saying ok there are two implementation present match it and this is formatted implementation if I'll go back here and if I'll do a ctrl click over here if I see a poor implementation again it is saying okay there is two different implementation is available for the cheat class one is Matchett implementation one is science it imporant implementation if I'll click the sign state it will take me to the science state class and there is a chip method over here as well so based on our requirement okay based on what cheat we are going to do right now based on our requirement the compiler oh sorry the JVM is going to decide ok this one this is the one method to run run I do I need to run the cute method of science eight class or do I need to run the suit method of master class or do I need to run the cute method of any other cheat class that is going to be decided by the JPM based on the runtime object that we are passing and we are going to see right now how we're going to do that right so right now that's that's looking good if I come back to my student does Java class right now so there you go so right now everything looks good right now in my student class I don't need to touch it anymore you know from this particular point and I'm going to show you that house so first of all let's say I'm going to go to my bin start xml files so where is it ok so right now you tell me I do have two different sheet of available right there are two different sheet object available over here two different beam that I have already configured one beam is for the match suit one beam is for the sign state right so in my student class what you need okay what exam you have so let's say this Allah said tomorrow I have the math exam okay no problem I'll come to my bin start xml file i'll say the property name each it obviously this this name will be always going to be cheat only right now what is the sorry Binstead xml file so right now what is the value that we you are going to insect you are going to inject a match suit class value or you are going to inject the sign state class upset we subject you are going to insect let's say i am going to inject the sign state object until say it control v it over here to a control s okay come back to the client or java class I am NOT going to I haven't changed anything over here I'm going to run it so this class method will be get called science sitting right because this is something just like this right so see it's very simple in the Binstead XML file the runtime object is passing is science state science data objects value that means this is belongs to this selenium express dot d ID or sign state class the class name is sign state it is a sign state object so in the runtime you are passing the sign state object over here here the cheat has the sign state object so it is setting something like the estate's a take you to a new science ship okay so right now which particular method will be get called simple interface concept right the object is of science type class should call the science cheat method right this is science type method right this is just to make you understand so let me remove it okay if that would be match it it will call the match it method method right so let's do that if I will go to be installed XML file let's say my science it exam is done tomorrow I have the math exam so what I will do I'll take this match it value I'll just do inject it over here in the ref that's it if I'll go to the client or Java right now if I run this the math cheat is going to be get started you can see in the screen match sitting started right so right now our student is able to you know take any kind of this is a pocket right variables are pocket we are going to store some value in the pocket if I do have I don't have a pocket here right now we have created a pocket for our student and this pocket is right now support any kind of cheat he can take any kind of right now let's say I have in another class he has in another exam tomorrow or day after tomorrow or two days after let's say he'll have the Java exam this is Java shipped so Java exam means we need to prepare the Java right we are very good in that much probably not you are very good I know you never do sitting I also know that I will click finish here so okay so in the Java cheat is also going to implement our cheat interface so implements and again I have to implement the unimplemented methods and right now this method is going to let's say this out just out and let's say Java shifting started right so let's say two more of my student hit the Java exam so I don't need to do anything my pocket is going to support it I just need to configure it outside of the class right I am NOT going to touch this particular class I am NOT going to change the source score spring is fast because we don't need to change the source code we are just going to change the configuration so I will come to the configuration class and sorry configuration file and here I'm going to create a another bean because I need a new Java cheat right so I'm going to say instead of science I'm going to say Java cheat object value and the class that I have created right now the class name is Java cheat probably okay and there we go and inside my student a bean I so there is a dependency called cheat and this cheat I want the Java cheat object over here so what I'm going to do I'm going to go to my bin start XML file and I'm going to take the Java cheat of the value and I'm going to put it over here inside the Rex there you go right simple done right now our object will be injected by spring spring will do the dependency injection for us so if I'll go to my java sorry client or Java class again and if I'm going to run this particular application and you can see Java cheating is getting started right you understand the concept now but you can see the result over here right so I said Java chatting is getting started we are not harming our source code because our class is right now loosely copied right this is this is this is what we are doing lewis coupling here because we are taking the interface reference we're not taking the implementation class difference we are taking the interface reference is that clear I think it is very clear right so this is how spring is helping us to do the dependency injection in in Java so this approach guys it is very important you have to understand it I think you have already got it I'm right now I'm going to give you an assignment don't hate me but I'm going to give you an assignment you need to do what just a similar structure something like this and you need to create a you need to create your assignment sorry you have to do your assignment you need to show me that assignment you have to send me that particular assignment if you are following my course so if you are done with this guys the rest of the course is going to be nothing for you right you are done with dependency injection means you are done with one of the most important concept in spring and they are going to ask this type of questions very very I mean so many times you're going to get this type of question generative interview right it would be very simple if you if you are practiced it right so I really tell you one thing that you have to practice right just turn watch videos also do the practice I'm not saying that was my videos I'm saying what's anybody's videos if you are learning something implemented right right that code in the same day and programming is going to be very simple for you guys okay good space right so so what I'm going to do right now this is how the loose coupling need to be happened and right now I'm going to give you an assignment time right now you understand this I'm going to give you a similar program like this so you just need to do that right and once you do that guys life will be very easy because we are going to talk about Auto wearing annotations and all the stops and we are going to go like a bullet right because once you understand these things those concept will be super super super easy okay so first of all okay so what what is the assignment okay so think about a cellular company let us talk about Airtel right so if you are a Airtel customer or any kind of seem if you are using then you you might be getting various services suppose you will call hey I need the data service I need to oh I need to use internet right so they will activate the data service for you right if you say hey I don't want the data service I want the missed call alert surface then they'll activate the missed call alert service for you and if you'll say hey I want the hello to in service I don't want the missed call alert service then they'll activate the miss called sorry the hello doing service for you so there will be different services that our Airtel is providing to us right not I'll not say our Airtel okay so I am NOT advertising our tail over here okay and I haven't got a single penny from them as well right otherwise I'll get start getting comments okay you you are getting money from them not at all I'm not getting a single penny from anybody so okay so now let's say I I do have the class called I do have the interface called service okay and then that service I do have a method called service as well and I do have different implementation classes just like missed call alert service class hello doing service class then data service service class right so I do have different kind of service classes over here and all the service classes are implementing my best interface which is service interface and this service interface has a common method called service and that service method is being operated by every classes that I do have over here like mr. Cole alert or like hello dude or like the data service right very simple you got a clear picture so right now your job will be create a class called Airtel okay take the interface variable over there as the reference and do a setter injection or constructor injection edge of I mean whatever you are comfortable with then create a method called call service or activate service and call you know the service method of any of the service method of the implementation now you need to configure everything for this AR till class in the bin start xml file right you need to create different bins for the different services and you need to configure those bins with the Airtel bean and so that if I want to if I want to use the hello 10 service you should inject the hello to in service subject if I want to use the let's say the data surface you should inject the data service object right so that I will get the internet so can you please do a program like these and can and can say in that particular file - you know selenium Xpress at the rate gmail.com I'll just do it okay and send it to me I'll definitely going to check it out and I'm definitely going to reply you back right so do this assignment for you I'll just you know try to put the pictures of all this things that I've discussed you know over here over here as well so that you know you can get a clear picture that whatever the requirement that I am giving it to you and once you are done guys seriously where we are not going to stop after after this thing we are going to you know run like a tracing bullet and we are going to catch everything very very quick okay so do this configuration in the Binstead xml file try to do it the property property tact i'd try to use the constructor our tag I haven't covered the constructor our tag for the injecting the object type value but I have covered the constructor arc tag for injecting the literal value or for the primitive type value injection right you can get the concept from there and try to do all these things right okay so this is a simple requirement that I want to give it to you so you can do you know setter injection or you can do a constructor injection whatever you want but the requirement need to be fulfilled okay so do it zip this file send it to my email id okay and you are ready for the next tutorial where we are going to learn the auto wearing concept of spring and then we'll be jump into the annotation kind of stuff right so a whole lot of exciting stuff czar coming up I hope you guys are loving it I should be saying it I don't know okay you let me know you are loving it or not or I'm just boring you guys okay this let me know so that I can also improve so that's it guys so do let me know what do you think about this tutorial if you liked this tutorial do subscribe don't forget to do that so that you know we can make our family big as soon as we can and we'll get a good community over here to talk about coding and all the stuffs right so do subscribe do you like and let me know if you don't like this tutorial and tell me that okay what is wrong with this particular video Kay what do you want to see as the improvement all right that's it thank you very much for watching this and have a nice day bye [Music] [Music]
Info
Channel: Selenium Express
Views: 51,060
Rating: 4.9467454 out of 5
Keywords: spring 5 tutorial for beginners, spring framework complete tutorial, spring step by step tutorial for beginners, dependency injection in spring framework, spring important concepts, spring dependency injection example step by step, spring dependency injection xml example, spring loosely coupled example, loose coupling in spring, selenium express spring, spring by selenium express, spring, di, depency, injection, loose, coupling, injecting, object, spring xml configuration tutorial, abhilash
Id: EfSyyGpff5o
Channel Id: undefined
Length: 39min 29sec (2369 seconds)
Published: Sun Oct 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.