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 wor
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.
Checking the Spring documentation:
http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html
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"})
The Spring Boot documentation for @SpringBootApplication states
Many Spring Boot developers always have their main class annotated with
@Configuration
,@EnableAutoConfiguration
and@ComponentScan
. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient@SpringBootApplication
alternative.The
@SpringBootApplication
annotation is equivalent to using@Configuration
,@EnableAutoConfiguration
and@ComponentScan
with their default attributes: [...]
where the @ComponentScan javadoc states
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.
That is, only the types that are in the same package as your ReadingListApplication
will be scanned.
If you want a custom configuration, provide your own @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
, as appropriate.
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
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"})