Read Properties from file

Add following config to applicationContext.xml:

<context:property-placeholder location="classpath*:*.properties" />

Reference using ${} notation:


Or, Inject directly into bean:


Note 1: Having more than placeholder in the same application context will lead to Runtime error unless ignoreUnresolvablePlaceholders is set to true. (There is also an optional ignoreResourceNotFound for resources.) When using multiple PPCs, use the order attribute, lower number means higher priority, 0.

Note 2: <context:property-placeholder> is a shortcut for :

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer #before spring 3.1 #spring 3.1+

Note 3: PPCs are BeanPostProcessors, so they must all be created, before any are invoked. One PPC can’t use properties that only defined inside another PPC.

Note 4: From Spring docs: The classpath*: prefix

[…] location string may use the special classpath*: prefix: […]

This special prefix specifies that all classpath resources that match the given name must be obtained […], and then merged to form the final application context definition.

Also from StackOverflow:

The classpath*:conf/appContext.xml simply means that all appContext.xml files under conf folders in all your jars on the classpath will be picked up and joined into one big application context.

In contrast, classpath:conf/appContext.xml will load only one such file… the first one found on your classpath

Java Config
Here is the Java configuration to create the same thing:

    static @Bean public PropertySourcesPlaceholderConfigurer myPropertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
        Resource[] resourceLocations = new Resource[] {
                new ClassPathResource(""),
                new ClassPathResource(""),
        return p;


Or the much more concise version using @PropertySource:

public class AppConfig {
 Environment env;

 public TestBean testBean() {
     TestBean testBean = new TestBean();
     return testBean;
Posted in Spring

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: