2014-03-28 92 views
1

我正嘗試使用下面的代碼從外部屬性文件讀取。我很確定我有正確的路徑設置,但我仍然沒有找到文件錯誤。有什麼建議麼?這裏是我的代碼:彈簧外部屬性文件;文件未找到異常

public class Timer { 

    @Autowired 
     private ApplicationContext ctx; 

    @Autowired 
     private SpringMailSender springMailSender; 

    @Scheduled(cron="${timer.time}") //this is the line that is having trouble 
    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()); 
    } 

} 

這裏是我如何我的配置文件設置爲它...

<!-- Property Placeholder --> 
     <bean 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      <property name="locations"> 
       <list> 
        <value>classpath:properties/system.properties</value> 
        <value>file:${external.property.directory}propfilename</value> 
       </list> 
      </property> 
     </bean> 

<!-- messageSource --> 
    <bean id="messageSource" 
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basenames"> 
      <list> 
       <value>file:${external.property.directory}propfilename</value> 
      </list> 
     </property> 
    </bean> 

隨着我的外部文件路徑在屬性中設置這樣的文件中的web應用程序。

#directory on the server where property files will be stored 
external.property.directory=C\:\\propfoldername\\ 

而我得到的錯誤是這樣的:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ${external.property.directory}propfilename (The system cannot find the file specified) 

任何幫助,將不勝感激。如果我遺漏了一些可能需要查看的代碼,請告訴我。

+0

您不能在佔位符中使用'PropertyPlaceholderConfigurer' config(spring需要'PropertyPlaceholderConfigurer'來處理'$ {xxx}'vars) – 2014-03-28 18:37:14

+0

你正在試圖使用property之前配置它 –

回答

1

你試圖做的事不會工作,因爲$ {external.property.directory}沒有得到解決。 但是,如果您需要兩個屬性文件,則可以使用環境變量來實現相同的結果(請注意,第二個屬性中的任何屬性都將覆蓋第一個屬性中的相同屬性)

+0

謝謝。我只是將外部屬性行放在我的內部屬性文件中,而不是修復它。 – Stevo