简体   繁体   English

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

[英]Access a Property file outside the classpath

I need to access property file outside the class path . 我需要在类路径之外访问属性文件。 The file path will be in windows C:\\Temp\\remote.txt or in linux /tmp/remote.txt . 文件路径将位于Windows C:\\Temp\\remote.txt或linux /tmp/remote.txt How can my spring solution can access this file and read the content when starting up the application server. 启动应用程序服务器时,我的spring解决方案如何访问此文件并读取内容。

How can I do this with Spring PropertyPlaceholderConfigurer or with any other mechanism in my spring hibernate application 如何使用Spring PropertyPlaceholderConfigurer或Spring Hibernate应用程序中的任何其他机制执行此操作

一个例子:

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

PropertyPlaceholderConfigurer PropertyPlaceholderConfigurer

As of Spring 3.1, PropertySourcesPlaceholderConfigurer should be used preferentially over this implementation; 从Spring 3.1开始,应优先使用PropertySourcesPlaceholderConfigurer ,而不要使用此实现。 it is more flexible through taking advantage of the Environment and PropertySource mechanisms also made available in Spring 3.1. 通过利用Spring 3.1中也提供的EnvironmentPropertySource机制,它更加灵活。

Using a PropertySourcesPlaceholderConfigurer you can load properties from any path on the filesystem with: 使用PropertySourcesPlaceholderConfigurer可以通过以下方式从文件系统上的任何路径加载属性:

PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

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

you can use get resource as stream like: 您可以使用get资源作为流,例如:

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

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

I hope it helps 希望对您有所帮助

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

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