java - 如何在 Spring 中从文件读取参数到 Schedule 注释?
问题描述
我在 c:\Temp 文件夹中创建文件 init.txt。
login=rtyhjmdsf
password=cxzxdrfks
fixrate=6000
如何从我的文件中读取参数 'fixrate' 到 Spring 中的 Schedule 注释?如何在 SpEL 中将字符串转换为 Long?
这种收缩不起作用(
@Component
@PropertySource("file:c:\temp\init.txt")
class CronSchedule {
@Scheduled(fixedRate = "#{Long(scheduler[fixrate])}" as Long)
fun publicImage() {
println("I'm starting.")
}
}
解决方案
无需将其转换为 long,而是可以使用 fixedRateString。
@Scheduled(fixedRateString = "${fixrate}")
下面的代码在Java中工作
@Component
@PropertySource("file:/tmp/init.txt")
class CronSchedule {
private static final Logger log = LoggerFactory.getLogger(CronSchedule.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRateString = "${fixrate}")
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
推荐阅读
- haskell - 获取用户输入并添加到列表中
- ffmpeg - 使用 ffprobe 确定 wtv -> mp4 转换的 crf
- flutter - 带附件的颤振表单(文件上传)
- r - 删除完全不适用的行
- javascript - 动态添加来自不同 json 响应的值 - JS
- android - 在单元测试期间调用初始化 JSModule
- kubernetes - Kubernetes:使用容器作为代理
- python - Python:有没有办法使用字段名称访问 HL7 消息
- javascript - DFP 广告未在某些网页上展示。谷歌控制台刚刚加载
- git - 在功能分支合并之前回滚 git 主分支,做一些工作并再次合并功能分支