【问题标题】:Env variables in data class in Redis model annotationRedis模型注释中数据类中的环境变量
【发布时间】:2020-10-28 20:02:46
【问题描述】:

难以在模型类@Hashkey Redis 注释中指定动态环境变量。

型号:

@RedisHash("${spring.redis.namespace}:Book")
public class Book {
    @Id
    private String id;
    private String name;
}

我的 application.properties 文件:

spring.redis.namespace=local

生成的键是“${spring.redis.namespace}:Book”而不是 local:Book

谁能帮我解决这个问题?

【问题讨论】:

    标签: java spring spring-boot redis model


    【解决方案1】:

    请使用Keyspaces 进行操作。有两种方法。我用一种方法来完成你的要求。

    @Configuration
    @EnableRedisRepositories(keyspaceConfiguration = MyKeyspaceConfiguration.class)
    public class ApplicationConfig {
    
      @Value("${spring.redis.namespace}:Book")
      String myKey;
    
      //... RedisConnectionFactory and RedisTemplate Bean definitions omitted
    
      public static class MyKeyspaceConfiguration extends KeyspaceConfiguration {
    
        @Override
        protected Iterable<KeyspaceSettings> initialConfiguration() {
          return Collections.singleton(new KeyspaceSettings(Book.class, myKey));
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 2012-10-25
      • 2020-03-28
      • 2017-09-24
      • 2014-09-30
      相关资源
      最近更新 更多