Up here on the screen I've added in a class entire project here and this is a lifecycle demo bean and 1
what I've done is I've created a bean that implements the variety of interfaces that Spring Framework 2
gives us to work with lifecycle events. 3
So add in initializing bean disposable bean, bean name aware, bean factory aware and application context 4
aware. 5
So these are all the variety of things that we can work with. 6
And what I want to show you know is let's go ahead and run this and we'll see how this interacts with 7
our application. I'm just going to click on the Run dialog and take a look at the log output. 8
Now the lifecycle demonstration bean I did prefix everything with a double hashtag so we can easily 9
see how things are happening. 10
So you can see right here the first thing that happens that's in the lifecycle bean constructor. We 11
get the name the bean factory is set. The application context has been set and then the post construct 12
has been called. A lifecycle bean has all of its properties set and then you can see the output that 13
we had before from the variety of beans that we've been working with throughout this and right towards 14
the bottom there you can see two more pre destroy and termination messages. 15
So as the context came up we saw all the lifecycle events and then we ran through basically everything 16
that we had before and then as the context shuts down you can see the other methods getting called. Now 17
this works out well. This gives you a hook into all of the lifecycle events. 18
Now we also are able to work with a custom bean post processor so I've added in this class as well. 19
I had the component commented out there. 20
Now let's go ahead and run this again. 21
And this allows us to do actions on beans as a post processor. So as beans come up we can run some 22
type of post processor. 23
Now we can see here we have additional messages output now that I brought that into the Spring Context. 24
You can see here this before init, bean called by the bean post processor and then the after init. 25
So again these are hooks that we can have into the Spring bean lifecycle. And to be honest I've done 26
a lot of development with the Spring Framework. 27
Pretty rare that you need to get into lifecycle events like this but important you might have some type 28
of edge case where you need to get access to these. 29
This is typically stuff that the Spring Framework developer is going to be using as they develop on 30
the framework itself. 31
You as an application developer probably aren't going to have a big use case for hooking into these 32
lifecycle events. But it's also important to be aware that they are there. 33
So when you do need to access lifecycle events of a bean you do have that access. 34
And keep in mind that there are cases where you as an application developer, again you don't need to do that and 35
get a handle on these life cycle events. 36
So just as a quick review between videos I did go in and add in two new classes. 37
One is a new Spring component called the lifecycle demo being so down will be available inside of GitHub 38
and I implemented all the Spring interfaces to work with Spring bean lifecycle event. So important to 39
keep in mind that there are a set of things that do happen as a Spring bean is being initiated within 40
the Spring Context. 41
And then there are also another set of things that are happening as that Spring Bean is destroyed or 42
brought down inside in the Spring Context. 43
Then the second class I'm adding into the entire project. This is going to be the custom bean post 44
processor. 45
So this is going to implement Spring interface of a bean post processor. Spring 46
is going to see this as a component bring it up and then this is going to see it allow you to inspect 47
all Spring Beans before they are initialized. 48
And then after they are initialized so sometimes you do need to hook into those events set on a more 49
of a global scale. 50
So the sooner you can utilize a bean post processor for any special configuration needs that you have. 51
And again both these classes will be checked into Github for your review.