How to read properties file in Spring


Spring framework gives us two annotation @PropertySource and @Value which make the reading properties file values super easy. @PropertySource annotation is used to define the properties file location and @Value annotation is used to inject the properties file values into the bean.

In this tutorial, we will show you how to read properties file in Spring. The properties file is more convenient to define the configuration of application like database configuration, master configuration, etc. Changes are reflected without restarting the application.

Let’s see the below user.properties file, which contains the user information.

user.properties
# user information

name= Sandeep Sharma
age= 26
jobTitle= Data Scientist
company= Google
location= Silicon Valley, California

1. @Value Annotation

Here is the bean class where we inject properties file values. @Value annotation injected the value one by one and best for small properties file.

UserProperties.java
package org.websparrow;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:user.properties")
public class UserProperties {

	@Value("${name}")
	private String name;
	@Value("${age}")
	private int age;
	@Value("${jobTitle}")
	private String jobTitle;
	@Value("${company}")
	private String company;
	@Value("${location}")
	private String location;

	// Generate Getters and Setters...

	@Override
	public String toString() {
		return "UserProperties [name=" + name + ", age=" + age + ", jobTitle=" + jobTitle + ", company=" + company
				+ ", location=" + location + "]";
	}
}

2. @ConfigurationProperties Annotation

@ConfigurationProperties is best for big properties file, you don’t have to define the values one by one. Below class is equivalent to the above class.

UserProperties2.java
package org.websparrow;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:user.properties")
@ConfigurationProperties
public class UserProperties2 {

	private String name;
	private int age;
	private String jobTitle;
	private String company;
	private String location;

	// Generate Getters and Setters...

	@Override
	public String toString() {
		return "UserProperties [name=" + name + ", age=" + age + ", jobTitle=" + jobTitle + ", company=" + company
				+ ", location=" + location + "]";
	}
}

WARNING: When using @ConfigurationProperties it is recommended to add ‘spring-boot-configuration-processor’ to your classpath to generate configuration metadata.

If you see the above warning message, add the following dependency to your pom.xml file.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

3. Reading the values

Here is my simple controller class where I call toString() method which returns the user data stored in the properties file.

MyController.java
package org.websparrow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

	@Autowired
	private UserProperties userProperties;

	// @Autowired
	// private UserProperties2 userProperties2;

	@GetMapping("/user")
	public String getProperyFile() {

		return userProperties.toString();

		// return userProperties2.toString();
	}
}

Output: Your controller will response you the below result.

UserProperties [name=Sandeep Sharma, age=26, jobTitle=Data Scientist, company=Google, location=Silicon Valley, California]

To get the only one or two value or want to prepare properties file data according to your need, use the getter methods.

return "Hi, My name is " + userProperties.getName() + " and I work with " + userProperties.getCompany();

4. Spring Recommendation

Spring recommends to use Environment to get the property values.

MyController.java
package org.websparrow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

	@Autowired
	private Environment environment;

	@GetMapping("/user")
	public String getProperyFile() {

		String name = environment.getProperty("name");
		String company = environment.getProperty("company");
	
		return "Hi, My name is " + name + " and I work with " + company;
	}
}

References

  1. Spring PropertySource Annotation
  2. Spring Value Annotation
  3. Spring ConfigurationProperties Annotation

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.