Narrator: Alrighty, so let's go ahead 2 2
and move into Eclipse. 3 3
Looking at our development process, 4 4
the first thing we want to do is define methods 5 5
for init and destroy. 6 6
So I'll go ahead and do this here. 7 7
I'll make use of my TrackCoach.java class. 8 8
So let's go ahead and move into this class. 9 9
And we are going to just open it up. 10 10
And we'll simply scroll down a bit 11 11
to the bottom, and we'll define those new methods. 12 12
So again, I always like to drop in 13 13
some quick comments here just so I keep myself on track. 14 14
So I want to add an init method, 15 15
and I want to add a destroy method. 16 16
So again, this is custom code that will execute 17 17
during the bean's lifecycle, 18 18
when the bean's being created 19 19
or the bean's being destroyed, our little custom hook. 20 20
And now what I want to do, just inside of this method here, 21 21
just print out a little message here 22 22
just so I know I'm actually inside the method. 23 23
So I'll say TrackCoach: inside method 24 24
do my startup stuff. 25 25
So just to make sure that spring's actually calling 26 26
my initialization method. 27 27
But that's basically it right there. 28 28
So this looks pretty good so far. 29 29
Alright, so let's move on down 30 30
and let's do a similar thing in here for our destroy method. 31 31
So again, this is a hook or a method that'll be called 32 32
when your bean's actually being destroyed 33 33
by the spring container. 34 34
So I'll call this doMyStartupStuff, 35 35
and I'll copy/paste the method down there. 36 36
And I'll just copy/paste that method name, and ew. 37 37
I notice this is the wrong method name. 38 38
I have a typo here, so let me change this. 39 39
So instead of doMyStartupStuff, doMyCleanupStuffYoYo. 40 40
Alright, doMyCleanupStuff. 41 41
So I'll copy/paste. 42 42
I'll fix that issue. 43 43
So again, that's different method names. 44 44
So this is our destroy method, 45 45
so I'll call it doMyCleanupStuffYoYo. 46 46
And I'm being funny here just to verify 47 47
that, hey, there's no special method naming convention. 48 48
You can give it any method name that you want. 49 49
Alright, so that looks good. 50 50
So that's our init method, and that's our destroy method. 51 51
Good job so far. 52 52
Now let's go ahead and make use of a configuration file. 53 53
And what I'm gonna do here is I'm gonna 54 54
just kind of copy/paste a previous config file 55 55
and simply make some changes to it. 56 56
So I'm gonna copy/paste the beanScope-applicationContext.xml 57 57
from the previous video. 58 58
Simply do just a right-click and choose Copy 59 59
and then a right-click and choose Paste. 60 60
And then for the new name that I'll give here, 61 61
I'll call it beanLifeCycle-applicationContext 62 62
and remove the two at the end, .xml. 63 63
So again, we can have multiple config files, right? 64 64
So I'm just giving a unique name here 65 65
for this demo project. 66 66
And if you didn't complete the previous video, 67 67
simply grab any one of your XML files and just rename it. 68 68
So anyway, let's take a look 69 69
at this beanLifeCycle-applicationContext.xml, 70 70
and let's kind of expand the window here. 71 71
And let's move down. 72 72
Now there's one thing that's a leftover 73 73
from the previous video as far as playing around 74 74
with bean scopes. 75 75
So on line 19, I'm gonna delete this entry, 76 76
scope="prototype". 77 77
I'm gonna remove that because I want to make use 78 78
of simply just the default scope, right? 79 79
And as a quiz question, the default scope is singleton. 80 80
Alright. 81 81
Now here, let's go ahead and add our init method. 82 82
So we need to give the actual method name 83 83
from our bean class. 84 84
So I need to kind of move back over 85 85
to my TrackCoach and find out which init method 86 86
I'll use here. 87 87
So I'll call it doMyStartupStuff. 88 88
I'll simply copy that name, and then 89 89
I'll simply paste it here. 90 90
I use the keyboard for that, copy/paste. 91 91
And there we go. 92 92
So that's my init method. 93 93
That's in place. 94 94
Now let's do a similar thing here 95 95
for the destroy method, so destroy-method equals. 96 96
And again, I need to get that actual method name. 97 97
So I'll kind of move back over to TrackCoach, 98 98
and I'll copy that name. 99 99
So it's doMyCleanupStuffYoYo. 100 100
And then move back to the config, 101 101
and then paste that in there. 102 102
So that's our destroy method that spring will call 103 103
when the bean is being destroyed. 104 104
Alright, this looks pretty good. 105 105
Now I need to have a main program to kind of run everything. 106 106
So again, I'm gonna kind of copy 107 107
one of my previous examples. 108 108
We've been doing a lot of stuff from scratch. 109 109
And here I'm just gonna copy this 110 110
and just make some quick updates to it. 111 111
So here, I'm gonna copy this BeanScopeDemoApp, 112 112
BeanScopeDemoApp.java, tongue twister. 113 113
I'll just copy that, and I will just paste it. 114 114
And I'll give it a new name, so I'll call this 115 115
BeanLifeCycleDemoApp, take off the two at the end. 116 116
And if everything's okay, then we can click the OK button. 117 117
Alright, so we should have this new file here, 118 118
BeanLifeCycleDemoApp.java. 119 119
So let's go ahead and double-click that one, 120 120
open it up, expand the window. 121 121
Cool, now the first thing I want to do here is on line 11 122 122
where we have the actual file name. 123 123
I want to change this file name 124 124
to use my LifeCycle example. 125 125
Right, so it has to say 126 126
beanLifeCycle-applicationContext.xml. 127 127
You want to make sure that it's calling 128 128
the right config file for your application. 129 129
Alright, and then we'll kind of scroll down a bit. 130 130
I'll delete a lot of that other stuff that was there. 131 131
So we should have just a very simple program right now. 132 132
So again, we did a little copy/paste 133 133
to kind of help us out, but just a real small program: 134 134
load the config, retrieve the bean, 135 135
and close the context. 136 136
So just a little checkpoint to make sure 137 137
we're on the same page here. 138 138
And now I'm just gonna use the bean 139 139
once I've retrieved it from the container. 140 140
I'll just print out getDailyWorkout 141 141
just to print something to the screen, nothing big there. 142 142
And that's pretty much it for this little demo here 143 143
for the LifeCycle. 144 144
So we simply load the config file. 145 145
We retrieve the bean from the container, 146 146
call methods on it, and finally we close the spring context. 147 147
Alright, so that's it. 148 148
It looks good. 149 149
BeanLifeCycleDemoApp, it's ready. 150 150
Let's save it, and let's run it. 151 151
Right-click, Run as Java Application. 152 152
And cool, here's our output. 153 153
So, a couple of things here. 154 154
Make sure that it's reading from the right config file, 155 155
beanLifeCycle-applicationContext.xml. 156 156
So that's the correct file that we just created. 157 157
We see that it's calling our method, 158 158
inside method doMyStartupStuff, which is good. 159 159
That's our init method. 160 160
We call our daily workout which is great. 161 161
And then we see when this container's closing, 162 162
at the end it'll actually call our destroy method. 163 163
So, inside method doMyCleanupStuffYoYo. 164 164
So this is great. 165 165
So spring's actually calling our init method as desired 166 166
and it's also calling our destroy method. 167 167
So this is great. 168 168
So we can add our own custom hooks here 169 169
into the spring bean lifecycle.