spring boot 多个包启动失败-添加注解扫描多个包
spring boot 多个包启动失败-解决:扫描多个包
表现:启动时提示如下信息:
因为项目需要,将代码结构调整为:
- com.xxx
- xx
- yy
- XxxxApplication
这种组织结构,注意,此处时同一个项目(没有拆分成多个maven模块),有多个包保存不同功能模块的代码。此时启动项目,报错如下:
1 | Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. |
可以看到,初始化各种bean失败(controller、service等)。
解决
推测是spring boot启动类扫描代码的目录错误。在启动类的@SpringBootApplication注解中指定:
1 | @SpringBootApplication(scanBasePackages = {"com.xxx.xx", "com.xxx.yy"}) |
即可解决该问题。
但有引入新问题:
新增mapper后,mapper实例化失败
报出如下异常:
1 | Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. |
解决
添加扫描多个包注解:
在启动类中添加@MapperScan,并写上不同包的mapper:
1 | @MapperScan({"com.xxx.yy.mapper", "com.xxx.xx.mapper"}) |