java - spring 外部属性文件;文件未找到异常

标签 java xml spring javabeans

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

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)

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

最佳答案

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

关于java - spring 外部属性文件;文件未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720675/

相关文章:

java - 对话范围意外行为

java - 带有文本/html 响应的响应式(Reactive) WebClient GET 请求

php - Xml 喊话到 mysql 数据库

java - 为什么我看不到我的网站,也看不到 HTTP 状态 404?

java - AOP Spring 方面方法未应用于目标对象

java - 通过 Jena 启用 SPARQL 请求的推理?

java - 如何抛出 InvalidLockCombinationException

SQL:如何获取 XML 数据类型中的属性值?

android - 如何在 android 布局中使用多个 MathJax 公式 View

java - Spring MVC : the difference between DeferredResult and ListenableFuture?