ruoyi框架启动报错:
"Error creating bean with name 'sysConfigServiceImpl': Invocation of init method failed;"
和"Error creating bean with name 'captchaController': Unsatisfied dependency expressed through field 'XXX'; "
和"Exception encountered during context initialization "
-
这三个错时一般有几种情况,查资料全部整理出来了:
- 检查项目中所有.yml文件的数据库密码和redis密码(90%的问题都出在这里)
- 检查JDK版本
- 查看xml配置文件路径
- 查看xml中的方法名id是否重复了
- 对应的接口ServiceImpl文件是否有:@Service
- serives和serviceImpl没有对应的方法
- xml中方法的参数类型paramterType不对
- application放置的位置不对
- 类忘记加 @Repository 注解
- mybatis-config.xml文件缺少了表头尖括号内容 如下:

- 在mapper.xml中resultMap的type中包的路径错误,在这个项目中就是因为.xml中的包路径错误导致
- 查看扫描方法以及范围是不是没有把文件路径包含在内
- pom.xml缺少了某些插件
- redis服务异常
- Spring 容器初始化
sysConfigServiceImpl这个 Bean 时,依赖注入失败 - 项目初始化方法执行异常
- 数据库相关依赖缺失
总结出来有四个大的排查方向:
- 数据库连接正常(能查询
sys_config表); - MyBatis 映射文件 / 接口正确(能操作
sys_config表); - 依赖的 Bean(如
sysConfigMapper、Redis)已正常初始化; - 配置文件(如
application.yml)中数据库、Redis 等参数正确。
下面是我项目报错的点:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'captchaController': Unsatisfied dependency expressed through field 'configService111'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysConfigServiceImpl': Invocation of init method failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException:
-
sysConfigServiceImpl 这个类是 RuoYi 系统配置的核心服务类,其初始化逻辑主要是 加载系统配置到缓存
这种情况下,最先排查的就是检查项目中所有.yml文件的数据库密码和redis密码,因为基本上报
Error creating bean with name 'sysConfigServiceImpl': Invocation of init method failed
这个错时,90%的问题都出在这里,后面跟的其他报错信息只是来凑热闹,非常具有迷惑性
-
如图,这些关系到数据库的地方一个一个排查发现的确少配了一个,修正完就正常了
-
另一种情况是 .xml文件中方法名id重复了,但这个报错就很清晰了,连XML报错的文件路径都爆出来了,如下
//查询冰淇淋数量
<select id="countIceType" resultType="Integer">
select count(1) from ic_ice where ice_type=#{iceType}
</select>
//查询冰淇淋
<delete id="countIceType" parameterType="Long">
delete from ic_ice where ice_type=#{iceType}
</delete>
别的原因有很多大佬的帖子都有说到,我是因为redis密码问题,下面集中记录一下别的情况:
场景 1:最常见 - 数据库连接 / 表结构问题
初始化 sysConfigServiceImpl 时触发 init() 方法,但数据库连接失败 /sys_config 表不存在 / 字段不匹配。典型日志片段:
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection;
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure;
Caused by: org.apache.ibatis.exceptions.PersistenceException: Error querying database. Cause: Table 'ry-vue.sys_config' doesn't exist
解决方案
-
检查数据库连接配置打开
application-druid.yml(或application.yml),核对数据库参数:
spring: datasource:
url: jdbc:mysql://127.0.0.1:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root # 确认账号正确
password: 123456 # 确认密码正确
driver-class-name: com.mysql.cj.jdbc.Driver
本地测试可关闭 useSSL=true(改为 false),避免 SSL 认证失败。
-
检查
sys_config表是否存在- 执行 RuoYi 官方提供的初始化 SQL 脚本(
ry_202xxxxx.sql),确保sys_config表及数据已导入; - 若手动修改过表结构(如新增 / 删除字段),需同步更新
SysConfig实体类和sys_configMapper.xml中的字段映射。
- 执行 RuoYi 官方提供的初始化 SQL 脚本(
-
检查数据库驱动版本匹配
- MySQL 8.0+ 必须用
com.mysql.cj.jdbc.Driver,且驱动版本 ≥ 8.0(如mysql-connector-java:8.0.30); - MySQL 5.7- 用
com.mysql.jdbc.Driver,驱动版本 ≤ 5.1.49; - 驱动版本不匹配会导致连接失败,修改
pom.xml:<!-- MySQL 8.x 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>runtime</scope>
</dependency>
- MySQL 8.0+ 必须用
场景 2:依赖注入失败(UnsatisfiedDependencyException)
这个原因有大佬说是因为Spring 无法注入 sysConfigServiceImpl 依赖的 Bean(如 sysConfigMapper),典型日志:
Unsatisfied dependency expressed through field 'sysConfigMapper';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ruoyi.system.mapper.SysConfigMapper' available
解决方案
-
检查 Mapper 接口是否加注解
SysConfigMapper接口必须加@Mapper注解,或在启动类加@MapperScan扫描 Mapper 包:
// 方案1:Mapper接口加注解
@Mapper
public interface SysConfigMapper { ... }
// 方案2:启动类加扫描(推荐)
@SpringBootApplication
@MapperScan("com.ruoyi.**.mapper") // 扫描所有mapper包
public class RuoYiApplication { ... }
-
检查 Mapper XML 文件路径 / 命名
SysConfigMapper.xml必须放在resources/mapper/system/目录下(与 RuoYi 约定一致);- XML 文件的
namespace必须与 Mapper 接口全类名一致:<mapper namespace="com.ruoyi.system.mapper.SysConfigMapper">
- 确认 XML 中
selectConfigList等方法 ID 与接口方法名一致。
-
检查依赖是否缺失若
sysConfigServiceImpl依赖 Redis(RuoYi 默认缓存配置到 Redis),需确保 Redis 依赖和配置正确:# application.yml 中 Redis 配置
spring:
redis:
host: 127.0.0.1
port: 6379
password: # 无密码则留空,不要填null
database: 0
若不需要 Redis,可注释
sysConfigServiceImpl中@Cacheable注解,或改为本地缓存。
场景 3:init() 方法执行异常(缓存加载失败)
报错特征
sysConfigServiceImpl 的 init() 方法(加载配置到缓存)执行时抛出异常,典型日志:
Caused by: java.lang.NullPointerException: null
at com.ruoyi.system.service.impl.SysConfigServiceImpl.init(SysConfigServiceImpl.java:56)
解决方案
-
检查
init()方法逻辑RuoYi 中sysConfigServiceImpl的init()方法通常是:@PostConstruct // 初始化时执行
public void init() {
// 加载配置到缓存
List<SysConfig> configsList = list();
for (SysConfig config : configsList) {
RedisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
}
}
- 若
list()方法返回null(数据库查询失败),会触发 NPE; - 临时注释
@PostConstruct注解,启动后手动调用init()排查具体报错。
- 若
-
检查 Redis 服务是否启动RuoYi 默认将系统配置缓存到 Redis,若 Redis 未启动 / 连接失败,会导致
init()方法报错:- 启动 Redis 服务(本地执行
redis-server); - 若无需 Redis,修改
application.yml关闭缓存:ruoyi:
cache:
type: local # 改为本地缓存
- 启动 Redis 服务(本地执行
场景 4:包扫描 / 注解配置错误
报错特征
Spring 未扫描到 sysConfigServiceImpl 或其依赖的 Bean,日志含:
No qualifying bean of type 'com.ruoyi.system.service.SysConfigService' available
解决方案
-
检查 Service 类注解
SysConfigServiceImpl必须加@Service注解,且包路径在 Spring 扫描范围内:
@Service
public class SysConfigServiceImpl implements SysConfigService { ... }
-
检查启动类扫描范围RuoYi 启动类的
@SpringBootApplication需覆盖所有业务包(默认com.ruoyi),若手动修改包名,需调整扫描范围:@SpringBootApplication(scanBasePackages = "com.ruoyi") // 确保包含system模块
public class RuoYiApplication { ... }
文章主要讲述了在Ruoyi框架中遇到的启动报错问题,包括ErrorcreatingbeanwithnamesysConfigServiceImpl和ErrorcreatingbeanwithnamecaptchaController。最常见的原因是数据库和redis密码配置错误,其次是XML配置文件中的方法名ID重复和Redis连接问题。通过检查和修正这些问题,可以成功解决大部分启动故障。
1175

被折叠的 条评论
为什么被折叠?



