【问题标题】:Conditionally excluding spring data jpa repositories有条件地排除 spring data jpa 存储库
【发布时间】:2013-07-27 18:11:21
【问题描述】:

我们使用spring + spring data jpa repositories + maven 作为构建环境。我们将项目战部署在多个云环境中。

对于特定的云环境,我们从数据库中检索登录凭据。 例如。对于AWS,我们可能有AWSCredentials 域对象和AWSCredentialsRepository 对于Azure,我们可能有AzureCredentials 域对象和AzureCredentialsRepository

spring data jpa 中有没有一种方法可以使用构建脚本中的某些参数有条件地加载存储库?

例如:我想部署到 Azure 云。所以我只需要AzureCredentials 域和AzureCredentialsRepository 而不是AWS

spring 中是否有一种优雅的方法可以做到这一点?

【问题讨论】:

    标签: spring jpa configuration repository spring-data-jpa


    【解决方案1】:

    遗憾的是,我认为这适用于 java-config。 (我必须用这个巫术替换 applicationContext.xml 才能让“可选的 jpa 存储库”工作。

    我无法找到 applicationContext.xml .. xml IoC 对此问题的答案。

    就我而言,我希望本地开发人员(spring-profile=default)使用 H2 数据库来存储一些信息,但是“真实代码”....非默认 spring 配置文件...会遇到 redis-缓存。

    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    
    @EnableJpaRepositories(basePackageClasses =
            {com.mystuff1.MyJpaRepository1.class, com.mystuff2.MyJpaRepository2.class})
    @Configuration
    /* jpa:repositories (di.xml) does not support being wrapped in a spring-profile-attributed-bean.  this class is a workaround */
    /* we only want InternalFhirBundleProviderWrapperJpaRepository "scanned in" as a JpaRepository .. when the spring-profile of "default" is specified.  */
    @Profile("default")
    public class JpaReposInASpecificSpringProfileVoodooWhereWaldoJavaConfig {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 2016-03-26
      • 2016-12-26
      • 1970-01-01
      • 2016-05-21
      • 2018-10-06
      相关资源
      最近更新 更多