Read Properties from file

Add following config to applicationContext.xml:

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

Reference using ${} notation:

${webapp.url}

Or, Inject directly into bean:

@Value(${webapp.url:optionalDefaultValue})

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
org.springframework.context.support.PropertySourcesPlaceholderConfigurer #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:

4.7.2.2 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("my.custom.properties"),
                new ClassPathResource("my.other.properties"),
        };
        p.setLocations(resourceLocations);
        return p;
    }

source: http://forum.spring.io/forum/spring-projects/container/101405-propertyplaceholder-in-java-config-howto

Or the much more concise version using @PropertySource:

@Configuration
@PropertySource(value="classpath*:app.properties")
public class AppConfig {
 @Autowired
 Environment env;

 @Bean
 public TestBean testBean() {
     TestBean testBean = new TestBean();
     testBean.setName(env.getProperty("testbean.name"));
     return testBean;
 }
}
Advertisements
Posted in Spring

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: