In the session, let us learn about being scope and the life cycle. Let me first start with the scope, what is the scope scope is something which controls the number of instances that is created by the IOC container. Till now, we have used the IOC container to create the Java objects which are nothing but spring beans. By default they are Singleton. What is the meaning of it? Whenever you are calling context dot get bean method, the same instance will be returned to reclaim the IOC container will not create a new object for each and every method call.
The same object will be given to the client. So the default scope for a spring Venus Singleton these are the scores that are available for spring being Singleton prototype request session and global session. Record first session and global session they are used when you're working in a web application. If you if you want to have your own scope, you can go in for custom scope. Now, let us talk about the singleton and prototype. This is what we are going to learn now.
Singleton is the default scope, where each and every call of context start get beat only the same spare Java objects will be returned by the IOC container. In case of prototype Whenever you call context start getting a new object will be created and given to decline. This is fast Singleton and prototype. Next, these are the scopes that are available in the context request session, global session and application special request. The IOC container creates a new bean instance for each and every request session. The new and new being instance will be created for each and every session and that is good session and application scope, what is the difference between these two global session is used for portlets portlet based web application, whatever web application we are using our servlet based web application global session is similar to that of session in case of portlet based web application and next you have got application scope.
The IOC container will create one bean instance which will be used for the whole web application the scope of application scope okay. So, this we will be understanding while we are working with spring MVC. Now, let me go back to eclipse and we will see how the singleton and prototype scope are working for working with scope. We are going to use one annotation at the rate scope. Let me just explain you while doing that. I'll go back to eclipse and working on the same application annotation demo.
I have created a new package comm dot n o dot lifecycle because we are going to see the lifecycle and scope in the same program only I have created a student class I have annotated with a direct component and I have given values for the instance variables directly I have given the value if you want you can use application dot properties file, which is already there in this particular project, I have overridden to string also this is the main class I have instantiated the IOC container I have passed on the full package name, then I am trying to retrieve the student object. Now, let me run this application already we have done this so, many times it is going to give the proper output name Assam city of Bangalore no issue in this now what we have learned this this a single 10 Next, let me try to call context start good deeds and assign it to one other student radio but let me just do that.
I'll copy these two lines Ctrl V, it is student one. Student one So, here I am trying To retrieve a student object now we are going to check whether it is the same object or a new object for this I am going to change the city student one dot state city of Munich that we have a dummy printed and statements and also let me try to print the student object now what what should be the output? The first output will be named Rob sorry here named Ron Citibank loose now again you're calling context aren't getting enough students? Will the IOC container container create a new object? No, it is going to return the same object to the student one instance here student one variable and using the student one you are changing the city to full name. When you try to print student one what will be the output name as Rob city is full name.
What about students? Because both of them are references pointing to the same object automatically This also is going to give the same output name as ramen city. So now, let me run the application. I've got the output, this is referring to students. This one is student one name is from city spoony. What about students?
Again, this is named ROM city pony. Let me explain this with a pictorial representation. Never I call context dot get been of students. The IOC container would have created the student object already. So it will be available there that is referenced by students. This is a reference variable.
So students refer to the object students. Now what I'm doing doing, again, I'm trying to call contexts that get enough students and I'm assigning it to student one, because the scope is Singleton. Now what has happened, a new reference variable student one is created, it is also going to point to the same object. It means the IOC containers going to give the same object to students one also Because of the scope, what if they want to get a new object for each and every method call of context or get beam. In that case, I need to go and change the scope to prototype. Now, let me go back to a clip.
Here, let me run the application again so that you will get an idea. The first liners students object. The second one is students want a reference pointing to the same object. The third one is students, this is also pointing to the same object, that's why cities getting changed. Now, what I will do is I will go to student class here, let me add the annotation at the rate scope. Within round brackets, I need to give the value as prototype you need to import it as the input as appropriate input.
Now, let me save this and come to this main scope. No change in this program. Let me run the application. Can you see the output This is the output for students lowercase through Name ram city Bangalore. What about this, this is the output first student one in this case the IOC container has created a new object and given to the student one reference however this name is not actually the same object we are given a great value annotation so name will be Lam city will be Bangalore but explicitly you are changing the city to pony that's why she wouldn't want us getting name ram city spoony, what about students it is pointing to its own object student on is pointing to a new object. So using prototype, you will be getting new objects on the call of context dot get bean method.
So this is about scope. Next, let us talk about the lifecycle of a beam. What is the lifecycle of a spring beam, you know that the IOC container is responsible for creating the spring beans, that is nothing but the Java objects. Once it has created the beam that then it will inject all the dependencies to the screen based on the annotations that you have given. But they use sector based dependency injection after use constructor based dependency injection. So, now the Venus lady to service the client, the client means in the public static void Main when you're trying to retrieve the the the Venus basically the object which is created and dependencies added by the IOC container.
Now, what if there is a scenario wherein I want to consider the bean with some functionalities before it is reaching the client. So, first step is creation of objects then the dependencies are added by IOC container and then it is given to the client wherein they are trying to print objects, but if there is a scenario wherein for this being I want to consider a few functionalities, who has to do with the IOC container only can have to do it even before adding the dependencies. Why because when it is reaching the client, it is a fully loaded spring bean. So for adding the configuration details, you can go in for lifecycle methods. The next cycle methods or I mean data races, inbuilt lifecycle methods, custom lifecycle methods and annotation based lifecycle methods. inbuilt lifecycle methods means the your class should implement initializing being and disposable mean and you want to override after properties set wherein you will be writing the functionality that should be added to the beam like you if you want to connect to the database or if you want to add some configuration details later to the server.
So, these kinds of information can be added in this after properties set. And what is this destroy method whatever you have added to the beat must be removed in which method will you remove it in destroy method, you need to override these two methods and you have to add your details. If you don't want to be tightly coupled with spring, that is, if you are implementing initializing and disposable weights, it means that actually your classes coupled with spring framework If you don't want to do that, in that case, you can go in for your own custom based methods and these information should be given an example, you create a method in it and clean up other functionalities and gives us information in the exam. And now, we are not going to use examples, we are using annotation only. So, you can go in for annotation based lifecycle method wherein you add two methods in it and cleanup and annotate the method will have post construct and pre destroy when this method will be called force can have the method annotated with post construct the Java objects are the spring beans created by the IOC container.
Then the method annotated with post construct will be called it means all the configuration needed for the spring bean is given and then the dependencies are injected by the IOC container. Now the Venus ready to service the client. Similarly, when this method annotated with fried OSI will be called when the beans references made to null, like you feel Writing students student equal to null. In that scenario the object should be garbage collected before garbage collecting the method annotated with three destroy and record, there is an order for execution, it is annotation, inbuilt lifecycle methods and custom lifecycle methods if you are having any, this is like this one. Okay, this comes in the XML part, see an example for using the slice cycle method. Let me go back to eclipse.
I'm going to work on the same application only. The first thing is I will comment this part and copy it and commented, let me go and create a new application context and remove all the stuff these are all not needed. Okay, now I'll come to the student. The student let me just have the scope a singleton only. Now I will save this. The first thing is the class should implement disposable bean initializing bean and disposable bean.
Let me add that implements Nice macing b comma disposable D keep the cursor and add unimplemented methods they are nothing but destroy and after property set. So, let me add printed statements. Now go the methods are ready with the printed and statement. Next, I need to have two more methods that are annotated with at the rate post conflict and three destroys. So, let me add two more methods in it and destruct and I will add print statements. My two methods are ready with the annotation at the rate force construct another a tree destroy if you see here I am getting an error, even if it keeps the cursor on the sanitation.
I am not getting any content that says to import these two annotations. Why? Because these two annotations have support for Java versions greater than 1.6. Already we have changed the Java version in Maven, but still we need to do one more thing. Let me just show it to you. Here.
You can just see Gary says library it is pointing to J to ESP 1.5 this should be greater than 1.7. So 1.6 basically we are working with 1.8 so let me go and change it selected, right click Build path, configure build path, a new window will open. again select this edit in new window will open here you have got execution environments. Now here gone select Java SE 1.8 now click finish, click Apply, click OK Come back you will still have the error but keep the cursor now you will get content that is for importing pre destroy and post construct Okay, now just have a look on these four methods. These two other methods which are from the inbuilt spring framework, after property status from initializing v destroy is from what essentially disposable beans and again clean up an init init is a method which should be called after the object is created cleanup is a method which should be called before the object is being destroyed by the IOC container.
Now, I have completed this part let me just go to this main scope. Now, let me run this application I have not changed anything let me run the application at a zero. Now I have got the output what is the first line inside our no init method. So are the IOC container first create the object if you are having a constructor the constructor will be called first, then it goes and call some method that is annotated with accurate post conflict then it is going and calling the inbuilt init method that is after property set. Then if there are any dependencies there are they are injected to this particular beam. In our case they are only primitive values, the values are injected to the beans, the bean is ready to service decline.
So when you are trying to print the student object, you are getting the output What about the other two methods, the method annotated with three strikes and destroy method are not called. So what we need to do we have to explicitly close shut down the IOC container and make the student objects of smell. So let us do that and go back to the program. Here. I'm just going to make the student objects as null. And then I have to go in first context dot register shutdown coop coop.
Okay, can you see I'm not getting any content versus because you cannot shut down the container using application context. You need to go in for abstract application context. Abstract application context important. And now if we keep the cursor automatically it gets registered shut down. So I'm shutting down the container and then I'm closing context close. You can you have to use context or flows to close the IOC container.
Till now you are getting a warning meaning to say that there is a So snick you're not close IOC container, now, you closed it. Now, let me run this application for the output you can see that the annotation config application context container is getting shut down or closed and then the method annotated with freedom of choice getting called in our case class cleanup and the inbuilt Delphi method is getting called then die was the object is garbage collected or is left for garbage collection. So, this is about the life cycle using the life cycle method. I have just shown you simple print and and statements basically they are used for adding configuration details to a spring bean. So, what actually we have seen in this session is about the bean scope and bean lifecycle in been scope we have seen we have talked about this prototype.
And in bean lifecycle we have talked about the inbuilt methods and the methods that are annotated with post construct and three destroy. Remember, you need to comment this accurate scope. prototype because in case of prototypes and new objects will be created each and every time even though you close sick I mean IOC container you are given student equal to none and you are trying to close the IOC container, but still it will not call the destroy method. Okay, so if the objects a singleton, you can just see all the outputs in the console. Thank you.