Spring PropertySourcesPlaceholderConfigurer Example -2


This article is in continuation to Spring PropertySourcesPlaceholderConfigurer Example -1 where I showed you how to make use of Spring PropertySourcesPlaceholderConfigurer to configure properties based on environment. My scenario is I want to have different URLs for authorizations based on my environment.

I have the below environments as of now and for each environment I want to have different authorization URLs.

  • test
  • prod

In my previous article, I showed you the simplest approach.
In this article, we will explore two more ways. First method is to have different URL keys based on environment and the second method is to have one URL key but different property files for each environment.

    • Add URLs for each environment in a properties file. We will have different property key for each URL.

Below is auth.properties with the URL key/values:


Now we will modify the context file’s PropertySourcesPlaceholderConfigurer bean, to add location property to load properties from auth.properties:


    <bean id="springdemoAuthorization" class="springdemo.TestAuthorizationBean">

        <property name="url" value="${url_${env}_env}"/>


    <bean id="propConfig" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">

        <property name="location" value="springdemo/auth.properties"/>



Notice the URL property, the key follows a pattern url_${env}_env. Based on the environment, we will inject the ${env} and we will get different URL key names.

In the test case below, we set the environment, create the context and check whether the authorization bean has got the correct URL.

    public void testUrlKeys() {
        String testUrl = "http://localhost:8280/springdemo/testAuthorize";
        String prodUrl = "http://localhost:8280/springdemo/prodAuthorize";
        System.setProperty("env", "test");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/springdemo/testPropertConfig.xml");
        assertEquals(testUrl, ((TestAuthorizationBean) context.getBean("springdemoAuthorization")).getUrl());

        System.setProperty("env", "prod");
        assertEquals(prodUrl, ((TestAuthorizationBean) context.getBean("springdemoAuthorization")).getUrl());
  •  Instead of having different URLs for each environment, we can use the same property key but different properties file.

For example:





There is a slight change in context file.

The location path now has a placeholder ${env}. Based on the ${env} value, PropertySourcesPlaceholderConfigurer will load either testAuth.properties or prodAuth.properties. Out test case remains the same, we will just re-run the test with the modified context file and make sure the test passes. Our modified context file would be:

    <bean id="springdemoAuthorization" class="springdemo.TestAuthorizationBean">

        <property name="url" value="${url_env}"/>


    <bean id="propConfig" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">

        <property name="location" value="springdemo/${env}auth.properties"/>



Leave A Reply