有多少@EntityScan注释?

时间:2022-09-11 16:15:02

I have multiple "microservices" Spring Boot apps. Each has it's own package structure to run independently just fine. Each may have it's own @EntityScan as well depending on whether or not I can use the auto-configure from a base package trick.

我有多个“微服务”Spring Boot应用程序。每个都有它自己的包结构,可以独立运行。每个人都可以拥有它自己的@EntityScan,这取决于我是否可以使用基本包技巧中的自动配置。

When I try to combine them into a single Spring Boot application, do the separate @EntityScan annotations all get honored? I am not seeing this behavior right now. It appears to only honor one @EntityScan and then I get the dreaded could not autowire a custom repository because the JPA entity for it does not exist.

当我尝试将它们组合到一个Spring Boot应用程序中时,单独的@EntityScan注释是否都得到尊重?我现在没有看到这种行为。它似乎只尊重一个@EntityScan,然后我得到了可怕的无法自动装配自定义存储库,因为它的JPA实体不存在。

Spring Boot 1.2.3-RELEASE

Spring Boot 1.2.3-RELEASE

1 个解决方案

#1


They may well all get evaluated but it looks like the bean post processor (see EntityScanRegistrar.class) will just overwrite whatever the previous value of packagesToScan was on the EntityManager bean, which is not what you want.

它们可能都得到了评估,但看起来像bean后处理器(参见EntityScanRegistrar.class)只会覆盖在EntityManager bean上的packagesToScan的先前值,这不是你想要的。

You could probably quite easily write your own bean post processor that builds a combined list from all of the @EntityScan annotations.

您可以很容易地编写自己的bean后处理器,从所有@EntityScan注释构建组合列表。

You could also relatively simply configure the EntityManager bean manually, or even more simply, use the Spring @Profile features to selectively enable and disable your @Configuration classes that have the @EntityScan annotation when running combined services.

您也可以相对简单地手动配置EntityManager bean,或者更简单地,使用Spring @Profile功能有选择地启用和禁用在运行组合服务时具有@EntityScan注释的@Configuration类。

#1


They may well all get evaluated but it looks like the bean post processor (see EntityScanRegistrar.class) will just overwrite whatever the previous value of packagesToScan was on the EntityManager bean, which is not what you want.

它们可能都得到了评估,但看起来像bean后处理器(参见EntityScanRegistrar.class)只会覆盖在EntityManager bean上的packagesToScan的先前值,这不是你想要的。

You could probably quite easily write your own bean post processor that builds a combined list from all of the @EntityScan annotations.

您可以很容易地编写自己的bean后处理器,从所有@EntityScan注释构建组合列表。

You could also relatively simply configure the EntityManager bean manually, or even more simply, use the Spring @Profile features to selectively enable and disable your @Configuration classes that have the @EntityScan annotation when running combined services.

您也可以相对简单地手动配置EntityManager bean,或者更简单地,使用Spring @Profile功能有选择地启用和禁用在运行组合服务时具有@EntityScan注释的@Configuration类。