2014-03-28 95 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