Spring 数据 MongoDB 如何从系统设置索引 ttl 属性

Spring Data MongoDB how to set index ttl from system property

最好使用 @Indexed 注释或其他一些声明方式,是否可以注入系统 属性,最好使用 SPeL。

我尝试了以下但发现 expireAfterSeconds 给出了错误,因为它需要一个 int:

@Data
@Document(collection = "#{@xyzUpdates.collectionName}")
public class UpdatesFromXyz {

    @Id
    @Field("resourceId")
    private UUID resourceId;

    @Indexed(expireAfterSeconds = "#{@xyzUpdates.maxRecords}")
    private LocalDate updateDate;

}

和我的属性 class:

@ConfigurationProperties("xyz.updates")
@Getter
@Setter
@Component
public class XyzUpdates {

    private String collectionName = "updatesFromXyz";
    private int maxRecords;
}

因为 SpringData MongoDB 2.2 你可以使用 expireAfter 它接受数值后跟它们的度量单位或 Spring 模板表达式。