11. Scope and lifecycle of a spring bean

17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
Already have an account? Log In

Transcript

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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.