报错信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in com.zyr.springbootdemo.sys.LoginController required a single bean, but 2 were found:
- sysUserServiceImpl: defined in file [F:\workspace\springboot-demo-mp\target\classes\com\zyr\springbootdemo\sys\user\service\impl\SysUserServiceImpl.class]
- sysUserService: defined in file [F:\workspace\springboot-demo-mp\target\classes\com\zyr\springbootdemo\sys\user\service\SysUserService.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Disconnected from the target VM, address: '127.0.0.1:50631', transport: 'socket'
Process finished with exit code 1
当在使用spring boot开发的过程中,通常需要实现control层,service层,dao层。其中service层的实现使用@service注解,dao层的实现使用@mapper或者@mapperscan注解。当调用时使用@Autowired注解。
当我们使用 接口 + 实现类的方式实现service层时,只需要在实现类上增加@service注解,正常情况下spring boot的bean容易只应该保存一个该类型的bean实例。在control层可以进行一下调用。
但在使用@mapperscan注解(不指定扫描包)的情况下会出现上面的异常。
原因是由于使用的是@mapperscan注解的问题。
@SpringBootApplication
@MapperScan("com.zyr.springbootdemo")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
当启动springboot时启动时,由于存在@MapperScan注解,因此spring boot会将所有的接口(interface)生成一个bean实列保存到容器中。
解决方法有一下几种:
1、注入的变量名写清楚,SysUserService 变量名不能写成userService,这样就会找到两个,接口类和实体类
2.放弃使用@MapperScan
3.指定@MapperScan的扫描路径