private static final String CONSTANT = (p.getProperty("MILLISECONDS_OF_12_HOURS"));

@Scheduled(fixedRateString = CONSTANT)
public void clearCache() throws IOException {


    if(!rrcodeService.cachedAccessGroups.isEmpty()) {
        for (Entry<String, CachedAccessGroups> entry : rrcodeService.cachedAccessGroups.entrySet()) {
            String key = entry.getKey();
            CachedAccessGroups accessGroups = rrcodeService.cachedAccessGroups.get(key);
            if(now.getTime() - accessGroups.getCachedDate().getTime()> Integer.parseInt(p.getProperty("EVICT_CACHE"))) {
                rrcodeService.cachedAccessGroups.remove(key);
            }
        }
    }
}


在外部属性文件中设置了MILLISECONDS_OF_12_HOURS的值。我正在尝试将该值设置为FixedRateString。但是其抛出错误,提示“注释属性Scheduled.fixedRateString的值必须为常量表达式”
任何帮助将不胜感激..谢谢!

最佳答案

您可以从上下文中注入值。就像是

@Scheduled(fixedRate = "${propertykey.myRate}", initialDelay=1000)
public void clearCache() throws IOException {
   .....
}


并在外部定义属性文件(application.properties YAML)

propertykey:
    myRate: 5000


请记住,上例中的fixedRate需要一个long

09-30 15:11