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.



How to Access Values From application.properties in Spring Boot?

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

Similar Reads

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....