简体   繁体   中英

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?

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.

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"})

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 . ie if you put that class in com.mypackage , then it will scan for all classes in sub-packages ie 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 ie @ComponentScan("com.mypackage")

I would recommend you have a base package ie com.mypackage . And within those packages, have your sub-packages. Have you class containing the @SpringBootApplication in that base package.

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"})

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM