Mar*_*cel 2 java spring entity hibernate spring-boot
我们有一个带有一些数据库实体类的 Spring-Boot 应用程序。
我们ddl-auto: validate用来确保连接的数据库具有正确的架构。
现在我们要添加一个可以切换以匹配不同环境的功能,NewFeatureService 用@ConditionalOnProperty("newfeature.enabled").
一切正常,直到这里。
问题是该功能需要一个数据库实体。
@Entity
@ConditionalOnProperty("newfeature.enabled") // <--- doesn't work
public class NewFeatureEnitity{...}
Run Code Online (Sandbox Code Playgroud)
@ConditionalOnProperty 显然不会起作用,但是如果设置了属性,那么告诉 Hibernate 仅根据数据库验证该实体的好方法是什么。
我们不想要的:
只是为了确保它不受监督,我想提供我的建议作为答案。
它比O.Badr提供的答案更多地使用 spring-boot 。
您可以将 spring-boot 应用程序配置为仅扫描您的核心实体,如下所示:
@SpringBootApplication
@EntityScan("my.application.core")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
所以你可以在一个包中提供你的可选实体(和功能)my.application.features(可以随意使用任何其他结构,但在先前指定的基本包之外的包)。
@ConditionalOnProperty("newfeature.enabled")
@Configuration
@EntityScan("my.application.features.thefeature")
public class MyFeatureConfiguration {
/*
* No Configuration needed for scanning of entities.
* Do here whatever else should be configured for this feature.
*/
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1496 次 |
| 最近记录: |