Not a managed type: class com.example.commonspojo.entity,公共实体类剥离,然后引入报错的问题及解决办法

Not a managed type: class com.example.commonspojo.entity,公共实体类剥离,然后引入报错的问题及解决办法

公共实体类

我们在使用spring cloud 的时候会使用,把一些公共的实体剥离出来,减少代码冗余

在这里插入图片描述

这里就是我定义的公共实体类

我们在pom.xml 中引入

<!--        公共实体类-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>commons-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

我们现在在我们引用实体类去使用

package com.example.commonsjwt.repository;

import com.example.commonspojo.entity.CloudAdminEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;


public interface CloudAdminRepository extends JpaRepository<CloudAdminEntity,Integer> {

}

Error creating bean with name 'loginComtroller': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.example.userverification.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.commonspojo.entity
Not a managed type: class com.example.commonspojo.entity

这句话的意思就是我找不到这个实体类,我已经引入了这个实体类,但会报错,编辑器找到了,并且没有报错,但是jvm 找不到

解决办法

我们在启动类里面去

package com.example.userverification;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
//扫描引入已经实体剥离的包
@EntityScan("com.example")
public class UserVerificationApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserVerificationApplication.class, args);
    }

}

com.example 就是我们前面引入的包groupId 添加这个 用于扫描JPA实体类 @Entity,

<!--        公共实体类-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>commons-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

再次运行,正常运行

参考文档:https://blog.csdn.net/heyewu4107/article/details/78942393

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值