Spring Read Property file

If we have some values(data) in a file and want to read them using bean's getter methods then use property files. The property file is used to contain the property values of the Spring application.

We can use it to hold the field values of a bean. It uses .properties extension to create a file as a property file.

How to Load a property file into Spring Application

To load the property file, Spring provides @PropertySource annotation and <context:property-placeholder> tag. If you are working with XML then you can use <content:property-placeholder> tag otherwise use annotation to load the property file. For example,

XML Tag Example

Use this tag into applicationContext.xml to load the property file.

content:property-placeholder location="classpath:filename.properties" />

Annotation Example

Use this annotation with the configuration class to load the property file.


How to Read Properties from the Property file

To read properties use @Value() annotation or <property/> tag. Spring allows using both XML and non-XML configurations. Let's see the syntax

XML Tag Example

Use this tag into applicationContext.xml to read the property.

<property name="propertyName" value="${propertyValue}"> </property>

Annotation Example

Use this annotation with the class field and create a getter method as well.


Example To Read property file in Java

Let's create an example to read the properties of a property file. This project has the following several java files.

// BankApp.java

This file contains the code to create an IOC container for our application. The AnnotationConfigApplicationContext class is used to create an object for application context.

Here, we used getters to read the properties of the Manager bean.

package com.studytonight.community;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BankApp {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		Manager employee = context.getBean(Manager.class);
		System.out.println("Email: " + employee.getEmail());
		System.out.println("Id: " + employee.getId());

// AppConfig.java

This is a configuration file in Java which is an alternate of the applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @PropertySource annotation is used to indicate the location of the property file in our spring project.

package com.studytonight.community;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

public class AppConfig {
	public Manager manager() {
		return new Manager();

// Manager.java

This is a component class that is marked using the @Component annotation and implements the Employee interface. In this class, we are using @Value annotation to map fields to the property file. See, We created getters to read the properties from the property file. See the example below.

package com.studytonight.community;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

public class Manager implements Employee{
	private int id;
	private String email;

	public void work() {
		System.out.println("Manage the branch office");

	public int getId() {
		return id;
	public String getEmail() {
		return email;

// manager.properties

This is our property file that contains two properties with values.


// Employee.java

This is an interface Employee that contains a Work() abstract method. Each class that implements this interface will have to override this method.

package com.studytonight.community;
public interface Employee {
	void work();

// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->


Run the Application

After successfully completing the project and adding the dependencies run the application and you will get the output as below.

Manage the branch office