今天讲springboot项目中的启动类换到了start包下,发现无法扫描并注册javabean.
原因:
容器在启动时会由spring.classPathBeanDefinitionScanner和spring-mybaits.classPathMapperScanner两个类去执行doScan方法,如果没有使用@ComponentScan和MapperScan两个注解
spring会使用SpringBootApplication注解中的compontScan的扫描路径 默认扫描路径是启动类所在包下
spring-mybaits会扫描SpringBootApplication注解中的EnableAutoConfiguration注解所在类的包下(也就是启动类包下)
因此需要扫描的组件所在包如果不放在启动类所在包下,则必须使用ComponentScan和MapperScan才能被扫描到生成实例让spring容器管理
如果只使用compontScan注解而不使用mapperscan注解 则mybatis无法生成对应的mapper实例对象 导致注入失败 报错原因如下 (因为spring-mybatis扫描时的根路径是
SpringBootApplication注解中的EnableAutoConfiguration注解所在类的包下,所以成功生成mapper对象,导致注入失败
)
两次扫描的代码(https://www.cnblogs.com/zeus00456/p/16528993.html)
标签:springboot,包下,spring,扫描,ComponentScan,MapperScan,注解 From: https://www.cnblogs.com/UUUz/p/18240944