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
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
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.