Bean named ‘userServiceImpl‘ is expected to be of type ‘xxxxx‘ but was actually of type ‘xxxxxx‘

在写测试时发现这个报错
Bean named ‘userServiceImpl‘ is expected to be of type ‘xxxxx‘ but was actually of type ‘xxxxxx‘_第1张图片
原因是在注入依赖时写的是实现类,而不是接口
Bean named ‘userServiceImpl‘ is expected to be of type ‘xxxxx‘ but was actually of type ‘xxxxxx‘_第2张图片
Bean named ‘userServiceImpl‘ is expected to be of type ‘xxxxx‘ but was actually of type ‘xxxxxx‘_第3张图片

解决:将注入实现类改为注入接口
Bean named ‘userServiceImpl‘ is expected to be of type ‘xxxxx‘ but was actually of type ‘xxxxxx‘_第4张图片
然后就查询成功了
Bean named ‘userServiceImpl‘ is expected to be of type ‘xxxxx‘ but was actually of type ‘xxxxxx‘_第5张图片

总结:
如果你的代码中使用了 AOP 或者声明式事务,在注入 Bean 的地方,尽量使用接口类型进行声明,而不是具体的实现类类型。例如,将注入点的类型改为接口类型,如 UserService,而不是具体的实现类类型 UserServiceImpl。

private UserService userService;

你可能感兴趣的:(java报错处理,数据库)