Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:01,230 --> 00:00:02,950
Alright, so moving into Eclipse,
2
2
00:00:02,950 --> 00:00:05,100
we're going to go ahead and write some code.
3
3
00:00:05,100 --> 00:00:07,660
So, we'll make use of that existing project
4
4
00:00:07,660 --> 00:00:10,010
that we've had, Spring Demo One,
5
5
00:00:10,010 --> 00:00:11,820
and this is the same project that we had
6
6
00:00:11,820 --> 00:00:13,460
in our previous videos as far as making use
7
7
00:00:13,460 --> 00:00:15,540
of our Coach, Baseball Coach, and Track Coach.
8
8
00:00:15,540 --> 00:00:17,733
So again, the same project there,
9
9
00:00:21,340 --> 00:00:22,520
and what we want to do is make use of
10
10
00:00:22,520 --> 00:00:24,880
some code that we've downloaded for this course.
11
11
00:00:24,880 --> 00:00:26,810
So in the previous videos, we gave you the links
12
12
00:00:26,810 --> 00:00:29,710
on how to download the course files.
13
13
00:00:29,710 --> 00:00:31,070
So once you download them, you'll get
14
14
00:00:31,070 --> 00:00:34,150
this spring-and-hibernate-source-code.zip.
15
15
00:00:34,150 --> 00:00:36,300
You can extract it, and then you'll get this photo.
16
16
00:00:36,300 --> 00:00:38,230
So again, look at the previous videos
17
17
00:00:38,230 --> 00:00:40,460
in the courses for how to download
18
18
00:00:40,460 --> 00:00:42,810
the actual course zip file, but anyways,
19
19
00:00:42,810 --> 00:00:45,740
once you have it, you've unzipped it,
20
20
00:00:45,740 --> 00:00:47,860
there's this spring hibernate source code,
21
21
00:00:47,860 --> 00:00:49,070
and we'll look in this directory here
22
22
00:00:49,070 --> 00:00:51,520
called Spring Core because what we want
23
23
00:00:51,520 --> 00:00:54,100
to do is actually copy a starter file
24
24
00:00:54,100 --> 00:00:56,713
that'll help us out with the XML configurations.
25
25
00:00:58,320 --> 00:01:00,210
So, I just moved down into Spring Demo One,
26
26
00:01:00,210 --> 00:01:01,810
Starter File, and there's a file
27
27
00:01:01,810 --> 00:01:04,560
there called applicationContext.xml.
28
28
00:01:04,560 --> 00:01:07,530
It's just a basic starter file, basic shell,
29
29
00:01:07,530 --> 00:01:09,720
but let's go ahead and copy this file,
30
30
00:01:09,720 --> 00:01:11,663
just do a right click and choose copy,
31
31
00:01:14,170 --> 00:01:16,420
and let's move back over to Eclipse,
32
32
00:01:16,420 --> 00:01:18,750
and we want to place this in our source directory.
33
33
00:01:18,750 --> 00:01:21,240
Alright, so highlight that source directory,
34
34
00:01:21,240 --> 00:01:23,780
do a right click, and then choose paste
35
35
00:01:25,240 --> 00:01:26,710
because what this'll actually do is it'll
36
36
00:01:26,710 --> 00:01:28,560
paste the file and actually put it on
37
37
00:01:28,560 --> 00:01:31,170
our applications class path so we can make use
38
38
00:01:31,170 --> 00:01:34,270
of our class path application context.
39
39
00:01:34,270 --> 00:01:35,320
So, there's our file.
40
40
00:01:35,320 --> 00:01:37,510
Now if we just double click this file,
41
41
00:01:37,510 --> 00:01:40,630
our first step is configuring our Spring Beans.
42
42
00:01:40,630 --> 00:01:43,360
Alright, so this file has a lot of
43
43
00:01:43,360 --> 00:01:45,870
header information here for the xml name spaces,
44
44
00:01:45,870 --> 00:01:48,160
and that's what's required for Spring
45
45
00:01:48,160 --> 00:01:50,170
to be able to process this file using
46
46
00:01:50,170 --> 00:01:53,480
its grammar and xml schema,
47
47
00:01:53,480 --> 00:01:55,420
and now let's go ahead and define our bean.
48
48
00:01:55,420 --> 00:01:57,420
So, we have this bean, and now
49
49
00:01:57,420 --> 00:01:59,820
I need to set up two attributes.
50
50
00:01:59,820 --> 00:02:02,650
The first attribute I need to set is ID equals,
51
51
00:02:02,650 --> 00:02:05,540
and this is basically our alias of what the app will use.
52
52
00:02:05,540 --> 00:02:09,170
I'll just call it My Coach, and then I give class.
53
53
00:02:09,170 --> 00:02:12,630
Now I give the fully qualified class name
54
54
00:02:12,630 --> 00:02:14,460
of our implementation.
55
55
00:02:14,460 --> 00:02:16,100
So in this case, I want the Baseball Coach.
56
56
00:02:16,100 --> 00:02:17,780
So, I know the Baseball Coach is
57
57
00:02:17,780 --> 00:02:19,610
in com.Luv2code.springdemo., and
58
58
00:02:21,360 --> 00:02:22,815
actually this is Track Coach.
59
59
00:02:22,815 --> 00:02:23,648
(chuckling)
60
60
00:02:23,648 --> 00:02:25,443
Sorry, so com.Luv2code.springdemo.TrackCoach,
61
61
00:02:26,980 --> 00:02:29,203
and that's our theme definition there.
62
62
00:02:30,410 --> 00:02:32,250
So, that pretty much covers step one
63
63
00:02:32,250 --> 00:02:35,343
as far as configuring our Spring Beans.
64
64
00:02:37,580 --> 00:02:39,940
Alright, so now let's go ahead and
65
65
00:02:39,940 --> 00:02:41,623
take a look at one other item,
66
66
00:02:46,410 --> 00:02:47,243
and now what we're going to do is
67
67
00:02:47,243 --> 00:02:50,350
actually make use of a java class.
68
68
00:02:50,350 --> 00:02:53,893
So, I'm going to create a new class called Spring Hello App.
69
69
00:02:55,590 --> 00:02:58,730
So, I'll select my package here.
70
70
00:02:58,730 --> 00:03:02,333
I'll do a right click, and I'll say new class,
71
71
00:03:06,890 --> 00:03:09,090
and the actual name of the class here,
72
72
00:03:09,090 --> 00:03:12,163
I'll actually call it Hello Spring App,
73
73
00:03:16,100 --> 00:03:17,400
and also I'll check the box here
74
74
00:03:17,400 --> 00:03:19,750
at the bottom, public static void main,
75
75
00:03:19,750 --> 00:03:22,930
just so it'll give us a main method for our application,
76
76
00:03:22,930 --> 00:03:25,310
and then I'll just hit the finish button.
77
77
00:03:25,310 --> 00:03:27,010
Okay, great, so again, just like before,
78
78
00:03:27,010 --> 00:03:30,143
this gives us a very basic java class.
79
79
00:03:33,400 --> 00:03:35,150
Now, what I like to do is I always like
80
80
00:03:35,150 --> 00:03:37,580
to put in comments first before I start writing code.
81
81
00:03:37,580 --> 00:03:38,820
So, the first thing I want to do
82
82
00:03:38,820 --> 00:03:41,293
here is load the spring configuration file,
83
83
00:03:44,440 --> 00:03:45,510
and then I actually want to go ahead
84
84
00:03:45,510 --> 00:03:48,073
and retrieve a bean from the spring container.
85
85
00:03:50,040 --> 00:03:52,030
Now that I have the bean, then I'll call methods
86
86
00:03:52,030 --> 00:03:54,110
on the bean, or a method on the bean,
87
87
00:03:54,110 --> 00:03:56,040
and then finally just to be nice,
88
88
00:03:56,040 --> 00:03:59,800
I'll close the actual application context.
89
89
00:03:59,800 --> 00:04:01,580
So, that's my basic game plan here
90
90
00:04:01,580 --> 00:04:04,643
for building our Hello Spring App.
91
91
00:04:07,670 --> 00:04:09,240
Alright, so let me stretch out here at the bottom,
92
92
00:04:09,240 --> 00:04:10,910
give myself some white space,
93
93
00:04:10,910 --> 00:04:13,710
and now let's go ahead and cover step two
94
94
00:04:13,710 --> 00:04:15,500
of creating a Spring container.
95
95
00:04:15,500 --> 00:04:16,910
So again, here we're going to use
96
96
00:04:16,910 --> 00:04:20,020
this Class Path XML Application context,
97
97
00:04:20,020 --> 00:04:22,520
and I say new, and that's a really long name,
98
98
00:04:22,520 --> 00:04:24,991
so I'm just going to copy paste that name.
99
99
00:04:24,991 --> 00:04:26,470
(chuckling)
100
100
00:04:26,470 --> 00:04:29,460
I'm taking the shortcut, and paste,
101
101
00:04:29,460 --> 00:04:31,690
and then in the construct, I give
102
102
00:04:31,690 --> 00:04:33,640
the actual name of the config file.
103
103
00:04:33,640 --> 00:04:35,370
So just like in the previous example here,
104
104
00:04:35,370 --> 00:04:37,580
applicationContext.xml.
105
105
00:04:37,580 --> 00:04:39,890
That's the name of our XML config file.
106
106
00:04:39,890 --> 00:04:41,210
Now, that'll give us error message
107
107
00:04:41,210 --> 00:04:43,630
as far as on an import, so we simply hover
108
108
00:04:43,630 --> 00:04:46,370
over that error message, and then we choose
109
109
00:04:46,370 --> 00:04:48,620
that first option of importing.
110
110
00:04:48,620 --> 00:04:51,090
So if you move up top, Eclipse will help you
111
111
00:04:51,090 --> 00:04:56,070
out with importing that class from the appropriate package.
112
112
00:04:56,070 --> 00:04:58,380
So, that's a nice little feature there.
113
113
00:04:58,380 --> 00:05:01,543
Okay, so now let's retrieve the bean from the container.
114
114
00:05:04,140 --> 00:05:06,980
So here, I say context.getBean,
115
115
00:05:06,980 --> 00:05:09,480
and I give myCoach, that's the ID,
116
116
00:05:09,480 --> 00:05:12,590
comma Coach.class, that's the actual name
117
117
00:05:12,590 --> 00:05:15,433
of the interface for our implementation.
118
118
00:05:17,730 --> 00:05:19,610
So now, I'm simply going to call methods on the bean.
119
119
00:05:19,610 --> 00:05:21,060
So, I simply use my system out
120
120
00:05:21,060 --> 00:05:24,180
print line trick, sys out control space,
121
121
00:05:24,180 --> 00:05:27,467
and then I'll say theCoach.getDailyWorkout,
122
122
00:05:32,740 --> 00:05:34,900
and now I simply go through and close the context
123
123
00:05:34,900 --> 00:05:37,230
by saying context.close, just to be nice,
124
124
00:05:37,230 --> 00:05:39,753
just to clean up after ourselves.
125
125
00:05:42,150 --> 00:05:44,160
Alright, so that's kind of the basic game plan
126
126
00:05:44,160 --> 00:05:45,490
here with this Hello Spring App.
127
127
00:05:45,490 --> 00:05:46,470
So, the first thing we do is
128
128
00:05:46,470 --> 00:05:49,010
we load the spring configuration file,
129
129
00:05:49,010 --> 00:05:52,010
then we go through and retrieve the bean from the container,
130
130
00:05:53,010 --> 00:05:55,540
then I call methods on that bean,
131
131
00:05:55,540 --> 00:05:58,910
and then finally I close the context by doing my cleanup,
132
132
00:05:58,910 --> 00:06:00,543
and that's the basic game plan.
133
133
00:06:01,420 --> 00:06:04,331
That's kind of your first Spring application right there.
134
134
00:06:04,331 --> 00:06:05,164
(gentle cheering)
135
135
00:06:05,164 --> 00:06:06,050
Now, let's go ahead and try this out.
136
136
00:06:06,050 --> 00:06:07,330
Let's run it.
137
137
00:06:07,330 --> 00:06:10,523
So, just do a right click, run as a java application,
138
138
00:06:12,530 --> 00:06:14,110
and there we go, boo-ya!
139
139
00:06:14,110 --> 00:06:15,410
Check the output.
140
140
00:06:15,410 --> 00:06:17,640
So here, look at some of the info messages.
141
141
00:06:17,640 --> 00:06:19,790
So, it's loading XML bean definitions
142
142
00:06:19,790 --> 00:06:21,140
from your class app resource,
143
143
00:06:21,140 --> 00:06:22,400
that's our configuration file,
144
144
00:06:22,400 --> 00:06:25,170
applicationContext.XML, and then
145
145
00:06:25,170 --> 00:06:26,610
we've called our sys out print line
146
146
00:06:26,610 --> 00:06:29,397
on the get daily workout, and our track coach said
147
147
00:06:29,397 --> 00:06:33,560
"Hey, we need you to run a hard 5K."
148
148
00:06:33,560 --> 00:06:35,550
So, this looks really good.
149
149
00:06:35,550 --> 00:06:36,670
I'm kind of excited about this.
150
150
00:06:36,670 --> 00:06:38,140
I hope you are too.
151
151
00:06:38,140 --> 00:06:40,270
This is our first real Spring Application,
152
152
00:06:40,270 --> 00:06:42,403
and it actually works which is cool.
153
153
00:06:46,930 --> 00:06:48,630
Alrighty, so just a heads up,
154
154
00:06:48,630 --> 00:06:49,960
when you run your application,
155
155
00:06:49,960 --> 00:06:52,890
you may not see the red logging messages.
156
156
00:06:52,890 --> 00:06:56,390
This is due to new changes in Spring 5.1.
157
157
00:06:56,390 --> 00:06:58,090
However, don't worry.
158
158
00:06:58,090 --> 00:07:01,000
You can configure your app to see the logging message.
159
159
00:07:01,000 --> 00:07:02,460
Simply go to the link here at
160
160
00:07:02,460 --> 00:07:07,270
luv2code.com/spring-xml-logs and I'll walk you
161
161
00:07:07,270 --> 00:07:11,200
through the exact process to add the configuration for this.
162
162
00:07:11,200 --> 00:07:12,033
Alrighty.
163
163
00:07:16,590 --> 00:07:18,650
So again, there's the code up top,
164
164
00:07:18,650 --> 00:07:21,120
and then our message is being displayed
165
165
00:07:21,120 --> 00:07:23,963
there at the bottom, reading the config file,
166
166
00:07:24,810 --> 00:07:26,633
getting the bean from the container,
167
167
00:07:27,879 --> 00:07:30,070
and then simply calling methods on that bean,
168
168
00:07:30,070 --> 00:07:31,710
and that gives us the desired output
169
169
00:07:31,710 --> 00:07:33,533
in our console window, cool.
170
170
00:07:36,870 --> 00:07:38,580
So, let me kind of switch back to
171
171
00:07:38,580 --> 00:07:40,650
our slides here for a second because
172
172
00:07:40,650 --> 00:07:42,900
now we've kind of come full circle here.
173
173
00:07:42,900 --> 00:07:46,450
So, now we have an app that meets the requirements.
174
174
00:07:46,450 --> 00:07:48,490
Our app is configurable based on
175
175
00:07:48,490 --> 00:07:50,530
that configuration file we just worked on,
176
176
00:07:50,530 --> 00:07:52,660
and also we can easily change the coach
177
177
00:07:52,660 --> 00:07:54,770
for another sport by simply putting
178
178
00:07:54,770 --> 00:07:57,030
in a different coach implementation.
179
179
00:07:57,030 --> 00:08:00,340
So, I'd like to say mission accomplished,
180
180
00:08:00,340 --> 00:08:01,870
and I'd like to say that we've met all
181
181
00:08:01,870 --> 00:08:04,403
of those requirements from our manager.
182
182
00:08:04,403 --> 00:08:05,540
(chuckling)
183
183
00:08:05,540 --> 00:08:07,490
So, hey boss, we got it all covered here.
184
184
00:08:07,490 --> 00:08:09,330
We're all done for the day, and thanks
185
185
00:08:09,330 --> 00:08:13,060
to Spring, our life is much easier.
186
186
00:08:13,060 --> 00:08:14,170
So, good job here.
187
187
00:08:14,170 --> 00:08:15,500
Our first Spring app, it's up,
188
188
00:08:15,500 --> 00:08:17,400
it's running, and it's working.
189
189
00:08:17,400 --> 00:08:19,470
A lot of great Spring stuff coming on later,
190
190
00:08:19,470 --> 00:08:22,470
but hey, this is a good start, so give yourself a pat
191
191
00:08:22,470 --> 00:08:24,963
on the back, job well done.
16382
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.