all right so let's discuss being
lifecycle callbacks and how to use them
in your programs so back in IntelliJ so
sometimes there's a need to perform
certain actions upon initialization and
destruction of your beans now one way is
to use XML and specify in it and destroy
methods inside the XML and when you
write initialization and destroy method
callbacks he typically write methods
with names such as init parentheses or
initialize parentheses to spoil use
parentheses and so on so ideally the
name of the lifecycle Corbeau methods
are standardized across a project so
that all developers use the same method
names and that ensures consistency in
the project but obviously in that
scenario you're relying on the
programmers to keep up and do things the
same way what you can also do is
configure the spring container to look
for named initialization to destroy
callback methods on every bean so that
means that you as an application
developer can write your replication
classes and use an initialization
callback call init parentheses without
having to configure and init method
attribute with each bean definition so
in other words the spring container
calls that method when the beans created
and also enforces a consistent naming
convention for initialization and
destroy method callbacks so in our case
for our project in the game imple class
we're calling the reset method to
initialize the class itself I'm just
going to bring that up on the screen so
we're obviously calling that method that
we wrote our reset method
we have a looking main dot Java you can
see if we were to call them in line 35
to that so at the moment were
initializing the class that way
by calling them ourselves but instead of
calling it manually we can actually
configure the container to call that
method for us so first let's remove this
call to the reset that we're doing in
main Java so I'm going to delete these
two lines and also delete the extra
blank line there so now the method the
reset method won't be called but we
still want to call it after the beans
been initialized so let's open our beans
that XML file and one way of doing it is
to add an attribute to our beans
definition
called init method so this attribute can
be used to call any method that's used
for initialization in our case it's
going to be the reset method so let's
try that approach first we've come up
here on the first line after class and
put in each method and you can see that
IntelliJ gives us a shortcut there and
we're going to type in reset which is
the name of our method and now if we go
back to our main type Java and run this
and you can see this line here is
telling us invoking init method reset on
being with named game so that's one way
i've called me the initialization code
once the beans been initialized but this
way is prone to errors because for every
bean we've got we've got to specify the
init method in XML and it's basically a
different method for each bean that so
we use now we can try and ensure
consistency by always using a reset
method but basically it's up to us to
get this configuration correct in the
XML in other words making sure that we
always use reset and type it in there in
that init method so to ensure
consistency there's another way we can
add the default a knit method inside the
take the beans tag at the top of its Tia
file so we can also do it here we're not
gonna actually use this but I just want
to show you that it does exist so in
here we could also put default init
method you could do the same thing there
like that and that would then ensure
that for every bean that we've defined
it would automatically use that init
method because we've defaulted that init
method to reset but again we're not
going to use that for now some time and
some delete that and remove that now you
configure destroy method callbacks
similarly in XML that is by using the
default destroy method attribute at the
top level and I've just removed the
default init method so you can do
something similar for the destroy method
but this can be very bad practice
because it's very easy to get lost
especially if every bean has a different
name for the unit or destroy method all
right but we don't want to do that here
so we're not going to be configuring the
default destroy method or the default
init method you know being configuration
at the top either because there's an
even better way than using XML what we
can do is use the jsr 250 post construct
and pre destroy annotations which are
generally considered best practice for
receiving lifecycle callbacks in a
modern spring application so using these
annotations means that your beans are
not coupled to spring specific
interfaces and it's a good thing so
let's go ahead and use that what I'm
going to do is remove this an it here
for our game bean we're kind of using
that either and obviously we've removed
the other temporary thing that I had to
here the attribute for beans on the next
line the roads
for happines definition at the top of
the file so to use these annotations
instead we've got a register a beam with
the common annotation beam post
processor right so how do we go about
using these annotations or to use the
post construct annotation to call the
reset method and get the spring
containers to do that for us we just
need to annotate it with the post
construct annotation so that's our reset
method itself which is in game import
but to do that first we need to define a
beam definition for the container to
recognize the post construct annotation
and the spring posts construct
annotations handled by the common
annotation being post processor class so
we'll start off by defining that beam on
this line and this is our next line so
we've come down here and we put beam the
beam tag and plus equals all dot Spring
Framework dot context dot annotation dot
it's common annotation being posted
process you see in today was pretty
helpful they're getting used to find
that so we need to define that as you
can see I haven't defined an idea been
ID we just need the class attribute
that's enough to get it to work now that
we've done that we can add a dependency
to the annotation so for the annotations
API so that we can use the post
construct and pre destroy annotations so
gonna need the parent pom file open so
I'm going to open that there and we're
going to be adding another dependency
now we need to know what version that
were going to be adding those so the
quickest way of doing this is to go to
the nvm repository website and be in
composite recom and what we're looking
for is the Java rx10 annotation so let's
just type that in Java annotation I'm
going to select the first option that's
come back here you can see it's one
point three point two so I'm just going
to select that I'm just going to grab
the dependencies obviously we could type
this manually Feeny knew the settings
but in our case we just copied that I'm
going to paste that in and here below it
depends on West new dependency I should
say and it's going to make a note here
that this is for the annotation
API so we've got version 1.3 point to
death but we're not hard coding version
numbers anymore I want to cut that out
that'll give us a temporary error we
need to come up here we need to add a
new property for our annotation
sanitation
- API division I'm going to call it and
we're going to paste in the version
number there and now if we go back down
in version we're going to put dollar
sign left right curly braces annotation
- API version alright so that's a
configuration at the parent pom level
now since we've declared it in that a
parent pom file we also need to add this
dependency to the core module but if you
call without the version number so I'm
going to go back to our this time the
pom file for our core project and
they're gonna come down here and paste
that in as a dependency and obviously
copy the raheem so I'm just go back to
the other file again and I'll just copy
the entire dependency we will go back to
our core paste that in but we're going
to remove the version for the version is
automatically found from the parent pom
file alright sorry to find that now
right so this point we can now use
annotations which will be coming from
the annotations API dependency that
we've just added so let's go back to our
game input class we're gonna close down
this lady on the bottom it gives a bit
more space so we've got this reset
method so what we're going to do is move
this the reset method above the common
public methods and that's because it's
going to be caught in automatically by
the spring container so I'm going to
make it obvious to ourselves but this is
initialized and then it method
effectively so I like related a separate
section so just pasted that there moved
it up so it's clear to us so what you
need is to add the post construct
annotation from the coop here at posts
construct at the moment we've got a red
error there but I will talk about that
shortly
so in initialization methods you can add
an initialization code that usually goes
to constructors when you're not using
the supreme container so our file
smartly it's nicely structured where
we've got to have fields at the top and
after the fields is now a neat and
public methods it's always a good idea
to
and initialization code right after the
fields that way code is easy to follow
and you don't have to scroll up and down
the list to find your initialization
code alright so at the moment we've
added postal strike the annotation but
the annotations are still in red in
other words IntelliJ is showing the
compilation error here now this error
will only occur with Java 9 and 10 and
it's because the annotation is in the
Java EE module and it will be removed in
Java 11 so the workaround here if you're
running Java 9 or 10
he's just to click on that line
somewhere I'll click into the post
constructed so until the red light
bottle bequite appears and it was notice
that it hadn't appeared before I started
typing anyway but once the red light
what was appeared I can click on that
notice that we've got a couple of
options here there's one suggestion to
add the Maven dependency but of course
we've just configured that dependency so
that's not going to help us here so I
want to click this first one add add
modules to Java next ws to add
annotation to the module compiler
options so again it's just a workaround
for Java 9 and 10 if using Java 8 or 11
you won't need to do this basically any
do this if you see that the annotations
are showing in red now that I click on
that I notice now that the air is gone
and the annotation is not no longer
displayed in red which is good alright
so I'm going to add another method after
our recent method which we're going to
call or have a spring call for us before
our bean gets destroyed so to do that
I'm just going to create a method public
void pre destroyed parenthesis and open
up the a letter like curly braces I'm
just gonna do a simple login so I'm
logged on info double quotes it's going
to be yeah in say in game free destroyed
just so we know that the pre destroyed
method has been called and what we want
to do is add an annotation to that so
that it does get automatically called
for us so at pre destroy using the a pre
destroy annotation on the pre destroy
method that we've just written so now
that we've done that we go back to main
and run that
it's not open have a bit of a look at it
and in fact they can often be fast that
to try and find it by using a controller
for command F depending on your
operating system I still search now for
common annotation that's probably an
affair so just want to see the uses of
our common annotation beam post
processor which is of course what we
added to our beam beam study XML file
I'm screwed I've never been one lucky
these lines here are interesting we've
found the init method on class okay I'm
in pool class and it shows us the method
reset it found the destroy method which
is our pre destroy method there it's
registered in it and destroy basically
registering those I sent you didn't
destroy methods down here
oh now these two lines here and then
it's actually invoking you can see it's
invoking the init method now against the
container doing all the hard work there
for us and if we go right down to the
end we can see that I we're invoking the
destroy method as well and we can see
that we've got the airport from our log
from our pre destroy method so that we
know that that code is being executed so
if the help of this logging we can
easily see what's happening now in the
background when these beans are now
created so it's a quick sum up now
before we finish the video best practice
here is to use post construct and pre
destroy methods instead of specifying
them using XML then in the myth in the
next video we're going to talk about
being Auto scanning an auto wiring which
is an important concept to understand
I'll see you in the next video