Configuration Using @SpringBootApplication Annotation

I have a problem with Spring boot configuration.

I created a basic Spring boot project using https://start.spring.io/

And I have a problem, the setting only works for classes in a subdirectory:

enter image description here

I tried the @ComponentScan annotation but that didn't help.

Do you have any idea what I can do with this?

+23
java spring spring-boot gradle
source share
5 answers

Spring Download Documentation for @SpringBootApplication contains

Many Spring Boot developers always have their main class annotated with @Configuration , @EnableAutoConfiguration and @ComponentScan . Because these annotations are so often used together (especially if you follow the recommendations above), Spring Boot provides a convenient alternative to @SpringBootApplication .

The @SpringBootApplication annotation @SpringBootApplication equivalent to using @Configuration , @EnableAutoConfiguration and @ComponentScan with their default attributes : [...]

where is @ComponentScan javadoc state

If specific packages are not defined, the scan will come from a class package that declares this annotation.

That is, only those types that are in the same package as your ReadingListApplication will be ReadingListApplication .

If you need custom configuration, specify your own @Configuration , @EnableAutoConfiguration and @ComponentScan , if necessary.

+53
source share

When setting up your Spring boot project, use your application class (the one that contains the @SpringBootApplication annotation in the base package.

One of what @SpringBootApplication does is scan components. But it only scans subpackages . that is, if you put this class in com.mypackage, it will scan all classes in subpackages, i.e. com.mypackage. *.

If you do not want to do this, you can also add @ComponentScan to the class that defines the root package ie @ComponentScan("com.mypackage")

I would recommend you have the base package ie com.mypackage. And these packages have their own subpackages. You have a class containing @SpringBootApplication in this base package.

+15
source share

Checking out the Spring documentation:

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html

With @SpringBootApplication, you can override the default values ​​for scanning components. You just need to include it as parameters:

@SpringBootApplication(scanBasePackages = "entertainment")

or an array of strings:

@SpringBootApplication(scanBasePackages = {"entertainment", "readinglist"})

+12
source share

I had the same problem, and to solve it, I renamed my packages as follows.

"com.project"

there you can place the main SpringBootAplication class, and then just create the rest of the packages starting with "com.project"

"com.project.dao"

"com.project.controller"

By creating this subproject structure, you do not need to use scanBasePackages in the @SpringBootApplication annotation, by doing this, your main class will be able to find every component in your project.

And if you decide to use scanBasePackages, remember that you need to install all your component packages like this.

@SpringBootApplication (scanBasePackages = {"com.project.dao", "Com.project.controller"})

+2
source share

All Articles