【springboot 无法注入Bean _springboot 】 | IT修真院·坑乎
问题已收录 springboot 无法注入Bean
我也踩过这个坑( 1 )
已统计您的踩坑,无需重复点击
回答(1)
springboot
详细描述
错误截图
源码
编辑于2024-11-24
  • [武汉|结业弟子]JAVA-方广辉
    0

    SpringBoot启动失败,Bean配置失败


    原因:在mybatis-spring-boot-autoconfigure的jar包中有一个类 MybatisAutoConfiguration,在这个类中的registerBeanDefinitions方法告诉了我们


    @Override

        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

     

          logger.debug("Searching for mappers annotated with @Mapper");

     

          ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

     

          try {

            if (this.resourceLoader != null) {

              scanner.setResourceLoader(this.resourceLoader);

            }

     

            List<String> packages = AutoConfigurationPackages.get(this.beanFactory);

            if (logger.isDebugEnabled()) {

              for (String pkg : packages) {

                logger.debug("Using auto-configuration base package '{}'", pkg);

              }

            }

     

            scanner.setAnnotationClass(Mapper.class);

            scanner.registerFilters();

            scanner.doScan(StringUtils.toStringArray(packages));

          } catch (IllegalStateException ex) {

            logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);

          }

        }

    解决方案


    1、mapper(Dao层)


    添加 @Mapper


    2、在启动类


    @MapperScan(value = "com.xxxxx.dao")




    SpringBootApplication
    @MapperScan(value = "com.ptteng.academy.Mapper")
    public class HomeWebApplication {
    public static void main(String[] args) {
    SpringApplication.run(HomeWebApplication.class, args);
       }
    }


    编辑于2018-10-13