Spring @Value annotation with Example





Hey guys in this post we will discuss Spring @Value annotation with example. This annotation is not specific to Spring, it also a part of Spring Boot.

Overview


@Valueannotation is used to read the properties from the application.properties file inside the spring boot. This annotation can be applied to a fields.

@Value("${app.name}")
private String appName;

Here, app.name is the property name which is present in the application.properties file. We are accessing the property app.name through @Value annotation and binding it to the field appName. To access the property name we need to use ${}. If the property is not present in the application.properties file then spring will throw an exception, when we run the application.

We can also provide default values for properties using @Value annotation

@Value("${app.version:version1}")
private String appVersion;

Here, we are providing the default value as version1 seperated with : (colon). If the property is not present in the application.properties file then it will take the default value instead of throwing the exception.

Example on @Value


The best way to understand @Valueis by looking at the example

Create spring boot project


There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies


Open pom.xml and add the following dependencies –

<?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.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>restcontrollerannotation</artifactId>
	<version>v1</version>
	<name>restcontrollerannotation</name>
	<description>Spring boot restcontroller annotation demo</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-devtools</artifactId>
			<scope>runtime</scope>
			<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>
			</plugin>
		</plugins>
	</build>

</project>

spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.




spring-boot-devtools dependency for automatic reloads or live reload of applications.

configure properties


To configure the properties open application.properties and add the following content

app.name=Demp app

Create a Rest controller


Create HomeController.java inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
	
	@Value("${app.name}")
	private String appName;
	
	@Value("${app.version:version1}")
	private String appVersion;
	
	@GetMapping("/version")
	public String getAppVersion () {
		return appName+" - "+appVersion;
	}
}

We have annotated the class with @RestController annotation, this indicates that the class serves the role of controller and also returns JSON response from all the methods.

Run the app


Run the application using the below maven command –

mvn spring-boot:run

Open the browser and navigate to the URL http://localhost:8080/version
Capture

That’s all about @Value annotation. Hope you like this post, if so share this with your friends and colleagues or share this with any of the social media profiles. I will see you in the next post.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply