Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,170 --> 00:00:03,920
-: This video will cover Component Scanning with Spring.
2
00:00:07,320 --> 00:00:09,210
Spring will scan your Java classes
3
00:00:09,210 --> 00:00:12,030
or special annotations such as @Component,
4
00:00:12,030 --> 00:00:14,070
and it'll automatically register the beans
5
00:00:14,070 --> 00:00:15,573
in the Spring container.
6
00:00:19,050 --> 00:00:21,213
Let's look at some Java source code here.
7
00:00:22,050 --> 00:00:24,150
So in our project, we currently have
8
00:00:24,150 --> 00:00:26,220
this Spring core demo application.
9
00:00:26,220 --> 00:00:28,560
That's our main Spring Boot application class
10
00:00:28,560 --> 00:00:30,960
that was created by the Spring Initializer.
11
00:00:30,960 --> 00:00:32,970
We also have our RestController that we created
12
00:00:32,970 --> 00:00:35,823
in an earlier video, called DemoController.
13
00:00:40,680 --> 00:00:43,350
Now, in that Spring core demo application,
14
00:00:43,350 --> 00:00:45,570
the one that was created by the Spring Initializer,
15
00:00:45,570 --> 00:00:49,140
note here that it does an import on Spring Boot Application.
16
00:00:49,140 --> 00:00:52,710
So this enables auto configuration, component scanning
17
00:00:52,710 --> 00:00:55,293
and additional configuration with Spring Boot.
18
00:00:57,060 --> 00:00:59,760
And really behind the scenes, this annotation is composed
19
00:00:59,760 --> 00:01:01,890
of the following annotations, enable auto
20
00:01:01,890 --> 00:01:05,673
configuration, component scan and configuration.
21
00:01:08,970 --> 00:01:11,520
So, a bit more here in these annotations.
22
00:01:11,520 --> 00:01:13,140
The @SpringBootApplication is composed
23
00:01:13,140 --> 00:01:14,853
of the following annotations.
24
00:01:15,956 --> 00:01:18,480
@EnableAutoConfiguration enables Spring Boot's
25
00:01:18,480 --> 00:01:20,587
auto configuration support,
26
00:01:20,587 --> 00:01:22,830
@ComponentScan enables component scanning
27
00:01:22,830 --> 00:01:24,600
of the current package and also
28
00:01:24,600 --> 00:01:27,150
recursively scans the sub-packages,
29
00:01:27,150 --> 00:01:29,160
and the @Configuration annotation is able to
30
00:01:29,160 --> 00:01:32,040
register extra beans with the @Bean annotation
31
00:01:32,040 --> 00:01:34,830
or import other configuration classes.
32
00:01:34,830 --> 00:01:35,970
I'll show you some examples
33
00:01:35,970 --> 00:01:38,973
of the configuration annotation a bit later in the course.
34
00:01:42,450 --> 00:01:44,280
And now back to our Java Source Code.
35
00:01:44,280 --> 00:01:45,390
So we know the information here
36
00:01:45,390 --> 00:01:46,950
about the Spring Boot application.
37
00:01:46,950 --> 00:01:49,500
Next we have this Spring application.
38
00:01:49,500 --> 00:01:52,110
And so this allows us to bootstrap our Spring
39
00:01:52,110 --> 00:01:54,660
Boot application, and then we give a reference here
40
00:01:54,660 --> 00:01:56,700
to the actual name of our class,
41
00:01:56,700 --> 00:01:58,523
in this case, SpringcoredemoApplication.
42
00:02:02,190 --> 00:02:04,440
So behind the scenes this will create the application
43
00:02:04,440 --> 00:02:07,350
context, register all the beans, and also start
44
00:02:07,350 --> 00:02:11,253
the embedded server such as Tomcat, et cetera, by default.
45
00:02:14,910 --> 00:02:17,910
Now, a bit more here on component scanning.
46
00:02:17,910 --> 00:02:20,010
By default, Spring Boot starts component scanning
47
00:02:20,010 --> 00:02:23,670
from the same package as your main Spring Boot application.
48
00:02:23,670 --> 00:02:26,940
And also it scans the sub-packages recursively.
49
00:02:26,940 --> 00:02:29,340
This implicitly defines a base search package
50
00:02:29,340 --> 00:02:30,780
that you can make use of.
51
00:02:30,780 --> 00:02:33,330
So it allows you to leverage default component scanning
52
00:02:33,330 --> 00:02:35,400
without having to explicitly referencing
53
00:02:35,400 --> 00:02:36,723
the base package name.
54
00:02:40,320 --> 00:02:43,650
So here's a diagram to kind of pull this together.
55
00:02:43,650 --> 00:02:45,960
We have our Main Spring Boot application class.
56
00:02:45,960 --> 00:02:47,730
It automatically component scans
57
00:02:47,730 --> 00:02:49,800
the package and sub-packages.
58
00:02:49,800 --> 00:02:52,380
We can create any other sub-packages that we want.
59
00:02:52,380 --> 00:02:54,633
We can give these sub-packages any name,
60
00:02:55,950 --> 00:02:59,940
and then it scans everything in core luv2code.springcoredemo
61
00:02:59,940 --> 00:03:02,550
package, and any sub-packages.
62
00:03:02,550 --> 00:03:03,870
So basically it starts scanning
63
00:03:03,870 --> 00:03:06,210
at the Main Spring Boot application class level
64
00:03:06,210 --> 00:03:08,553
and then all sub-packages underneath that.
65
00:03:11,730 --> 00:03:14,430
Now, a common pitfall when you're making use
66
00:03:14,430 --> 00:03:15,570
of Spring Boot, you may say,
67
00:03:15,570 --> 00:03:17,460
Hey, I'm going to use different packages
68
00:03:17,460 --> 00:03:20,130
and move things around and change things up or whatever.
69
00:03:20,130 --> 00:03:21,273
Here's an example.
70
00:03:22,170 --> 00:03:25,590
So we have our Spring Core Demo, that's the package
71
00:03:25,590 --> 00:03:28,020
of our Main Spring Boot application class.
72
00:03:28,020 --> 00:03:31,650
Then, you may create other packages outside of that.
73
00:03:31,650 --> 00:03:34,110
So using this example here of demo utils,
74
00:03:34,110 --> 00:03:37,680
notice here that it's outside of our Spring Core Demo.
75
00:03:37,680 --> 00:03:38,820
And so by default,
76
00:03:38,820 --> 00:03:41,880
Spring Boot will not component scan these packages.
77
00:03:41,880 --> 00:03:43,260
It will only scan the package
78
00:03:43,260 --> 00:03:47,400
of the Main Spring Boot application class and sub-packages.
79
00:03:47,400 --> 00:03:48,843
So this is very important.
80
00:03:54,720 --> 00:03:56,460
So, the default scanning works fine
81
00:03:56,460 --> 00:04:00,060
if everything is under com.luv2code.springcoredemo.
82
00:04:00,060 --> 00:04:01,800
But what about my other packages?
83
00:04:01,800 --> 00:04:03,630
Like I want to use some different names or whatever
84
00:04:03,630 --> 00:04:08,460
like com.luv2code.util, or org.acme.cart,
85
00:04:08,460 --> 00:04:12,360
or edu.cmu.spirit racing systems.
86
00:04:12,360 --> 00:04:13,440
How will this kind of work out,
87
00:04:13,440 --> 00:04:15,750
or how can I configure this accordingly?
88
00:04:15,750 --> 00:04:16,890
Well, what you can do is,
89
00:04:16,890 --> 00:04:19,560
in your Spring Boot application annotation
90
00:04:19,560 --> 00:04:22,320
then you can tell it to scan base packages.
91
00:04:22,320 --> 00:04:24,210
So, here I'm going to explicitly list
92
00:04:24,210 --> 00:04:25,890
the base packages to scan.
93
00:04:25,890 --> 00:04:28,020
And you simply give a comma'd limited list
94
00:04:28,020 --> 00:04:30,870
of those packages that you want Spring Boot to scan.
95
00:04:30,870 --> 00:04:33,360
So, give our com.luv2code.sprincoredemo,
96
00:04:33,360 --> 00:04:38,253
and then luv2code.util, org.acme.cart, edu.cmu.srs.
97
00:04:40,830 --> 00:04:42,840
All right, so this is all really good stuff.
98
00:04:42,840 --> 00:04:44,040
We're going to move into the next video,
99
00:04:44,040 --> 00:04:45,300
we're going to write the code.
100
00:04:45,300 --> 00:04:46,140
I'll show you how to make use
101
00:04:46,140 --> 00:04:48,270
of default scanning, and also I'll show you how
102
00:04:48,270 --> 00:04:51,900
to manually list the actual package scanning.
103
00:04:51,900 --> 00:04:54,123
I'll see ya in the next video.
8254
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.