SpringBoot出现bean无法被spring扫描到的问题.

SpringBoot出现bean无法被spring扫描到的问题. 

 多半是跟文件包的位置有关系。

springboot的bean装配规则: 

根据Application类所在的包位置从上到下进行扫描的! 

“Application类”是指springboot的项目入口类.这个类的位置很关键: 

如果Application类所在的包为:io.github.gefangshuai.app

那么扫描的位置就是:io.github.gefangshuai.app包及其下的所有子包.

如果需要注入的bean不在io.github.gefangshuai.app包及其子包下,则不会被扫描到.

注:

可以通过对Application类添加@ComponentScan(basePackages={“xxx.xxx”,”xxx.xxx”})来指定扫描的包,

但是一旦指定后就不会在默认扫描Application类下的包.

示例:

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

猜你喜欢

转载自huangqiqing123.iteye.com/blog/2413009
今日推荐