REAL-TIME: Spring Bean life cycle using @PostConstruct |@PreDestroy |init - method| destroy -method

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to the ninth episode of spring cold from tutorial series and today we will be talking about spring bean lifecycle one of the most important topic in spring cold framework again I'm saying it yes it is one of the most important topics so do not skip it this video is important ok my name is a Villas you're watching selenium Express without wasting any time let's get started [Music] so guys I hope you guys can see my screen here probably not because I have not saved my screen yet so let me share my screen and let me start the recording and I hope you guys can see my screen right now and right now I have a project here called beam lifecycle and I'm going to use this particular project to write my code okay right now I'm going to use this particular project and here I got a you know package already combed or selenium Express dot beam lifecycle dot a notation and first I'm going to create a class here called student Dao why I am writing a Dao class because I'm going to write some JDBC code so why I'm going to write some JDBC code because I have not written that he BC code for a long long time and if somebody is going to call me and ask me about hey do you know this question on JDBC then I cannot answer it and if I cannot answer it then I lose my 40 percent of hike just joking hi I'm going to use their even C code over here to demonstrate the beam lifecycle and the importance of beam lifecycle method so my intention is over here to hear some real-time scenario so that you guys will be pretty much flexible pretty much you know comfortable with this beam lifecycle methods right so as I said I'm going to write some JDBC code over here and I'm going to walk you through a table right now so I'm going to open the sequel server here and I'm going to go to my home so here I do have a schema here called es new inside the schema I am going to use this particular table right and if I do a select rows so I can see I do have this table called hostel student info and inside this table I have a few you know columns here right student ID student name hostel fee food types first my intention is to fetch all the columns from this particular table so I have to do some in our database operation so I'm choosing Jeremy's over here and it is really really simple I know you guys do not have any problem with writing JDBC code so let me go to my Eclipse right now and let me start writing code okay you okay so first of all to create a JDBC connection or what are the properties that I need obviously I'll be needing my username password URL and Driver right so let me define all those variables here private string first I need a driver so driver then private string a URL and also I want fry a string username most important and then I need a private string Pascua right so right now what I'm going to do I'm going to actually you know inject the value for all these things okay let me hard code it for now then I'm going to use spring right let me first write a simple JDBC program then we can bring in Spring Framework and we can inject all those things right for now let me hard code the driver my driver name is comm dot minus QL dot JDBC dot driver my URL let me grab the URL from my DV here so I'll do a right click copy JDBC connection string to clipboard I'll go to my Eclipse again I'm going to put my URL over here okay and apart from that I want to delete this user part and I want to provide my you know DB name the schema name where my table is present so I've already shown you here that I want to do my operation over a table which is present in this schema so what is my schema name yes new inside this yes new schema I have a table what is my table name this is the table that I am going to deal with right so first of all let me write the schema name yes new so I'll go back to my Eclipse and let me write e/s new so this is it so this is guys this is my you know IP if you want you can send it back to your localhost that's also going to work so nothing to be get confused by looking at this this is just my IP address right okay my username is root okay and my password is AVI okay now using all these four things I am going to do my DB operation so as I said first of all I am going to write a method to select all rows okay I can write a method like this and the intention of this particular method will be no fetching all the rows from my table right so as of now I'm not using Spring Framework I'm not using any single step I'm just writing a simple JDBC program to fetch all the row from a table right so first of all before I face the row from the table what I need I want to establish the connection with the DB and for that first of all I need to load my drivers though I can comment this out Lord driver so I can write class dot for a name my class name will be this one this is the driver that I want to use and this driver is you know now my SQL driver and this port name method is going to throw some exception I'm going to add it to the method declaration so this code here once I go once I load my driver I'm using my mind school that our database I've given my install driver if you are using any other database accordingly you provide the driver name okay once I load my driver I'm going to get a connection okay so to get a connection I need to use driver manager class driver manager dot get connection and I need to provide the URL so DB that I want to connect to the username and the password so what is my URL my url is URL you are username is username and password is password okay these are all the strings that I defined here so command click here it is defined right now this gate connection method is also going to throw some exception so I'm going to add it to the approach okay good so right now my connection is established right now I can execute my quarry right execute for it now to execute the query first of all I want to create my statement the corn dot create statement and this clear statement method is going to return me a statement and I'm going to hold the statement object statement java dot SQL make sure you are importing this one writes our load SQL and st empty the universal name and then I'll write stmd dot create quarry or execute quarry sorry execute quarry and Here I am going to pass my quarry that I'm going to run so I actually I want to get all the rows from my DB so actually I want to you know run a select statement and you run the Select statement so I'm going to copy this curry control C and I'm going to come back to my Eclipse and here I'm going to paste it and this one this quarry is going to give me a table write this this quarry is going to give me a table back just like this just like this so how to have to get all the row from the table obviously we need to iterate right so this particular query that I am running here this is going to give me a result set back right resolved set object there is all set RS equal to and let me minimize this so that you guys can see it in a full-screen and right now what I'm going to do is I'm going to hit array through it so I'll write while and I'm going to do what I'm going to as long as I have data in RS okay as long as I do have data in this result set this while loop should run and in this while rope I'm going to get all the rows all the columns from this result set right so RS dot get int first of all I am going to run why am i running the gate it so to explain you that I hope you guys already know about this but in case somebody is not knowing it so I'm going to describe this table so that I'm going to get the format what is the data type of the student ID what is the data type of this this and this right so describe my table name is this again let me copy this ctrl-c and ctrl-v it over here and right now let me do a command enter to run this query and you can see this is the rated I write to and ID is n type student name is Varsha type hostel fee is the we'll type and foot type is voucher okay so this is string this is string this is in this is double right so let me you know get this one for student ID so this is end so let me go back and let me do what column index 1 and this is my student ID so let me write int student ID okay so right now let me get RS dot what is the next column the next column was student name and let's say get string get string method pass the index number which is two in my case and here this is go this is going to return me the student name so I write string student name okay and right now I'm going to print all this things so so to print out I'll do this out and I'm going to write rodent ID laws student name sorry plus I need to give a space and after the space I'll write student name through a name after string name again I want to give a space then I want to print hostel fees and after that again I need to give a space and again I need to print food ties okay great so right now we have written a method and if we call this particular method we are going to get all the rules that we are having in our DB side so to test this application let me write a test class right yet we are not dealing with the Spring Framework we are just writing simple JDBC code here so here first of all I am going to write a main method right inside my main method my job is to call the student now select all rows method right so what i can do i can create a student now objects student Dao Dao equal to new student Dao okay and I'll do tau dot select all rows and this is this particular you know method if you see here this throws some exception so let me throw the same exception to the main method okay so let me add it to the Declaration okay so right now I can run this particular method and let's see our program is running or we're getting some error where are you boy okay we are getting Javad lambda class not found exception because I have not added MySQL JDBC driver to my class path right so let me add it to my class path okay we are just pre setting our environment before we write the spring program so let me do all this thing first build path configure build path and I'll write our external jar so I'm going to add first I do have a mine secure connector here let me are the MySQL jar and also I'm going to deal with spring so I'm going to add all the spring frame observes as well there's okay right now let me run this again and where is my console okay good so I'm getting all the data right now right so we are successfully written a JDBC program to fetch all the rows from the table right if you see in our in our workbench if I go to write in my mind scale I'm having all this one of the last 200 to none ways and all this because that I have over here I am getting it here in my console right now Oh guys this is not going to be a shortcut tutorial I'm going to talk too much so if you do not have much time already if you're going for an interview or something you are trying to find finding out okay what the bin lifecycle method does this tell me straight away then this tutorial may take a long time for you to get into the things right so let me tell you that previously right because I want to clear some scenario so that you know whenever you guys are working in real time you guys should feel comfortable because bin lifecycle methods are pretty much important because I don't think any project is there where we are not using you know life cycle methods or callback methods so this is pretty important so this one I'm actually emphasized okay no problem so I know you guys have time if you are my viewers especially you know I take a little long time to explain things so right now what I want to do actually I want to create a another method okay just I'll tell you why I'm creating all this method but let me create another method and this time my method should do what here I am actually you know pitching all the records from the database now tell me one method I should write one method using that method I should delete a record from the database right so right now I want to write a delete method here as well and my delete method job will be it will help me to delete a record from the database right so how do I the delete method here so again let me write a another public method so public void delete record okay let's say delete student record delete student record so that it will sound more than Rick and here let me delete a record so to delete a record first of all I need to do all the same thing right let me copy till here okay and let me paste it over here and once I am done with all these things the next thing I want is drawn the delete query dude on the delete query what I'll do STM t dot execute there is a method call probably execute update yes and here I am going to write my delete Cori so how to write a dilatory I hope you guys about it delete from table name what is my table name let me coffee from here control C hey what is the semicolon is doing here is not needed right and let me go come here and let me pass my table them where student ID is a cure to let's say one or two or three based on the ID that I am going to provide based on the ID actually it is going to delete the record late let's not hard code it lets the user decide what the row he is going to delete so let me pass him the student ID let's say student ID tell me the ID that you want to delete and floss I need to concatenate with student ID that's done right and we can print out a message right now okay so right now is saying to throw all these things declaration and here also I can throw it to the declaration okay so right now let's write a log statement let's say you wanted to delete this record so is deleted okay and here also we can put a logger statement simple right now let's test this daily student record so how to test it so right now again I will need to come here listen it Dow delay student record and I want to let's say you know let's say I want to delete the record number five I want to delete this problem record so let me pass five here to my program so let's say five okay and let me run this where is my console okay now first it is facing all the records then it is saying okay the record five is deleted to confirm again so what I will do here I'm going to run this select statement again here in my DV and you can see the number five record is no more so both of our methods are working okay so right now we have successfully written this two method and congratulation because you stick with me for this long and thank you very much for that right now we can tuck right would not need to write all this logic right now right now we are going to introduce Spring Framework and we're going to understand okay how does callback method we need to introduce over here okay how the bean lifecycle methods we need to introduce over here okay so I have missed one thing over here so if you see select all roads right and I'm creating the connection here I'm not closing it here also I'm creating the connection here and I'm not closing it this is just like you know you are going to your locker you're opening the locker door you are getting your jewelry you are not closing it right can you can you tell me why we need to close the connection why it is so important to close the connection why it is so recommend it to create the database connection right if you're opening the database connection means you need to close it and why can you give me a answer in the comment section and whoever give me the best answer I'm going to pin that out so that everybody walk in to watch this tutorial they they're going to you know see your answer now somebody some of you might be thinking like oh no you tell me the answer you know what you know when teacher doesn't know the answer you can say hey you you tell me the answer I am doing this this like that jokes apart but you know you just try to give the answer in the comment section so that you know we will not waste more time on it okay so now I need to close the connection once my attacks are done I can write connection object dot close right and here also once I am done with my stuff in the delete method I can also do con dot close right this is closing the connection right super [Music] right now okay now let's introduce spring first of all you know in the in the previous videos might be in the video number seven you know how to inject those these things from a property file right right now I'm not going to use any property file here I'm going to directly go to my bins dot XML file so I do have a bin start XML file over here if I open this here I'm going to define my bin right and let me inside this bin and bin let me give it an ID first let's say my ID name is student Dao and let me give the class name so class equal to let me go here let me copy the qualified name let me come to my bin start XML file and let me let me define where my class is defined in who is packaged and once I define that and here I can set my property okay so now let me say the property name one by one so here the property is driver let me control say it let me go here let me control V it and let me give the value for this property what is the value for this let's not hardcore it over here the rather I'm going to take take this particular value from here I'll do control X and I will remove this part here and I'll come over here to my bin store XML file I'll put my value here right and similarly I need to define all the properties so let me speed up this video probably I'm going to speed up this video so that you know I can do this operation quickly alright guys so right now you guys can see here I have written all my property here I've given it the value and I've also removed a few things over here and also I'm going to remove this thing from here because I'm going to pass it from there from my you know bin star XML file so I'm going to remove all these things right so I'm setting it here in my bin start XML file and in this time I am NOT going to create the object just like this over here rather what I'm going to do it I'm going to get it from the application context right so let me minimize it let me write application context context equal to new class path application context let me provide my bin start XML file here we'll start XML and here in my file let me do a context dot get bin and in my get bin method first of all let me provide the beam name my bin name is what my bill name is student off and we'll say it ctrl V it so this is going to give my student dow class object rights to attend now we can write like this and we can do a student Dow dot I can call my method right select all rows okay and let's try this out let me try this out right now this is this should call me select all room at the right if I do control click you should call me this particular method now this tell me if I'm going to run this particular application okay this is going to run or not I'm just going to test your knowledge here so right now I moved everything to Vince your XML file Here I am creating my bean and right now if I'm going to run this particular application it is going to run or not let me run it and I should get an exception and let's see what's the exception says so error in setting properties really so it is saying not writable property accepts and it is not able to write the property why right why does it not going to write the property for me in driver Y it is not able to inject the value for me what is what is the problem there so if you go over here it is not able to inject the property for driver URL username and password it is not able to inject this properties the reason for that is pretty simple because if you see over here all these values are private and I am NOT creating setter for that so let me create the getter and setter for all these things so I'll do source generate getters and setters and I'll do what I'll select all and I'm going to select the getter and setter for all these things after the username up to the password right after here I want to insert all the things so ok ok here we go so we got all the getter and setter right now ok cool so right now let me try again if I go to my tester Java here again if I am going to run this application now you can see the things are working fine so right now I'm getting my lager Ritter being all student data and it is giving me all the student data right that's cool so we are right now introducing spring but right now guys if you see in this particular class right there are something that we are writing again and again can you figure this out if you see this method and if you see this method there are something that we are writing again and again every time we are writing it and can we make it more generic this is not something pretty good coding that we have done this is doing our job that's fine but something which is pretty redundant over here which is pretty you know we're writing to placate course again and again here class dot for name and connect connection that we are creating over here is common over here as well we are doing the same thing here and here and also for closing we are closing it here and we are also closing it here so creating connection and closing connection is a pretty common thing that we are doing in this application right in this particular class so why don't you you know move this thing to a particular method so that one time only you can define over there so that you can make it more generic right let's say I need to do 50 DB operation I do not need to create my connection for 50 times right why don't I move this particular code you see over here right class that for name and get connection method this thing I can control exit and why don't I move this to a different method let's say I'm going to create a generic method here public void create you know student DB connection I am create a method like this and I can move my code to here command B and I'm going to format this method command shift F and I'm going to actually in or delete the things from here as well I don't need it I don't actually I want to make it more generic right so I am removing those code over here also and actually I'm defining this thing over here so that this particular method is responsible to create a connection before I perform my DB operation I want you know my connection to be created so this particular connection creation method stuff that I am doing I am putting it over here inside this particular method okay and similarly what I can do is I can actually create another method public void close let me define it here let me define it at last right so that you know it looks good that we maximize it let me write a method here public void flows connection close connection okay and what I'm going to do I'm open it and close it and this connection got close I'm going to move it to here control V and move this comment to here also control X control V and from here also I am going to remove the connection to close because the closing stopped this particular method is going to do right and I can say this Gorn is giving me error here here and everywhere why the corn is giving me error because we move the corn to this particular method and this can't become the local variable and this particular methods are actually not able to find this corn variable right so what I'm going to do I'm going to define the connection object here in the beginning so I'm going to take care let's say connection connection Java rescue con here and I'm going to initialize this connection over here I'm going to remove this con from here okay so that if I do command click I should come coming here right here I'm actually defining my connection right this is my connection object Here I am defining my connection not right here I'm initializing my connection on such simple so right now it is giving me a few errors because I need to handle those you know exceptions in my declaration so I can do like this and also I can put my cursor here and I can add this to the method declaration now it's gone naming class is ready so what what we basically did here just tell me we just did a very simple thing okay this is also throwing me some exception let me add it to the throws okay so what we are basically doing right now we are actually creating one method which is going to create our connection and one method which is going to close our connection this is like before I do any DB operation I need to create my connection do the operation then close the method okay and right now I'm going to run this particular program and let's see the things are working fine or not so I'm going to run this and let's see in the console what I am getting and I'm getting a null pointer exception and why I am getting a null pointer exception I'm getting a null pointer exception where right so let's debug it we're actually I'm getting is null pointer exception and what I'm going to do right now let me debug this particular program right click debug as java application right so let me open the debug perspective and right now let's see where my code is breaking right inside the select rows let me give a breakpoint so I did ctrl click and this is my select all rows method and let me put a breakpoint here and let me see where my things are you know felling so let me go back to my tester Java and let me do a f6 right so it came into the select all row method now let's see what is happening so if I do another f6 it is going to coming to this particular line and here if I put my cursor this con is not right so whenever I am doing any operation and whenever I am doing this clear statement this operation over this corner object this object is null and trolly this is going to give me the null pointer exception so if I do f6 again you can see where I'm going I'm going in to I should be going into exception see I came into exception right if I do if 6 again I'm getting exception over here right ok so right now let's do it analysis for 2 minutes and let us understand why still we are getting this null pointer exception guys is pretty simple I know most of you already caught why we are getting that right so I just want to show you this thing so there's this method does what simply tell me in one line what is the job of this particular method create student DB connection this method just initialize my connection object this corner object when I define this corner object my current object is defined here the value of this gone object is null and I am initializing if I do ctrl K here Here I am initializing my connection object my con variable get initialized over here correct in the line number 56 so whenever I am performing any operation here or I am calling any utility methods these methods are called utility methods the delete student record selecting all the records from the student right these methods they are doing something right so we call it utility meters so these methods are performing operation directly on our connection object or our object and if I am not calling this particular method okay then this gon object value is null because if I'll not call this particular method obviously the corn object will not be initialized right if it will not be initialized the value of this corn is what you filed oh come on Kay you already seen right the value of this corn is now so that's actually the problem so the solution for this particular problem is very simple if I call my this particular method create student DB connection here okay if I call my method here create student DB connection then my problem is solved if I if I go to test or job - if I run it then you can see there will be no you know error over here because now that particular object got initialized but I don't want to do this I don't want to do this I do control jet over here now why I don't want to do this because see guys right now I have two methods select all roles right what about if I call this particular method right now this if I call this particular method again it is going to throw me an exception because this gone object is not getting initialized inside this again I need to call that particular method over here so think like if I have 50 operation if I have 50 utility methods right then 50 times I need to call this particular method right this particular method I need to call it over here then again over here so for any utility methods that I am going to write like this or these so everywhere if I need some DB tasks to be done then I have to call this particular method okay inside my utility methods and I don't want to do this why I want to do that because I'm using Spring Framework I want my Spring Framework to execute this particular method before I perform or before I call any utility method just like these or these or any utility method that are going to write before performing those method I need my connection object so I'll tell my spring framework a framework do a favor for me you know once you create the bean please execute this particular method right so the way I'm going to tell it to the framework is by putting the annotation called post construct once the object got constructed please execute this particular method for me you okay let's not jump into that story right now we'll be covering everything step by step but right now let's simply understand that if I if I'm putting the post construct annotation over here is my problem is getting thought so to check that if I'll go to my test or job I again and if I'm going to run this particular application again I'm damn sure I'm going to get the same exception once again in my control non-funded exception so why I'm getting it while still I'm getting it let's dig into it okay so we found that our problem is not yet solved still we're getting null pointer exception right so we have you know an outdated this particular class is post construct annotation I'll tell you why I'm coming back to this particular point but my intention was the framework should call this particular method automatically and it should initialize Mike on object so that you know I can use my corn in the utility methods like delete like select all roads and all right but if this is not actually happening and why I think you already figured it out if I go tube instead XML file I have not you know I know I mean activated my annotation right so what I can do is I can write context annotation config I can just write end it over here and right now if I will go to the test or Java class and if I'll again run this and if I open my console let's see this time what is happening and it's working right so you understand bin life cycle no just joking so right now I'll be I'll be explaining everything very clearly I know whether you know most of you who are you know watching this they're beginners and I'll tell you the best practices I'll tell you you know or everything step-by-step I will not be rushing into things but you understand bin life cycle I'll tell you how so right now you can see this particular method create student DV connection if I do command F right if I'll do forward if I do backward you can see you know I am NOT finding that one so this particular method is not I'm not using any where am I using the method over here am i using the method over here no to create connection I'm not using it so framework is calling this particular method automatically right so just to confirm I can put a lock statement again here this is this out let's say creating connection for you know student you know DB I can say something like this okay if I will be running it say right now creating connection for student DB see where it is calling right before it executes my select method before that only this particular method is getting called this particular method is getting called correct correct now let us understand okay listen this time step by step you know what is the exact you know flow what is the exact process when our unit method will be get caught so when is the init method what does it mean right so you need method is a standard a name so if you already work with servlet I hope you guys already know about init method but if you go to real world application guys you need method is the one you will find everywhere right so you need in it is just a standard name right I can mark this name as in it you in it so what I can do right now rado you know making this as my init method whatever you do I create a unit method right now I'll here I'll create a knit naturalist a public void in it right I can name it at anything I can I can say it my in it I can say my in it that house a fine let's let's say in it method right and inside this init method I'm going to you know move this annotation over here just to you know may adjust to maintain a coding standard right so it's a good approach right I am writing unit method so if any developer comes if you start so in it he'll get this method right then you'll understand okay this is what the init method so inside the init method I want to call this one right right now I'll be doing the same thing I have just you know calling this particular method over here and obviously this particular method throws all this exception I will add it to the declaration so this is a very good architecture right now right because I'm maintaining a standard right so right now in I have created an init method over here I did put the post consider penetration over my knit method and let me remove this is out over here and let me put asses out here right inside the init method I can add inside the cost a minute method because I am only creating it inside the custom init method ok so right now let's go ahead again let me save this let me go to my test or java let me run this again let me see my own saw and everything is working fine see so inside the custom init method first rate is printing that mean my init method is getting called then it is calling my utility method that's working good right now let's understand that complete flow so guys tell me first of all okay so tell me when actually you know where actually the beans are getting created the bins are getting created inside your container what is your container your container is application context right if you go to your test plus so here you are creating your container object right so inside your container all the objects that you are creating will be get maintained by Spring Framework right so you know the objects that spring from up maintains we call it B so beans are actually inside the container so yeah it is a container responsibility that will create the object and that will manage the object inside itself right so once the object got created what is the next step so once the object are created the next step is the Spring Framework will inject all the dependency right for my class for my bean this is my bean this is the object that I want Spring Framework to create and you know maintain the student Dao object inside the container so what are the dependency I do have over here these are my dependency right and these dependences the next spring our target is once it created the object for me once it's created the student Dao object for me it is going to inject all the dependency for it like it will call the setter method offset drivers that URL set username set password it is going to inject although you know dependence right once it's inject all the dependency then after my unit method will be get called so before it is going to handing over the object to me it will make sure that it will call the unit method it will call the init method you got it right now let's prove it okay because I told you know don't believe me right let's let's prove everything right that we discussed okay so first of all what I'm going to do how to prove it so this driver command K this is get rebel command kay come on Kay so again I got my driver here so I was just looking for a driver command K is going to you know help you for such where the driver is present right so otherwise you can do command o you can you can use set driver right so this head driver will be used by Spring Framework to setting the dependency right we were we have already seen this right so what I'm going to see I'm going to put a logger here and I'll say in our setting driver dot dot okay let me copy this and again I will go to to RL so I just want to see this loggers are coming way and when what is the flow of execution right so what I want to do here right now inside the State username sitting I can say sitting username and then I'll say setting password so I put lover everywhere and also here also I have it logger inside the custom init method and this is okay here I can say this out creating connection okay good then here I already have a locker that's good so actually whenever we're setting property it is going to internally call the setter method right and that's what we are going to prove over here right now right now I'm going to test at Java again I'm running my application let's see what is going to happen okay support first we understand first it is going to create the object first student down okay once the student Dao of just got created then it is going to set all the properties right once it sets all the property then it is going to call your init method that's why you are writing here if you go to init method you can see you're writing post construct once if objectin struct it immediately after that not immediately after that there are some other things also happening we'll be talking about that later but once for this tutorial you understand once the objector created dependency got injected then once the object got constructed this init method will be called by the framework simple right so you can see here inside the console right now right now immediately after this the custom init method get called the custom init method is calling the connection create connection method you know you can see the custom init method is calling though this particular method if I contour click it this is this method and here we have a logger creating connection so that's what we are getting over here and after that the utility method that we are calling over here if you see here the utility method calling that is coming over here right this is doing this work so you understand the lifecycle how it is going first this then this then init method right you understand this if you understand this flow there is nothing you've already got the beam lifecycle so I can you know also print this sis out student Dao student Dao here so this student now is this object my student now object see where it is coming student our object here right so there is no confusion the init method will be called just after all the property got set to your PIN right simple right okay [Music] okay so that is right now you understand why we are using unit method right you understand why we are using init method like if whenever we need any you know setting up stuff okay before I call my utility method if I want to do some tasks some setup tasks okay like getting connection or whenever you are working with web services you want to do some you know before you execute your utility method you want to connect to the web service right you want to do some work or whenever you are doing socket programming at that time also you need to open the socket or you know you need to do some pre task right that task you can put it over here and annotate with first construct method and there is another method guys right this close method right this flow connects Condor close also we need to do this close method while running where we are doing some cleanup stop one switch first we did the start of stuff then we are running our methods once a method running is completed we'll make sure that we are cleaning off everything right for an example this con con object right the con object we have initialized somewhere at control K again okay so inside somewhere we are okay here we are initializing right con object-- so after our work is done we'll make sure to close the connection right and that closing stuff we are doing over here inside this closed connection method so I'll write another method okay called destroy method and the destroy method will do what whenever my beam will be removed from the container at that time the destroy method will be called and it will make sure whatever the cleaning stuff that you want to do you know you can do that for an example first of all if I if I can write if I want to make it my destroy method what I'll do all right before you destroy the object right wrong this method right pre destroy score right so what does it mean before this particular beam object right this particular beam is removed from your container before it removed make sure you call the destroy method free destroy method right so what this destroy method will do here we can put our cleanup stuff here we can do our cleanup job okay just like I am cleaning up the connection I'm closing the connection right so the framework will call this particular method you know close connection right now automatically because I am saying this is the destroy method for me okay you you so if I'll go to test or Java and if I'm going to run this application right now check what is going to happen and you see the destroy method is not getting called right you so I can do what I can put as this out over here or let's say inside destroy method okay so I can come over here again and run this particular application and see what is that mean okay that particular logger is not coming here right why this is the end of the point to understand so see guys right now we are working with AC application or e application is a web application or is there desktop application obviously there is nothing we're not dealing with any URL nothing right is this a simple desktop application so whenever you are working with a desktop application the closing stuff we need to take here right if it is a web application the closing stuff you don't need to take care you you see here the context that we are creating this object we're not closing it right so we need to explicitly close it over here right in your web application you don't need to do it your framework will take care of that but in a desktop application you have to explicitly do context dot close but see the close method is not coming right because inside the application context there is no close method defined so what we can do is inside the classpath XAML application context there is a closed matter so I can get the close method from this right so how to get it this is my subclass right this is my superclass context is my superclass object so what concept that I'll apply over here to call the close method from this class what I'll do I'll downcast it right to downcast it's simply all right here where the close method is present the close method is present inside this class control-c control-v and I'll wrap it again with a brace here okay and I'll do dot close cool so right now if I'll run it now see what will happen okay inside this try matter now my object got destroyed because if we see if I click here student down this particular method is executing right now my framework will make sure that to call this particular method automatically before the beam is removed from the container here we are closing the container means what we're removing the beam we are removing this particular beam right that means this beam is created inside the container okay and when that particular beam will be removed from the container when the container object itself is going to destroy your beam is also going to addressed all right we are going to remove the beam and before we remove the beam will make sure to do all our cleanup job by putting this particular annotation so that the framework will call this particular method and any open connection is there any open socket is there it is going to close that off well I mean close that for us right you understand that right now less fault follow the standard without making this close connection as pre destroy method what I'll do I will do control X created new method here command-v that's a public void destroy I can make I can give it any name here but I am giving you a destroy and I'm going to move this thing control X 2 here and this is this particular method is going to do my cleanup job and at last I'm going to call this close connection method right so I'll do close connection I'll call this method over here ok and I will throw the exception whatever it is throwing I throw over here ok so this is not mandatory might be your destroy method should not throw any exception right so take care of that so if I if I run this right now I can see no changes things are working fine my destroy method is getting called my unit method is getting caught you got the concept net method when it will be called once the object got created nobody got insert inserted after that only unit method will be get called ok then your beam is ready to use ok once you're in it method is get called after that only you can use the beam just after you destroy your container framework will make sure to call your destroy method ok to clean off everything oh I think I'm making sense all right [Music] there's some time guys you cannot see in your real-time project right this annotation config the this tag might not be there at their time you know if the tag might not be there how do you need and destroy method will not be get called right if I'll run this then my my those particular things will not be get called in obviously I'm getting a nullpointerexception right [Music] so might be there will be a beam just like this and the class name will be right now if you see I do not have an annotation context config tag I do not have over here right right now I'll directly go here and if I run this let's see whether a program is executing or not now is working fine so why I'm telling you to see this one what's the magic this is doing is it is pretty simple so this particular class is just activating this - annotation right this - annotation that we are using post construct and pre destroy you need pre destroy and post construct this is just you know you know activating these two annotation right but you know I'll be sticking to my previous approach but this is the one thing that I want you also to remember because you might see that in your code you might be surprised why they're there using it and how the things are working because we do not have annotation contracta right so that's what you know for that particular annotation we have this particular class if we initialize this particular class it will activate that - annotation we can understand it just like this right okay so this is one way of doing things and this is called your annotation approach right you you guys tell me one thing are you getting confused by looking into this line why I'm doing the down casting okay I can do I have a simple solution form it for you I will control sit I will do this way plus by the application context context record a new class for the application context and here I'll directly say context but close because the context is directly coming from this particular class right now this now is fine okay and I just have a curiosity that you know if you guys are not getting why I did that right this is just I'm just closing the content or nothing else right okay okay so now we talk about the XML uploads right this is one of the another pretty standard approach to do the things right we'll see the XML approach it is pretty simple the same thing we are going to do we are just going to remove about post construct will comment this out and comment the pre destroy also okay will we are not going to use annotation if I'm not going to use annotation how we are going to asset this if I'm going to run this obviously the things are going to frapp right you know about it okay so right now how to do it in a XML way let us understand it right now you where I'm defining this particular VIN for student doubt right so I'll go for the bin store XML file and here I'll search for the student now bin this is my bin and here only I'll define the unit method init method and what is my init method name my unit method name is what could command oh it this is going to show me all the method and unit I'll type and this is my knit method I'll just going to control see it now go over here and command V it and also I'm going to give your destroy method and my destroy method is what I'll go to again student Dao command or destroy I'm doing this because I don't trust my spelling and I'll go over here and I'll put my destroy method over here right now that's it okay you are done you are good to go now if I'll go to my test or Java class and if I'm going to run my application I should get all the expected result you can see the inside the destroy method is also getting called inside the unit method is getting called and you know when the when the unit method we get called right so once the property are set right now I can see the unit method are getting good you know the XML approach right now it's pretty simple right you okay guys I'll tell you one more thing so this I am doing context or clothes guys right I told you right in Java is the application we have to close our context manually by ourself right few people also do it in this way they'll do what command s they will do context dot they'll actually acquire a hook so this called register shutdown hook okay actually this registers are down hook is going to do the same work is close only right so there there is no in a big difference between this method and this method so thing is that this is one of the alternative for this close method right so if I if I'm going to run this one you can see there is not much you know differences between that you can see our inside destroy method is also getting called over here but thing one thing I'll show you over here I'll comment this out okay and I'll activate the close this fine right I am closing my context over here I'll run it if you see this running fine right now again I will do what I try to do what I'll control say it I'll again try to this is student now one now actually Here I am trying to get the student now object again with a different variable I'm storing I'll try to acquire the student now object again over here but see the close method is over here after the close method only I am trying to you know again acquiring the bean from this particular particular object right and I'm storing it over here and if I'm going to run this right now what will happen you are absolutely correct we are going to get it exception because we have already closed the context right so how can we get it you know how can we accept that we are going to get the beam back from our container because we close the container this what is saying bean factory is not initialized or already closed okay that's fine but I'm telling you because if you'll try this you might get surprised I will remove this this is also do what this is also going to close my context object right right now you're here I'm trying to do the same thing again okay I'm going to run this and see what is happening here this time I'm not getting it such okay so the job of this and job of this is same but this is going to do what right this is going to do what this is going to actually you know it's make sure that once your main method is end right it is going to call the register shut down hook method to close the container right so that's what you can see you are not getting any exception right you few people do it in this way guys you can see it in your application if you're going to any life of location you go through might be you can see things like this abstract application context control space to import this and this could okay so this abstract application context is actually implementing the application context interface right so this is also possible you can you can do abstract application context context record the gnu classpath xml application context this is also possible so somewhere people also take create objects just like this right don't get freaked out that okay I don't know about it this is nothing this is this is does the same thing right this is the superclass of this and this class is you know you know implementing that particular interface application context and references easy right good [Music] [Music] [Music] so if I go to my bin store XML file so guys instead of defining my unit method and destroy method here I also can define the init method and destroy method here inside this bin stack that will do what think like if I have hundred classes in my hundred classes you know I have the same init method and destroy method I want to call the init method and destroy method in for my hundred bins so I do if I'll create hundred wins just like this one again I will do create a bin over here again I 110 I'll create the hundred times I need to define this init method and destroy method here right so instead of that what I can do I can directly do it over here inside inside here only I can define okay so I'll show you an example so I'll create another class here let's say my class name is hello okay so here inside this hello let's say public void in it and inside this init method what I can do is I can do a cyst out inside hello class init method okay and I can also have a destroy method command C and I'll put it here command B and I'll change it to destroy alright and here I can say insert hello class destroy method okay and inside the bin start XML file what I will do over here right now I'll again create the bin for my hello class right so I'll do what been here again I'll give it a ID called let's say hello and my class name I'll be getting it from here right click copy qualified name will come to bin saud XML file I'll do ctrl V and here also I need to define my bean right my bean is defined here right I can also define my init method and destroy method here right if I want to call them controls it and I'll do the same thing control B control s my need method name is in a destra method name is destroyed inside the hello class right so right now see the same method again I'm putting in it method here and destroy method here so if I'm going to go to my test or Java and here if I will get my hello class object see what is happening command C and after this one I'll do what command B and I'll right here hello because my bin name is hello and I want to be get the bin for hello class alright hello dot class right and I'll make it hello as well let me make it hello class now on the error is gone right so here I am actually getting the hello hello class object right now if I'll run this application you can see here it is it should be called inside custom init method you can see inside hello class init method right that is fine and again inside hello class destroy method inside destroy method this fine right now I do have a common solution for this this kind of thing I do not have to explicitly define these particular things again and again so I can do what I can actually remove this thing from here and remove this thing from here and I will define this inside this bean and bean over here I'll say default init method init method is in it and I'll define the default destroy method is destroyed right so that I do not have to write the same method for here I do not have to define it for here right the framework now the default unit method name is in it and the default destroy method name is destroyed so now the benefit of keeping the same name is this right whenever you will work in a project your architecture will be just like that your architect will define what should be my init method name and that particular name will be maintained across your application all the developers should maintain the same approach right if I am giving in it you need method name Minutemen and another developer will also give the init method named s in it only right so this is the way we can define I have removed my net method from here from here so what I can do here right now I can come here if I run this application you can see my hello class gesture method is getting called and my Dao plus destroy method is getting called and here also you can see the init method is getting called furthered our class and here it is get called for the hello [Music] [Applause] [Music] [Music] so the other way to handle the bean lifecycle is implementing an interface right I can implement an interface called initializing being probably this is the one and if I'll do click here and I'll do a done implemented method you should implement them as you should override a method called after property set this is gonna be your init method right this is gonna be your init method so let me comment this thing so this is one of your init method I can write your sister out inside after property set method so this is your init method right let's say unit method after properties that fill the name after you set the property then this particular method will be get executed okay and like that I have another method for dot you know for the destroy method for that I need to implement another interface called disposable being the is let me get yes this is the one and this particular interface also has one particular method called probably destroy method okay this is your destroy method let me copy this command C command V inside destroy method okay so destroy cool so right now you do not need any annotation you do not need any XML file this is only going to take care of that thing right so if I am going to be installed XML file so for a moment let me comment this particular code okay let me comment this particular beam con command X command V here okay let me comment this out and let me let's not give any you know in it or destroy method here command s to save right I am just defining my bin here it does not have a unit method does not have it destroy method define over here so I'm just dealing with the hello class right I have overwritten this two method of initializing bin and disposable bin and given my implementation okay here I can have another method public void simple since out my coding starts from here and ends here this printing something okay now if I'll go to my tester Java if I'm going to run this application okay okay okay it is going to give me a exception because I need to comment this thing out because I'm not using this okay I am only dealing with hello one okay and I can do right now I can call the hello class method that I've defined hello dot simple okay okay right now I will be calling the simple method so first my init method should be called then the simple method should be called then I'm closing the context so my you know destroy method should call it mirror on it let me see whether it is happening or not cool inside the after property set is in it then my coding starts here and end here then I'm you know getting my destroy method here whenever I'm closing the context of these are the three ways hopefully you have mastered it right now you know when to use what to use and why we have to use the callback method what is the need of that [Music] so this is what I'm going to tell you I think I have bored you enough but I want to provide you all the information in one shot and you got the thing why we use all these things but it's the need of all these things and bill lifecycle comes really handy in your real time application so I'll definitely encourage you to try this example and also I'll definitely encourage you to debug this example also so that they can see the flow how the things are going okay so in the next video we'll be talking about beam scope beam scope is also one of the most important topic for the interview purpose because they'll ask you those questions I know so I will be talking about that thing that's that's probably one of the last important stuff that is pending for this particular course and then there are some small small stuff for remaining which are going to become handy whenever you're going to work in a light project so I'll be telling you those things also and after that we'll be wrapping this course and we'll be starting with spring MVC so thank you very much guys for watching this and hopefully we'll have a great great day don't forget me to write ml if you have any doubt or ping me in facebook facebook / selenium express and i will try to resolve their issue as soon as I can thank you very much guys and happy coding bye bye [Music] [Applause] [Music] you
Info
Channel: Selenium Express
Views: 64,602
Rating: 4.8652482 out of 5
Keywords: spring life cycle methods, @postconstruct spring, spring bean life cycle interview question, spring init method, destroy method in spring, spring core annotations tutorial, bean life cycle in spring annotation, spring tutorial for beginners with examples, spring core by selenium express, spring framework by abhilash, bean life cycle, selenium express, spring bean life cycle example, spring bean life cycle flow, how spring works internally, spring bean life cycle callback methods
Id: hUYGGqoXEPc
Channel Id: undefined
Length: 80min 53sec (4853 seconds)
Published: Sun Jul 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.