库项目中的 EntityScan 破坏了 SpringBootApplication 自动配置

cbi*_*ici 5 spring entities spring-data-jpa spring-boot

我正在尝试使用 spring 构建一个库,它将处理其他 Spring Boot 应用程序的任务管理。我的库包括服务、存储库和实体。图书馆将使用实体的父项目的数据源。我的目标是在其他 Spring 项目中使用任务管理库,仅使用 @EnableTask 注释。

为此,我准备了我的图书馆,它按我的预期工作。但是当我尝试将此库导入到 Spring Boot 应用程序时,库中的存储库和实体不可用。

我的EnableTask注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(TaskConfig.class)
public @interface EnableTask {

}
Run Code Online (Sandbox Code Playgroud)

和任务配置

@Configuration
@ComponentScan("com.cbidici.task")
public class TaskConfig {

}
Run Code Online (Sandbox Code Playgroud)

为了从注册库中创建存储库,我更改了我的配置类,如下所示。

@Configuration
@ComponentScan("com.cbidici.task")
public class TaskConfig {

    @Bean
    public TaskRepository taskRepository(EntityManager entityManager) {
        JpaRepositoryFactory jpaRepositoryFactory=new JpaRepositoryFactory(entityManager);
        return jpaRepositoryFactory.getRepository(TaskRepository.class);
    }

}
Run Code Online (Sandbox Code Playgroud)

它起作用了......

现在我把一切都搞砸了!使用此配置时,我的任务实体不是托管类型,这是一个例外。

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.cbidici.task.entity.Task
    at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:552) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:74) ~[spring-data-jpa-2.1.2.RELEASE.jar:2.1.2.RELEASE]
Run Code Online (Sandbox Code Playgroud)

为了注册实体,我尝试将 EntityScan 添加到我的 TaskConfig 类中。这搞乱了 SpringBootApplication 并且我的父项目本身不扫描实体。

我发现了这个问题,并且有一个解决方案。 https://github.com/spring-projects/spring-boot/issues/6141

但是,此解决方案包括将 @EnableJpaRepositories、@EntityScan 添加到我的父项目中,但我不想这样做。

我认为我需要找到一种在库中手动注册实体的方法,就像我对存储库所做的那样,但我做不到。