2014-03-28 26 views
0

我正在與一個STS Web應用程序中的cron工作,我正在開發和我試圖在cron計時器上運行SpringMailSender時出現字符串格式問題。我從外部屬性文件中提取cron的值,出於某種原因,它似乎並不喜歡它。有任何想法嗎?這裏是我的代碼...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)}" 
+0

可能重複的[無法解決Spring屬性佔位符](http://stackoverflow.com/questions/4779572/could-not-resolve-spring-property-placeholder) – ruhungry

+0

我認爲你正在尋找'#{ ...}'。 –

+0

爲什麼你不使用'PlaceHolderConfigurer'和'$ {timer.time}'來代替嘗試濫用'MessageSource'來獲取屬性。 –

回答

0

@Scheduled註釋支持使用propert佔位符(即${...})。爲了這個工作,你必須配置一個屬性佔位符。

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

然後在您的@Scheduled註釋中,您可以簡單地引用文件中的屬性。

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

這樣,您還可以刪除對ApplicationContext的依賴關係。

+0

你的意思是Spring Property Placeholder Configurer?我沒有看到上下文的任何信息:placeholder-configurer在Google上。 – Stevo

+0

Deinum的意思是將這個元素上下文:placeholder-configurer放置在您的Timer bean定義的bean定義或上下文文件(類似於applicationContext.xml)中(因爲我沒有看到任何註釋)。 – Prasad

相關問題