不兼容的类型。找到:'org.springframework.beans.factory.annotation.Value',需要:'long'

incompatible types. Found: 'org.springframework.beans.factory.annotation.Value', required: 'long'

我有一个清理数据库的调度程序

@Scheduled(fixedDelay = @Value("#{new Long('${clean_up.period}')}"))
public void cleanStatInfoTable() {
    List<StateInfo> infoLis=stateInfoRepository.findAllByCreatedDateBefore(LocalDateTime.now().minusHours(1));
    stateInfoRepository.deleteInBatch(infoLis);
}

但是会产生编译错误

Incompatible types. Found: 'org.springframework.beans.factory.annotation.Value', required: 'long'

我也试过表格@Scheduled(fixedDelay = @Value("${obi.payments.state_info.clean_up.period}")) 但还是一样的问题

如何将 long 值注入 Scheduled 注释中的 fixedDelay 属性?

试试这个:

@Value("${clean_up.period}")
private Long delay;

------
@Scheduled(fixedDelay = delay)

使用fixedDelayString代替您现在拥有的。你让事情变得过于复杂。

@Scheduled(fixedDelayString = "${clean_up.period}"))