SpringBoot has a problem that beans cannot be scanned by spring.

SpringBoot has a problem that beans cannot be scanned by spring. 

 Mostly it has something to do with the location of the file package.

 

Springboot's bean assembly rules: 

Scan from top to bottom according to the package location where the Application class is located! 

"Application class" refers to the project entry class of springboot. The location of this class is critical: 

If the package where the Application class is located is: io.github.gefangshuai.app

Then the scanning location is: io.github.gefangshuai.app package and all subpackages under it.

If the bean that needs to be injected is not under the io.github.gefangshuai.app package and its subpackages, it will not be scanned.

 

Note:

You can specify scanned packages by adding @ComponentScan(basePackages={"xxx.xxx","xxx.xxx"}) to the Application class,

But once specified, the package under the Application class will not be scanned by default.

 

Example:

@ComponentScan(basePackages={"cn.kfit","org.kfit","com.kfit"})  

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326190073&siteId=291194637