0

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. How can my spring solution can access this file and read the content when starting up the application server.

How can I do this with Spring PropertyPlaceholderConfigurer or with any other mechanism in my spring hibernate application

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
Kasun
  • 561
  • 11
  • 22
  • 1
    `PropertyPlaceholderConfigurer` accepts `file://` url in `locations` –  Apr 15 '14 at 05:38
  • @RC. post an answer maybe (providing links to official docs and all that, you know)? – Luiggi Mendoza Apr 15 '14 at 05:40
  • see [this question](http://stackoverflow.com/questions/3611250/reference-spring-properties-file-using-path-relative-to-config-file?rq=1) for example –  Apr 15 '14 at 05:40
  • @LuiggiMendoza I was looking for an existing answer instead ;) –  Apr 15 '14 at 05:40

3 Answers3

0

An example:

<context:property-placeholder location="file:/E:/Workspace/123.properties" />
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • This solution does not workfor me and giving INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9446e4: defining beans [org.springframework.context.support.PropertySourcesPlaceholderConfiorg.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at Caused by: java.io.FileNotFoundException: E:\Workspace\123.properties (The system cannot find the file specified) – Kasun Apr 15 '14 at 11:11
  • Do you actually have the file in that location? – Andrei Stefan Apr 15 '14 at 11:17
0

PropertyPlaceholderConfigurer

As of Spring 3.1, PropertySourcesPlaceholderConfigurer should be used preferentially over this implementation; it is more flexible through taking advantage of the Environment and PropertySource mechanisms also made available in Spring 3.1.

Using a PropertySourcesPlaceholderConfigurer you can load properties from any path on the filesystem with:

PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

configurer.setLocations(new Resource[] {
    new FileSystemResource(fileName),
});
Community
  • 1
  • 1
Bart
  • 17,070
  • 5
  • 61
  • 80
-1

you can use get resource as stream like:

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

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

I hope it helps

Haris Mehmood
  • 854
  • 4
  • 15
  • 26