Properties with Spring and Spring Boot
Last Updated :
23 Apr, 2024
Java-based applications using the Spring framework and its evolution into the Spring Boot and the properties play a crucial role in configuring the various aspects of the application. Properties can allow the developers to externalize the configuration settings from the code. Understanding how to work with properties in the Spring and Spring Boot is essential for building flexible and maintainable Spring applications.
Prerequisites
- Good Understanding of Java programming
- Basic understanding of Spring framework and Spring Boot basics
- JDK and Spring environment setup into your local system
Understanding of the Properties of Spring and Spring Boot
Properties in the Spring and Spring Boot are typically stored in the configuration files such as the `application.properties` or `application.yml`. These files reside in the src/main/resources directory of the Spring Boot application.
Properties can be used to configure the various aspects of the application such as the database connection details, logging settings, server port, etc. It can be injected into the Spring Components using the @Value annotation or access programmatically using the Environment abstraction of the Spring application.
Key Terminologies
- Properties: It can be key-value used for the configuring the various aspects of the application. These properties can typically stored in the configuration files of the Spring application.
- Configuration Files: It can be files where the properties are stored. In the Spring Boot, Commonly used for the configuration files are 'application.properties' or 'application.yml'. These files are reside in the src/main/resources directory.
- @Value annotation: This annotation can be used for the injecting property values into the Spring beans. It can be used to the directly inject the values from the properties files into the beans fields or the constructors parameters.
Project to Implement the Properties with Spring and Spring Boot
We can demonstrate how to the Implement the Properties with Spring and Spring Boot in the Spring application.
Step 1: Create the spring project using Spring STS IDE including the below mentioned dependencies into the project.
Dependencies:
- Spring Web
- Lombok
- Spring dev tools
Once the the project creation completed successfully, it will look like below image,

Step 2: Open the application.properties file and put the below code for the server port and spring security credentials configuration to the project.
spring.application.name=spring-properties-demo
app.greeting.message=Hello, Spring Boot!
Step 3: Create the new package and it named as the service in that package create the new Java class and it named as GreetingService. Go to src > org.example.springpropertiesdemo > service > GreetingService and put the below code.
Java
package org.example.springpropertiesdemo.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class GreetingService {
@Value("${app.greeting.message}")
private String greetingMessage;
public String getGreetingMessage() {
return greetingMessage;
}
}
Step 4: Create the new package and it named as the controller in that package create the new Java class and it named as GreetingController. Go to src > org.example.springpropertiesdemo > service > GreetingController and put the below code.
Java
package org.example.springpropertiesdemo.controller;
import org.example.springpropertiesdemo.service.GreetingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@Autowired
private GreetingService greetingService;
@GetMapping("/greeting")
public String getGreeting() {
return greetingService.getGreetingMessage();
}
}
Step 5: Open the main class and put the below code.
Java
package org.example.springpropertiesdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringPropertiesDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringPropertiesDemoApplication.class, args);
}
}
pom.xml:
XML
<?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>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>spring-properties-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-properties-demo</name>
<description>spring-properties-demo</description>
<properties>
<java.version>17</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 6: Once complete the spring project and it run as spring application once it runs successful then it starts at port 8080.

API Test:
GET http://localhost:8080/greeting
Image Output:

In the above example, We can demonstrates the how to use the properties in the Spring application to configure the simple greeting message that can be accessed via to the REST Endpoint.
Similar Reads
What's New in Spring 6 and Spring Boot 3?
Spring and Spring Boot are two of the most popular Java frameworks used by developers worldwide. The Spring team is continuously working on improving and enhancing the frameworks with each new major release. Spring 6 and Spring Boot 3 are expected to bring in significant new features and changes tha
5 min read
Spring Security Integration with Spring Boot
Spring Security is a powerful and customizable authentication and access control framework for Java applications. It provides comprehensive security services for Java EE-based enterprise software applications. This article will integrate Spring Security with a Spring Boot application, covering confi
5 min read
Securing Spring Boot API With API Key and Secret
In Web applications, securing the APIs is critical. One of the common methods of securing the APIs is by using API keys and secrets. This ensures that only the authorized clients can access the API endpoints. This article can guide you through the process of securing the Spring Boot API using the AP
6 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
Spring Boot Setup with Kotlin
Spring Boot is one of the best frameworks available to build full-stack enterprise applications. Initially, Spring was used to build stand-alone applications on the Java Platform supporting easy-to-develop and providing lots of handy features to ease the application development. Why Kotlin is Used?T
5 min read
Changing Spring Boot Properties at Runtime
Spring Boot provides a flexible way to configure application properties, typically defined statically in the application.properties or application.yml files. However, there are scenarios where properties need to be changed dynamically at runtime without restarting the application. In this article, w
5 min read
Spring Boot â Setting Up a Spring Boot Project with Gradle
Spring Boot is a Java framework designed to simplify the development of stand-alone, production-ready Spring applications with minimal setup and configuration. It simplifies the setup and development of new Spring applications, reducing boilerplate code.In this article, we will guide you through set
4 min read
How to Build a RESTful API with Spring Boot and Spring MVC?
RESTful APIs have become the standard for building scalable and maintainable web services in web development. REST (Representational State Transfer) enables a stateless, client-server architecture where resources are accessed via standard HTTP methods. This article demonstrates how to create a RESTf
7 min read
Add Prefix to All Spring Boot Controllers
The Spring Boot framework provides a lot of features for solving real-time problems in the software industry. Here we will discuss how to add a prefix to all APIs or Controller in the Spring Boot Application. The prefix means The APIs have a common part in the URL or API endpoints. We can achieve th
6 min read
Dockerize Spring Boot Application with MySQL
Spring boot is the most modern framework used for building microservice-based applications today. Deploying spring boot applications requires multiple steps which require complex infrastructure and tools. Docker helps with the easy deployment of spring boot applications with the help of containers.
4 min read