How to read properties file in Spring.
Jan 23
Java, Programming, Spring 17 Comments
If you need to read properties file in your Spring application all you need is to configure a PropertyPlaceholderConfigurer bean in your application context.
Following example shows how to read property values from a properties file named config.properties. This file needs to be in your classpath so Spring can find it.
Let’s begin by creating a simple Java class that will use the properties values from the file.
package com.zparacha.spring.examples.config;
public class CreateUser {
//Spring will populate these fields through Dependency Injection.
private String name;
private String email;
private String URL;
public CreateUser(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getURL() {
return URL;
}
public void setURL(String url) {
URL = url;
}
public void displayUser() {
System.out.println("Name=" + this.name);
System.out.println("Email="+ this.email);
System.out.println("URL=" + this.URL);
}
}
Now create the application context. I saved the file as user.xml
Notice the placeholderConfig bean. This is where we are instructing Spring container to load the values from config.properties file. You can name your will whatever you like.
Here is my config.properties file.
name=John Doe URL=http://www.zparacha.com email=[email protected]
That is all there is to read the properties file.
Following is a simple test client.
package com.zparacha.spring.examples.config;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserClient {
public static void main(String[] args) throws Exception {
String[] configFiles = new String[] { "user.xml" };
BeanFactory factory =
new ClassPathXmlApplicationContext (configFiles);
CreateUser createUser =
(CreateUser) factory.getBean("createUser");
createUser.displayUser();
}
}
If you execute this class it will print
Name=John Doe Email=[email protected] URL=http://www.zparacha.com
This approach to use PropertyPlaceholderConfigurer to read properties file will not work if you are using XmlBeanFactory. To read properties file with XmlBeanFactory you will have to retrieve the bean from context and invoke it on your factory.
For example:
package com.zparacha.spring.examples.config;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserClient2 {
public static void main(String[] args) throws Exception {
XmlBeanFactory factory =
new XmlBeanFactory(new ClassPathResource("hello.xml"));
PropertyPlaceholderConfigurer configurer = (PropertyPlaceholderConfigurer)factory.getBean("placeholderConfig");
configurer.postProcessBeanFactory(factory);
CreateUser createUser =
(CreateUser) factory.getBean("createUser");
createUser.displayUser();
}
}
Unless you have your reasons to use XmlBeanFactory, I would recommend that you use ClassPathXmlApplicationContext as your factory.
Enjoy.






Mar 14, 2009 @ 06:46:25
I have to say, I could not agree with you in 100%, but it’s just my opinion, which could be very wrong.
p.s. You have a very good template . Where have you got it from?
Mar 24, 2010 @ 23:12:03
Hi, Now that 3.0 has come, there are several other ways to access the properties from property files. This is not the best procedure however
Apr 19, 2010 @ 04:25:16
What will be the process if I had a properties file as
# devel,prod
env=local
# datasource properties for local
local.datasource.url=”mysql:local//connect……”
local.datasource.username=polo
local.datasource.pwd=polo
# datasource properties for prod
prod.datasource.url=”mysql:prod//connect……”
prod.datasource.username=polo
prod.datasource.pwd=polo
all in the same file.
How do i read that file ?
Remember first I have to read the env then according to the env i have to pick up the properties of the datasource ?
Jun 03, 2010 @ 11:29:06
Is there a way to have the property file specified dynamically? For instance, if I have multiple property files and I want to choose 1 at compile or run time, how would I do that?
Aug 20, 2010 @ 10:21:01
Make sure you capitalize the “L” above in schemalocation to schemaLocation or else you’ll get an error saying “org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’.”
Jun 15, 2011 @ 03:21:51
I found the article beneficial.
But Is it possible to read the properties from the disk e.g If I want to load the properties from C:\projects
I tried using
But it is not working..
Any help will be appreciated.
Jun 15, 2011 @ 03:32:50
Sorry the code I tried was
file:///c:/datasource.propertiesBut still I am not able to read from the external file
Jun 15, 2011 @ 03:36:05
Sorry guys the above is working..I had messed some other thing that was causing this not working.
Jul 22, 2011 @ 08:23:48
Hello,
Thank you so much for this articale, i really appritiate.
It helped me a lot.
Thanks
Aug 08, 2011 @ 01:59:00
I don’t know , but in my case i used because with classpath it said that it didnt see it.. maybe it helps someone
Aug 08, 2011 @ 01:59:44
I don’t know , but in my case i used file: because with classpath: it said that it didnt see it.. maybe it helps someone
Aug 08, 2011 @ 02:11:59
Hi all)))
document.bgColor = “RED”;
alert(“XAAAAAAAAAAAAAAAAAAAA”);
Jan 23, 2012 @ 05:54:15
Hello, good one but i have a query, if i want to add this .properties file outside src folder and within the project, i added config folder next to src and i kept my Information.properties file inside config.i tried with the entire class path as value=”classpath:config/Information.properties”, and also tried with
value=”classpath:/home/dev06/filesystem/config/Information.properties”
but i am getting a nested exception Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [resource/Information.properties] cannot be opened because it does not exist
any help will be appreciated
May 23, 2012 @ 05:30:39
Merci, c’est super !!
May 30, 2012 @ 10:27:56
Thank you !
Jan 07, 2013 @ 21:53:03
What if the CreateUser class has a property that is a list, like:
List friendsNames?
Then how to configure it in the property file? Separate by comma?
usedNames = John,Joe,Martin
Or Spring doesn’t support this at all and we have to parse the string ourselves in the CreateUser ?
May 03, 2013 @ 10:11:34
Hi, I think your web site might be having internet browser compatibility issues.
When I take a look at your site in Safari, it
looks fine however when opening in I.E., it has some overlapping issues.
I simply wanted to provide you with a quick heads up!
Apart from that, great blog!