cron问题“无法解决占位符”

时间:2014-03-28 14:49:12

标签: java spring cron

我正在使用我正在开发的STS Web应用程序中的cron,并且在尝试在cron计时器上运行SpringMailSender时遇到字符串格式问题。我从外部属性文件中提取cron的值,并且出于某种原因它似乎并不喜欢它。有任何想法吗?这是我的代码......

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)}"

1 个答案:

答案 0 :(得分:0)

@Scheduled注释支持使用属性占位符(即${...})。为此,您需要配置属性占位符。

<context:placholder-configurer location="path/to/your/external.properties" />

然后在@Scheduled注释中,您可以简单地引用文件中的属性。

@Scheduled(cron="${timer.time}")

这样你也可以删除对ApplicationContext的依赖。