最好,使用@Indexed注释或其他声明方式,可以注入(inject)系统属性,最好使用SPeL。
我尝试了以下方法,但发现 expireAfterSeconds
给出错误,因为它需要一个 int:
@Data
@Document(collection = "#{@xyzUpdates.collectionName}")
public class UpdatesFromXyz {
@Id
@Field("resourceId")
private UUID resourceId;
@Indexed(expireAfterSeconds = "#{@xyzUpdates.maxRecords}")
private LocalDate updateDate;
}
和我的属性类:
@ConfigurationProperties("xyz.updates")
@Getter
@Setter
@Component
public class XyzUpdates {
private String collectionName = "updatesFromXyz";
private int maxRecords;
}
最佳答案
从 SpringData MongoDB 2.2 开始,您可以使用 expireAfter它接受数值后跟其测量单位或 Spring 模板表达式。
关于java - Spring Data MongoDB 如何从系统属性设置索引 ttl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59760359/