Java “无法解析占位符”的cron问题
我正在开发的STS web应用程序中使用cron,当我尝试在cron计时器上运行SpringMailSender时,遇到了字符串格式问题。我从外部属性文件中提取cron的值,出于某种原因,它似乎不喜欢它。有什么想法吗?这是我的密码Java “无法解析占位符”的cron问题,java,spring,cron,Java,Spring,Cron,我正在开发的STS web应用程序中使用cron,当我尝试在cron计时器上运行SpringMailSender时,遇到了字符串格式问题。我从外部属性文件中提取cron的值,出于某种原因,它似乎不喜欢它。有什么想法吗?这是我的密码 public class Timer { @Autowired private ApplicationContext ctx; @Autowired private SpringMailSender springMa
public class Timer {
@Autowired
private ApplicationContext ctx;
@Autowired
private SpringMailSender springMailSender;
@Scheduled(cron="${ctx.getMessage('timer.time', null, null)}")
public void timer()
{
System.out.println("timer() in Timer Class has been stepped into");
try {
springMailSender.sendMail();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Method executed on every 2nd Monday of each month. Current time is :: "+ new Date());
}
}
外部属性文件中的信息如下所示
timer.time=0 0 8 ? 1/1 MON#2 *
我得到的错误是
"java.lang.IllegalStateException: Encountered invalid @Scheduled method 'timer': Could not resolve placeholder 'ctx.getMessage('timer.time', null, null)' in string value "${ctx.getMessage('timer.time', null, null)}"
@Scheduled注释支持使用属性占位符,即${…}。要使其工作,您需要配置一个属性占位符
<context:placholder-configurer location="path/to/your/external.properties" />
这样,还可以删除对ApplicationContext的依赖关系 我想您正在寻找{…}的可能副本。为什么不简单地使用占位符配置器和${timer.time}而不是试图滥用MessageSource来获取属性;嗯,这样做很有效。它给了我一个新的错误,因为现在有了符号,它似乎试图读取{ctx.getMessage'timer.time',null,null}作为cron表达式,错误是java.lang.IllegalStateException:遇到了无效的@Scheduled method'timer':cron表达式必须由在{ctx.getMessage'timer.time',null,null}中找到的3个字段组成@戴纳姆先生。我以前从未用过。代码看起来如何?您是指Spring属性占位符配置器吗?我在Google上看不到context:placeholder configurer的任何内容。Deinum的意思是将此元素context:placeholder configurer放置在bean定义或上下文文件中,例如applicationContext.xml,计时器bean在其中定义,因为我看不到任何注释。
@Scheduled(cron="${timer.time}")