Loading multiple properties files in spring framework can be accomplished in following ways.

  • JavaConfig approach using @PropertySources annotation
  • XML configuration approach using context:property-placeholder

Sample Property files

env.properties:

jdbc.url=127.0.0.1
jdbc.userid=
jdbc.userid.default=admin
jdbc.port=1521

env2.properties:

jdbc.userid=codesimplify
jdbc.port=1111

1. JavaConfig approach using @PropertySources annotation


1.1 Define @PropertySources annotation

@PropertySources annotation is expected to be defined on configuration class(@Configuration annotated class). @PropertySources acts as container annotation that aggregates multiple @PropertySource annotations.

package com.codesimplify.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources({
		@PropertySource("classpath:env.properties"),
		@PropertySource("classpath:env2.properties")
})
public class AppConfig {

	
}
NOTE: If you wanted to use Property placeholder ‘$’, you are expected to define PropertySourcesPlaceholderConfigurer bean. For more information, see section 2.2 in Spring @Value annotation usage using SpringEL

1.2 Run the application


SpringPropertyLoadApp.java

package com.codesimplify;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;
import com.codesimplify.config.AppConfig;

public class SpringPropertyLoadApp {

	public static void main(String[] args) {		
			    
		  ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		  Environment env = (Environment) context.getEnvironment();
		  
		  System.out.println("DB URL:"+ env.getProperty("jdbc.url") );
		  System.out.println("Mock User from env2.properties:"+ env.getProperty("jdbc.userid") );
		  System.out.println("Overwritten Property from env2.properties:"+ env.getProperty("jdbc.port") );
	}
}

Output:

DB URL:127.0.0.1
Mock User from env2.properties: codesimplify
Overwritten Property from env2.properties:1111

If properties with same name are defined in multiple properties files, then last one loaded will be considered.

2. XML Configuration Approach using >property-placeholder


1.1 Define context:property-placeholder in XML config

To load properties using Spring XML configuration, define context:property-placeholder as under

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 
      <context:property-placeholder location="classpath:env.properties,classpath:env2.properties" />
      
      <bean name="dbConfigBean" class="com.codesimplify.config.beans.DbXMLConfigBean">
      	<property name="dbUrl" value="${jdbc.url}"></property>
      	<property name="dbUserId" value="${jdbc.userid}"></property>
      	<property name="port" value="#{T(java.lang.Integer).parseInt('${jdbc.port}')}"></property>
      </bean>
 
</beans>

1.2 Run the application

package com.codesimplify;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.codesimplify.config.beans.DbXMLConfigBean;

public class ValueAnnotationUsageXmlConfig {

	public static void main(String[] args) {		
			    
		  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		  DbXMLConfigBean dbConfigBean = (DbXMLConfigBean) context.getBean("dbConfigBean");
		   
		  System.out.println("DB URL: "+ dbConfigBean.getDbUrl());
		  System.out.println("DB UserId from env2.properties(Overwritten property): "+ dbConfigBean.getDbUserId() );
		  System.out.println("DB Port from env2.properties:"+ dbConfigBean.getPort() );
	}
}

Output:

DB URL: 127.0.0.1
DB UserId from env2.properties(Overwritten property): codesimplify
DB Port from env2.properties:1111

How to load multiple properties files in Spring

Leave a Reply

Your email address will not be published. Required fields are marked *