SpringBoot项目web模块的启动类本来是这样写的:
@ComponentScan("cn.onesdream")
@MapperScan("cn.onesdream")
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
之前一直以为,包扫描的范围写的大一点其实也没有什么,至多就是影响一点扫描的速度。
但是,在今天出现了一个问题,当调用一个接口的时候,报500的错误,查看控制台发现提示有一个类方法不能找到,错误如下:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.onesdream.service.ArticleSerive.list
......
执行maven中的install命令,报错如下:
Description:
Field articleMapper in cn.onesdream.service.impl.ArticleServiceImpl required a bean of type 'cn.onesdream.dao.mapper.ArticleMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'cn.onesdream.dao.mapper.ArticleMapper' in your configuration.
......
一直折腾了很久,还是没有解决,这个问题实在怪异,明明是有的类啊。
然后无意中发现启动时的控制台有两条警告信息:
2020-05-23 17:49:25.515 WARN 16900 --- [ main] o.m.s.mapper.ClassPathMapperScanner : Skipping MapperFactoryBean with name 'articleMapper' and 'cn.onesdream.dao.mapper.ArticleMapper' mapperInterface. Bean already defined with the same name!
2020-05-23 17:49:25.516 WARN 16900 --- [ main] o.m.s.mapper.ClassPathMapperScanner : Skipping MapperFactoryBean with name 'regdataMapper' and 'cn.onesdream.dao.mapper.RegdataMapper' mapperInterface. Bean already defined with the same name!
看到这两个警告才开始意识到这两个包扫描之间有了冲突。
于是修改启动类:
@ComponentScan("cn.onesdream")
@MapperScan("cn.onesdream.dao.mapper")
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
启动,一切正常。