记SpringBoot中 Consider defining a bean of type 'com.xxx.classname' in your configuration 错误的解决办法

一、背景

      笔者项目中。有两个子模块代码。需要分别在不同的包名中运行,假设一个包名为 cn.com.a,另一个包名为cn.com.b。由于启动类只加了@SpringBootApplication注解,所以Springboot默认是在主类所在的包名下扫描,并注册bean,而现在项目已被分解为不同的模块,各自包名不同,因此报错  Consider defining a bean of type 'com.xxx.classname' in your configuration。

二、原因描述

      项目已被分解为不同的模块,因此需要在主类上。指定独立模块要扫描的类或包。

三、解决方案

       如下代码所示,先加上@ComponentScan 注解,如果报错 JpaRepositories 和 Entity 错误,再加上第2和第3行。

1 @ComponentScan(basePackages = {"cn.com.a", "cn.com.b"})
2 @EnableJpaRepositories(basePackages = {"cn.com.a", "cn.com.b"})
3 @EntityScan(basePackages = {"cn.com.a", "cn.com.b.domain"})
4 public class CcnGuardApplication implements CommandLineRunner {
}

        

posted @ 2021-04-07 15:07  屠城校尉杜  阅读(1894)  评论(0)    收藏  举报