The Spring Boot team wanted to make you life easy and by default all the packages located under the package where the annotation @SpringBootApplication
is found are scanned. In your example it means under the package of the class ReadingListApplication
and the package readinglist
(and below).
Following the example, as you did you can create a controller, a repository and a bean Book (based on the name we know it is from the domain).
Doing so there is some extra. You can define beans into the class ReadingListApplication
, and these beans will be scanned. You can define Java configuration under the package readinglist
and these beans will be scanned.
Nothing to be configured (only @SpringBootApplication
to be used).
If you want to define a class outside the readinglist
package then you need some configuration.
From the IDE or from the Java doc, look what is inside the annotation @SpringBootApplication
and you will find scanBasePackages
.
The parameter scanBasePackages
does configure the packages to be scanned.
If you want to add some extra packages like you did into your example, you have to use this annotation.
@SpringBootApplication(scanBasePackages = {"readinglist","entertainment"})
Of course, you have to add the package "readinglist" back because well, you configure the scanning explicitely and add all the extra packages you want, and in your example, only one, the package entertainment
.
This way, both packages readinglist
and entertainment
(and of course below) will be scanned. You can for example, put some Java config into entertainment
and these beans will be scanned.