Spring Boot reading from application.properties (2024)
In this tutorial, we will explore the how to read data from application.properties file in Spring Boot application.
We can read properties from an application properties file in the following ways.
@Value
annotationEnvironment
Object@ConfigurationProperties
annotation
Create Spring Boot Project from Spring Initializer
Project Structure
Dependencies
Add web starter dependency for web controller and lombok for auto generating getters/setter methods.
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-boot-read-properties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-read-properties</name>
<description>Spring Boot Read Property File</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@ConfigurationProperties
for reading properties from application.properties
Create Configuration java class to load the properties using
@ConfigurationProperties
annotation.
Note we have used lombok @AllArgsConstructor
, @NoArgsConstructor
, @Data
for generating constructors and for getters/setters.
package com.techgeeknext.config;
import lombok.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix="spring.datasource")
@Configuration
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Configurations {
private String username;
private String password;
private String platform;
}
Application Properties File
Add some properties so that we can use it for reading.
spring.application.name=config-service
spring.datasource.username=techgeeknext
spring.datasource.password=root
spring.datasource.platform=mysql
Rest Controller
Create the rest controller class. In this controller class will show all the different ways to
access the properties from application.properties
file.
package com.techgeeknext.controller;
import com.techgeeknext.config.Configurations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestPropertyValController {
@Value("${spring.application.name}")
String applicationName;
@Autowired
private Environment env;
@Autowired
private Configurations config;
@GetMapping("/get-values")
public Map<String, String> getAppNameVal() {
Map<String,String> values= new HashMap();
values.put("using @Value--",applicationName);
values.put("using Environment--",env.getProperty("spring.application.name"));
values.put("using @ConfigurationProperties--",config.getUsername());
values.put("using @ConfigurationProperties 2--",config.getPassword());
values.put("using @ConfigurationProperties 3--",config.getPlatform());
return values;
}
}
Take a look at our suggested posts:
Test
- Start the Spring Boot Application by running
spring-boot:run
or by running main class. GET - To test application properties values
Open POSTMAN, use the rest endpoint as http://localhost:8080/get-values and click on Send button.
Download Source Code
The full source code for this article can be found on below.
Download it here -
Spring
Boot reading from application.properties Example