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