'Configuration using annotation @SpringBootApplication
I have problem with Spring Boot configuration.
I have created base Spring Boot project using https://start.spring.io/
And I have a problem, configuration works only for classes in sub catalog:
I have tried annotation @ComponentScan but it didn't help.
Do You have any idea what can I do with this?
Solution 1:[1]
Checking the Spring documentation:
You can override, with the @SpringBootApplication, the default values of component scan. You just need to include it as a parameters:
@SpringBootApplication(scanBasePackages = "entertainment")
or String array:
@SpringBootApplication(scanBasePackages = {"entertainment", "readinglist"})
Solution 2:[2]
When setting up a Spring boot project, have your Application class (the one that contains the @SpringBootApplication annotation in the base package.
One of the things the @SpringBootApplication does is a component scan. But, it only scans on sub-packages. i.e. if you put that class in com.mypackage, then it will scan for all classes in sub-packages i.e. com.mypackage.*.
If you do not want to do it this way, you can also add a @ComponentScan to a class specifying the root package i.e @ComponentScan("com.mypackage")
I would recommend you have a base package i.e com.mypackage. And within those packages, have your sub-packages. Have you class containing the @SpringBootApplication in that base package.
Solution 3:[3]
I was having the same problem and to solve it I renamed my packages like this.
"com.project"
there you can place your SpringBootAplication main class, then just create the others packages beginning with "com.project"
"com.project.dao"
"com.project.controller"
Creating this sub project structure you have no need to use scanBasePackages in @SpringBootApplication annotation, doing this your main class will be able to find every component in your project.
And in case you chose to use scanBasePackages remember that you need to set all your components packages like this.
@SpringBootApplication(scanBasePackages = {"com.project.dao", "com.project.controller"})
Solution 4:[4]
For the scanning of packages to really work, you must do as follows.
@SpringBootApplication(scanBasePackages = {"com.your.package.test.*.*"})
The first asterisk tells you to scan all packages within the main path (com.your.package.test) and the second asterisk tells you to scan all files in each package.
For example:
com.your.package.test
|_ config
|_ business
|_ controller
|_ domain
|_ repository
Solution 5:[5]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paulo Merson |
| Solution 2 | Abdullah Khan |
| Solution 3 | Community |
| Solution 4 | Nick Borges |
| Solution 5 |

