springboot(cloud) 多模块 引用公共工程的jpa实体类提示java.lang.IllegalArgumentException: Not a managed type:class .

问题描述

springboot的多模块工程,jpa实体类从公共工程引用,启动项目提示异常

java.lang.IllegalArgumentException: Not a managed type: class cn.springcloud.book.common.entity.SysUser

项目结构大致如下:

module1:cn.springcloud.book.common.entity.SysUser
module2:cn.springcloud.book.auth.repository.SysUserRepository
module3:cn.springcloud.book.auth.repository.SysUserRepository

module2和module3引用module1的@Entity

原因

SpringBoot默认包扫描机制是:从启动类所在包开始,扫描当前包及其子包下的所有文件

解决

启动类添加@EntityScan("cn.springcloud.book.common.entity")
完整代码(我的项目springcloud工程,普通springboot项目可以省略@EnableDiscoveryClient,@EnableCircuitBreaker)

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EntityScan("cn.springcloud.book.common.entity")
public class AuthServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AuthServerApplication.class, args);
    }
    
}

延伸

引用公共module的对象可以使用如下注解:
(1)扫描@Bean、@Service、@Component

@ComponentScan(basePackages = "com.demo.xxx")

(2)扫描JPA的@Repository

@EnableJpaRepositories(basePackages = "com.demo.xxx")

(3)扫描JPA的@Entity

@EntityScan("com.demo.xxx")
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值