异常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name

这篇具有很好参考价值的文章主要介绍了异常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

项目场景:SpringBoot+Mybatis。

出现这种异常主要是无法创建bean到容器中,主要有以下几种情况:

1.注解没有添加:

controller:

@RestController
@AllArgsConstructor
@RequestMapping("/enterprise")
@Api(value = "企业数据", tags = "企业数据接口")
public class EnterpriseController {
	private final IEnterpriseService service;
}

注:

  • controller类要加入@RestController注解,@AllArgsConstructor注解视情况而定。
  • 引入了private final IEnterpriseService service,所以需要注入,可以在controller类上加入@AllArgsConstrctor注解修饰。
  • 或者用@Autowired修饰注入变量。
    @Autowired
	private final IEnterpriseService service;

service:

@Service
@AllArgsConstructor
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {
	
	public List<Map> countByType() {
		return baseMapper.countByType();
	}
}

注:

  • service层要加入@Service注解。
  • 如果service层中引入了Mapper变量,则需要加入@AllArgsConstrctor注解;当然也可以用@Autowired注解修饰变量。不过推荐使用@AllArgsConstrctor注解,因为只需要在类上引入一次;使用@Autowired注解需要修饰每一个引入的Mapper,极其繁琐。
@Service
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {

    @Autowired
	private final EnterpriseMapper mapper;

    @Autowired
	private final BlogMapper mapper;

	public List<Map> countByType() {
		return baseMapper.countByType();
	}
}

2.接口有没有对应的实现类,以及实现类实现的接口是否正确。

3.项目中的文件是否重名,全局定位异常文件,并修改为不同文件名。

4.启动类中注解是否扫描到所有mapper。

@EnableScheduling
@SpringBootApplication
@ComponentScan({"org.springblade","com.hello","com.test"})
@MapperScan({"com.hello.**.mapper.**","com.test.**.mapper.**"})
public class Application {

	public static void main(String[] args) {
		BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);

	}

}

注:

  • @ComponentScan注解和@MapperScan注解一定要扫描到所有文件路径,多个路径之间用逗号分隔开。
  • 或者在yml配置文件中配置mapper扫描路径。

5.Mapper.xml文件中配置的resultMap对应的实体类路径是否正确,以及各种Mybatis标签对应的resultMap或者resultType路径是否正确。

6.接口对应的地址是否重复。如下图,两个接口均使用了相同的地址,会出现异常。


	@GetMapping("/count")
	public R<EnterpriseVO> test(@RequestBody  Enterprise enterprise){
		EnterpriseVO detail = service.test(enterprise);
		return R.data(detail);
	}
	
	@PostMapping("/count")
	public R<Enterprise> update(@RequestBody Enterprise enterprise){
		boolean flag = service.updateByBody(enterprise);
		Enterprise entity = service.selectInfo(enterprise);
		return R.data(entity);
	}

异常排查技巧:

通常出现这种异常会在控制台打印出一堆异常信息,在控制台上方的异常信息大多是和程序真正的问题没有直接关系的,这里给出以下异常排查技巧:文章来源地址https://www.toymoban.com/news/detail-833800.html

  1. 控制台看报错:从下往上看。
  2. 利用好idea的全局搜索定位功能去排查异常。

到了这里,关于异常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包