Load Properties File Spring Config

Load Properties File Spring Config

If you are using Spring framework in your application and want to load complete properties file through class constructor through XML file configuration please use below code.

Note: Your properties file should be in classpath

<util:properties id="javahonkproperties" location="DEV.properties" />

<bean id="loadPropertiesFile" class="com.javahonk.LoadPropertiesFile">
     <constructor-arg ref="javahonkproperties"/>		
</bean>
  • To better understand please have a look below maven project:

Load Properties File Spring Config

  • As you see above we have DEV.properties file as below:
Javahonk.URL=http://techiworks.com
JavaHonk.name=JavaHonk
  • LoadPropertiesFile.java:
package com.javahonk;

import java.util.Properties;

public class LoadPropertiesFile {
	
	private Properties properties;

	public LoadPropertiesFile(Properties properties) {
		super();
		this.properties = properties;
	}

	public Properties getProperties() {
		return properties;
	}	

}
  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javahonk</groupId>
  <artifactId>LoadProperitesFileXMLConfig</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <properties>
		<org.springframework.version>4.1.5.RELEASE</org.springframework.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

	</dependencies>
</project>
  • spring-context.xml:
<?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"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
		
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">	
		<property name="locations">
			<list>
				<value>DEV.properties</value>
				<value>classpath:javahonk/Javahonk.properties</value>
				<value>classpath:javahonk/${environment.target}.properties</value> 
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
		<property name="ignoreResourceNotFound" value="true"/>
	</bean>

	<util:properties id="javahonkproperties" location="DEV.properties" />

	<bean id="loadPropertiesFile" class="com.javahonk.LoadPropertiesFile">
		<constructor-arg ref="javahonkproperties"/>		
	</bean>

</beans>

Test java class to load Spring context and read properties from file:

  • LoadApplication.java:
package com.javahonk;

import java.util.Properties;

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

public class LoadApplication {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
		
		Properties properties = context.getBean(LoadPropertiesFile.class).getProperties();
		String javahonkURL = properties.get("Javahonk.URL").toString();
		String name = properties.get("JavaHonk.name").toString();
		System.out.println("JavaHonk URL: "+javahonkURL);
		System.out.println("JavaHonk name: "+name);
		
		((AbstractApplicationContext)context).close();
		
	}

}
  • Output:

Load Properties File Spring Config

  • For more information please visit Spring documentation here

download Download Project:  LoadProperitesFileXMLConfig

Leave a Reply

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