Springboot项目启动报错UnsatisfiedDependency、NoSuchBeanDefinitionException

今天做项目的过程中,启动项目突然出现:

控制台报错

Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating 
bean with name 'otOvertimeController': Unsatisfied dependency expressed through 
field 'otOvertimeService'; nested exception is org.springframework.beans.factory
.UnsatisfiedDependencyException: Error creating bean with name 'otOvertimeServiceImpl':
Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.
springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'com.bravovax.otservice.mapper.OtOvertimeMapper' available: expected at least 1 bean which 
qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.
annotation.Autowired(required=true)}****

上下文初始化期间遇到异常-正在取消刷新尝试:org.springframework.beans.factory。UnsatisfiedDependencyException:
创建名为“otOvertimeController”的bean时出错:通过字段“otOvertimeService”表示的不满意依赖关系;
嵌套异常是org.springframework.beans.factory。UnsatisfiedDependencyException:创建名为“otOvertimeServiceImpl”
的bean时出错:通过字段“baseMapper”表示的不满意依赖项;嵌套异常是org.springframework.beans.factory。
NoSuchBeanDefinitionException:没有类型为“com.bravovax.otservice.mapper”的合格bean。OtOvertimeMapper“可用:
应至少有1个符合autowire候选条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

 

UnsatisfiedDependencyException:这个翻译后的意思是“不满足依赖异常”,可以看到前面“org.springframework.beans.factory”的这个信息,这个异常属于Spring的bean.factory的,就是说属于Spring的IOC容器里发生了这个异常,大家都知道IOC是管理对象的,这个异常的发生就说明你有类(Bean)没有放入到IOC中,那么它就无法创建所需要的对象,导致依赖创建失败。

NoSuchBeanDefinitionException:这个异常一般是有bean没有加入到容器中,从提示中也可以很清楚的看出是xxxDao没有加入到容器中。

最后发现是因为使用代码生成器没有自动创建mybatisplus的配置类

我的代码生成器:

package com.bravovax.eduservice;
​
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
​
public class CodeGeneratorOv {
    @Test
    public void main1() {
​
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();
​
        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        gc.setOutputDir("E:\\Code\\Project\\bravo-dashboard\\service\\service_ot" + "/src/main/java");
        gc.setAuthor("bravovax");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        /*
         * mp生成service层代码,默认接口名称第一个字母有 I
         * UcenterService
         * */
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(false);//开启Swagger2模式
​
        mpg.setGlobalConfig(gc);
​
        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/kaoqin?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
​
        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("otservice"); //模块名
        pc.setParent("com.bravovax");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);
​
        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("ot_overtime");
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
​
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
​
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
​
        mpg.setStrategy(strategy);
​
        // 6、执行
        mpg.execute();
    }
}

缺少的组件

package com.bravovax.otservice.config;
​
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
@MapperScan("com.bravovax.otservice.Mapper")
@Configuration
public class EduConfig {
    /**
     * 逻辑删除插件
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
​
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

缺少@MapperScan这个重要注解就是报错的原因

遇到同样错误可以进行以下检查:

  • 检查controller层service层mapper层注解是否添加?

  • 各组件是否完整(实现类、配置类和接口)

  • 检查xml配置文件和Mapper层的对应是否正确

  • 检查包名称、路径是否错误(大小写、是否有重复名称等)

posted @ 2022-10-26 23:59  Fancy[love]  阅读(3587)  评论(0编辑  收藏  举报