繁体   English   中英

在类路径之外访问属性文件

[英]Access a Property file outside the classpath

我需要在类路径之外访问属性文件。 文件路径将位于Windows C:\\Temp\\remote.txt或linux /tmp/remote.txt 启动应用程序服务器时,我的spring解决方案如何访问此文件并读取内容。

如何使用Spring PropertyPlaceholderConfigurer或Spring Hibernate应用程序中的任何其他机制执行此操作

一个例子:

<context:property-placeholder location="file:/E:/Workspace/123.properties" />

PropertyPlaceholderConfigurer

从Spring 3.1开始,应优先使用PropertySourcesPlaceholderConfigurer ,而不要使用此实现。 通过利用Spring 3.1中也提供的EnvironmentPropertySource机制,它更加灵活。

使用PropertySourcesPlaceholderConfigurer可以通过以下方式从文件系统上的任何路径加载属性:

PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

configurer.setLocations(new Resource[] {
    new FileSystemResource(fileName),
});

您可以使用get资源作为流,例如:

String filePath = "C:/Temp/remote.txt";

BufferedReader input = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(filePath)));

希望对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM