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