[经验记录] SpringBoot报错解决方案汇总

本文档记录了在使用MyBatisPlusGenerator并尝试启动SpringBoot应用时遇到的两个常见错误:1) 缺少AutowireCandidate的Bean错误,主要是由于缺少对学生Mapper的扫描;2) 缺少特定Bean的错误,可能源于未配置DataSource或在Service层注入了错误的对象。解决方案包括在启动类中添加@MapperScan注解以扫描Mapper,以及确保在配置文件中指定数据库连接信息。此外,还应注意避免在Service层错误地注入Model对象。

1. 运行时报错runtime error

1.1 expected at least 1 bean which qualifies as autowire candidate

1.1.1 整体错误描述

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘studentController’: Unsatisfied dependency expressed through field ‘studentService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘studentServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.case02.scu.student.mapper.StudentMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

1.1.2 背景

使用MyBatisPlusGenerator生成器,生成了所有的文件,编译没有任何错误。但直接启动springBoot application时报错。

1.1.3 错误关键信息及分析

从整体错误里,可以看到从controller 到service 到ServiceImpl 到Mapper都有提及,那到底问题在哪里呢?可以看到整个错误是一个嵌套的,也就是说最前面的是Exception的最上层,需要顺着提示,找到最底层throw exception的地方。这里关键就是:

No qualifying bean of type ‘com.case02.scu.student.mapper.StudentMapper’ available
这里可以看到根源是Mapper有问题,那么针对这个类型找如何解决。

1.1.4 解决方案-临时

临时的解决方案就是在SprintBoot启动程序加上Mapper的映射。见代码中@MapperScan部分。

@MapperScan("com/case02/scu/student/mapper")
@SpringBootApplication
public class myApplication {
    public static void main(String[] args) {
        //启动SpringBoot
        SpringApplication.run(myApplication.class,args);
    }
}

比如报错是com.case02.scu.student.mapper.StudentMapper
那mapperscan中的路径就是com/case02/scu/student/mapper
之所以说是临时方案,是因为学习时没见到有这么写的,应该是哪里掌握程度不够导致。后面会学习看整体方案是什么。

1.2 No bean named ‘org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry’ available

1.2.1 整体错误描述

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration’: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration’: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry’ available

1.2.2 背景

使用mybatisplus generator生成文件之后,添加了SpringBoot Application直接运行。编译没报错,运行springboot启动时报错。

1.2.3 错误关键信息及分析

在网上查了,这个错误原因真是非常非常多,总结就是SpringBoot启动错误后抛出的统一异常,再往前看,其实可以看到更详细的信息。

1.2.4 错误关键信息及分析 - 情况1

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

2022-04-18 13:53:36.680  WARN 24260 --- [           main] o.s.boot.SpringApplication               : Unable to close ApplicationContext

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available

所以关键信息是:

Description:
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

那么解决方案 就是:
引入application.yaml 或者application.properties,配置数据库信息

1.2.4 错误关键信息及分析 - 情况2

***************************
APPLICATION FAILED TO START
***************************

Description:

Field studentDO in com.case02.scu.student.service.impl.StudentServiceImpl required a bean of type 'com.case02.scu.student.model.StudentDO' 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 'com.case02.scu.student.model.StudentDO' in your configuration.

2022-04-18 14:53:05.036  WARN 11228 --- [           main] o.s.boot.SpringApplication               : Unable to close ApplicationContext

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available

以上的关键信息就是

Field studentDO in com.case02.scu.student.service.impl.StudentServiceImpl required a bean of type ‘com.case02.scu.student.model.StudentDO’ that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

看了我的代码,原来是StudentServiceImpl 里抄了这样的语句:

public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentDO> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentDO studentDO;

把以下语句删掉即可

    @Autowired
    private StudentDO studentDO;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值