Open In App

Properties with Spring and Spring Boot

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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,


propertiesfile


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.

propertiesspringlog-compressed


API Test:

GET http://localhost:8080/greeting


Image Output:

propertiespostmanlog

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads