ruoyi框架启动报错:“Error creating bean with name ‘sysConfigServiceImpl‘: Invocation of init;“或Unsatisfied.

文章主要讲述了在Ruoyi框架中遇到的启动报错问题,包括ErrorcreatingbeanwithnamesysConfigServiceImpl和ErrorcreatingbeanwithnamecaptchaController。最常见的原因是数据库和redis密码配置错误,其次是XML配置文件中的方法名ID重复和Redis连接问题。通过检查和修正这些问题,可以成功解决大部分启动故障。

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 "

-

这三个错时一般有几种情况,查资料全部整理出来了:

  1. 检查项目中所有.yml文件的数据库密码redis密码(90%的问题都出在这里)
  2. 检查JDK版本
  3. 查看xml配置文件路径
  4. 查看xml中的方法名id是否重复了
  5. 对应的接口ServiceImpl文件是否有:@Service
  6. serives和serviceImpl没有对应的方法
  7. xml中方法的参数类型paramterType不对
  8. application放置的位置不对
  9. 类忘记加 @Repository 注解
  10. mybatis-config.xml文件缺少了表头尖括号内容 如下:
  11.  在mapper.xml中resultMap的type中包的路径错误,在这个项目中就是因为.xml中的包路径错误导致
  12. 查看扫描方法以及范围是不是没有把文件路径包含在内
  13. pom.xml缺少了某些插件
  14. redis服务异常
  15. Spring 容器初始化 sysConfigServiceImpl 这个 Bean 时,依赖注入失败
  16. 项目初始化方法执行异常
  17. 数据库相关依赖缺失

总结出来有四个大的排查方向:

  1. 数据库连接正常(能查询 sys_config 表);
  2. MyBatis 映射文件 / 接口正确(能操作 sys_config 表);
  3. 依赖的 Bean(如 sysConfigMapper、Redis)已正常初始化;
  4. 配置文件(如 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

 

解决方案
  1. 检查数据库连接配置打开 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 认证失败。

  1. 检查 sys_config 表是否存在

    • 执行 RuoYi 官方提供的初始化 SQL 脚本(ry_202xxxxx.sql),确保 sys_config 表及数据已导入;
    • 若手动修改过表结构(如新增 / 删除字段),需同步更新 SysConfig 实体类和 sys_configMapper.xml 中的字段映射。
  2. 检查数据库驱动版本匹配

    • 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>

场景 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

解决方案
  1. 检查 Mapper 接口是否加注解SysConfigMapper 接口必须加 @Mapper 注解,或在启动类加 @MapperScan 扫描 Mapper 包:

// 方案1:Mapper接口加注解
@Mapper
public interface SysConfigMapper { ... }

// 方案2:启动类加扫描(推荐)
@SpringBootApplication
@MapperScan("com.ruoyi.**.mapper") // 扫描所有mapper包
public class RuoYiApplication { ... }

 

  1. 检查 Mapper XML 文件路径 / 命名

    • SysConfigMapper.xml 必须放在 resources/mapper/system/ 目录下(与 RuoYi 约定一致);
    • XML 文件的 namespace 必须与 Mapper 接口全类名一致:

      <mapper namespace="com.ruoyi.system.mapper.SysConfigMapper">

    • 确认 XML 中 selectConfigList 等方法 ID 与接口方法名一致。
  2. 检查依赖是否缺失若 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)

 

解决方案
  1. 检查 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() 排查具体报错。
  2. 检查 Redis 服务是否启动RuoYi 默认将系统配置缓存到 Redis,若 Redis 未启动 / 连接失败,会导致 init() 方法报错:

    • 启动 Redis 服务(本地执行 redis-server);
    • 若无需 Redis,修改 application.yml 关闭缓存:

      ruoyi:
        cache:
          type: local # 改为本地缓存

场景 4:包扫描 / 注解配置错误

报错特征

Spring 未扫描到 sysConfigServiceImpl 或其依赖的 Bean,日志含:

No qualifying bean of type 'com.ruoyi.system.service.SysConfigService' available

解决方案
  1. 检查 Service 类注解SysConfigServiceImpl 必须加 @Service 注解,且包路径在 Spring 扫描范围内:

        @Service
        public class SysConfigServiceImpl implements SysConfigService { ... }

  1. 检查启动类扫描范围RuoYi 启动类的 @SpringBootApplication 需覆盖所有业务包(默认 com.ruoyi),若手动修改包名,需调整扫描范围:@SpringBootApplication(scanBasePackages = "com.ruoyi") // 确保包含system模块
    public class RuoYiApplication { ... }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值