【问题标题】:Is the model or a service responsible for defining the default TTL to a new instance of the model?模型或服务是否负责为模型的新实例定义默认 TTL?
【发布时间】:2018-02-07 11:31:40
【问题描述】:

事情是这样的,我想在我的 Dynamo 表中添加 ttl,它有一个默认的 Duration,但我希望它可以通过属性进行配置,以便每个 Spring Profile 都有自己的配置。

public Long getTtl() {
    return ttl;
}

public void setTtl(Long ttl) {
    this.ttl = ttl;
}

为了正确填充它,我需要做这样的事情。

entity.setTtl(Instant.now().plus(Duration.from(defaultTTL)).getEpochSecond());

我会从@Value 或其他东西加载这个默认TTL。

但我的问题是放在哪里。我的第一直觉是在默认构造函数中设置,但我不喜欢在其中加载 Spring 值。

遵循“一起改变的代码,属于一起”。

我错了吗?

【问题讨论】:

    标签: java spring oop


    【解决方案1】:

    我在服务的常量中为 CRUD 操作定义了 DEFAULT_TTL,并在模型的构造函数中设置了 TTL。

    看起来不错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 2022-08-04
      • 2019-03-22
      • 1970-01-01
      • 2015-01-12
      相关资源
      最近更新 更多