【问题标题】:spring external properties file; file not found exception弹簧外部属性文件;找不到文件异常
【发布时间】:2014-03-28 18:35:33
【问题描述】:

我正在尝试使用以下代码从外部属性文件中读取。我很确定我的路径设置正确,但我仍然收到未找到文件的错误。有什么建议么?这是我的代码:

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>

在网络应用程序的属性文件中像这样设置我的外部文件路径。

#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)

任何帮助将不胜感激。如果我遗漏了一些您可能需要查看的代码,请告诉我。

【问题讨论】:

  • 你不能在PropertyPlaceholderConfigurer 配置中使用占位符(spring 需要PropertyPlaceholderConfigurer 来处理${xxx} vars)
  • 您在配置之前尝试使用属性

标签: java xml spring javabeans


【解决方案1】:

您尝试执行的操作不会起作用,因为 ${external.property.directory} 没有得到解决。 您可以使用环境变量来获得相同的结果,以防您需要两个属性文件(请注意,第二个中的任何属性都将覆盖第一个中的相同属性)

【讨论】:

  • 谢谢。我只是将外部属性线放在我的内部属性文件中,然后修复它。
猜你喜欢
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多