How to Access Values From application.properties in Spring Boot?
Last Updated :
20 May, 2024
In a Spring Boot application, application.properties is the central repository for defining configuration properties. These properties are accessible across the application to customize behavior. Accessing values from application.properties is a common requirement in Spring Boot projects.
Spring Boot automatically loads properties from application.properties files in the classpath. To access these properties in Spring components, we use annotations like @Value or the Environment object provided by Spring.
Key Terminologies:
- application.properties: This is the file where Spring Boot application configuration properties are defined, usually located in the src/main/resources directory by default.
- Properties: These key-value pairs are used for configuration in Spring Boot and are typically defined in the application.properties file.
- @Value: This annotation in Spring is used to inject values into Spring beans from properties files, environment variables, or system properties.
- Placeholders: For Spring Boot, placeholders are expressions used in resolving property values at runtime, usually specified with ${...} syntax.
- Classpath: This is the location in the Java Virtual Machine where classes are stored. the application.properties file in the classpath is automatically loaded by Spring Boot.
- Configuration Properties Binding: This provides a way to bind properties defined in application.properties to Java objects, simplifying the configuration and use of properties
Implementation to access values from application.properties in Spring Boot application
Below is the implementation steps to access values from application.properties in Spring Boot.
Step 1:
Create a new Spring Boot project using Spring Initializr and include the following dependencies:
- Spring Web
- Spring Devtools
- Lombok
Once the project is created, the file structure resembles the image below.

Step 2:
Open the application.properties file and add the following properties:
spring.application.name=spring-value-demo
# Properties of the app title
app.title=My Spring Boot App
Step 3: Create the Controller class
We will create the controller class to access values from the application.properties file.
Go to src > org.example.springvaluedemo > ExampleController and put the below code.
Java
package org.example.springvaluedemo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExampleController {
@Value("${app.title}") // Injecting the value of app.greeting from application.properties
private String greeting;
@GetMapping("/greet")
public String greet() {
return greeting; // Return the value of app.greeting
}
}
Step 4:
Open the main class(no need to changes in main class).
Go to src > org.example.springvaluedemo > SpringValueDemoApplication and put the below code.
Java
package org.example.springvaluedemo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringValueDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringValueDemoApplication.class, args);
}
}
Step 5: Run the Application
Once we run the application, then the project will run at port 8080.

API Endpoint:
http://localhost:8080/greet
Output:

This example demonstrates the how to the access values from the application.properties into the Spring Boot application.
Similar Reads
Add Build Properties to a Spring Boot Application Spring Boot framework is an open-source framework for developing web applications with high performance and the Spring Boot makes it much easier to develop Spring-based applications developed by Java programming In this article, we will learn how to add build properties to a Spring Boot Application.
4 min read
How to Convert application.properties to application.yml for Spring Boot? Spring Boot allows us to configure application properties in two formats that are application.properties and application.yml. Both serve the same purpose of configuring application settings such as database connections, server ports, and framework-specific configurations. Many developers prefer YAML
7 min read
Spring Boot - Application Properties Spring Boot is built on top of the Spring Framework and includes all its features while simplifying configuration and setup. It has become a favorite among developers because it provides a rapid, production-ready environment that allows them to focus on business logic instead of dealing with complex
3 min read
How to Create a Simple Spring Boot Application? Spring Boot is one of the most popular frameworks for building Java-based web applications. It is used because it simplifies the development process by providing default configurations and also reduces boilerplate code. In this article, we will cover the steps to create a simple Spring Boot applicat
2 min read
How to Set Context Path in Spring Boot Application? The context path is a prefix to the URL path used to identify and differentiate between different context(s). In Spring Boot, by default, the applications are accessed by context path â/â. That means we can access the application directly at http://localhost:PORT/. For example http://localhost:8080/
6 min read
Spring Boot â Managing Application Properties with Profiles In Spring Boot, managing application properties is crucial for configuring the application based on different environments (e.g., development, testing, production). Spring Boot provides a feature called Profiles to help with this. Profiles allow you to define different sets of properties for various
6 min read