In this tutorial, we will learn how to use @Value Annotation in Spring or Spring boot applications.
YouTube Video
@Value Annotation Overview
Spring @Value annotation is used to assign default values to variables and method arguments.
The @Value annotation is mostly used to get value for specific property keys from the properties file.
We can read spring environment variables as well as system variables using @Value annotation.
@Value Annotation Example
In this example, we will see how to use @Value annotation to read the value for a property key, and read environment variables as well as system variables.
Add the below properties to the application.properties file:
mail.email=javaguides@gmail.com
mail.host=gmail.com
mail.password=password
Next, we will see how to read the values for the above property keys using @Value annotation.
Using @Value Annotation:
Create a ValueAnnotationDemo class and add the following to it:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ValueAnnotationDemo {
@Value("default value")
private String testDefaultValue;
@Value("${java.home}")
private String javaHome;
@Value("${HOME}")
private String homeDir;
@Value("${mail.email}")
private String email;
@Value("${mail.host}")
private String host;
@Value("${mail.password}")
private String password;
public String getTestDefaultValue() {
return testDefaultValue;
}
public String getJavaHome() {
return javaHome;
}
public String getHomeDir() {
return homeDir;
}
public String getEmail() {
return email;
}
public String getHost() {
return host;
}
public String getPassword() {
return password;
}
}
From the above code, Spring @Value annotation is used to assign a default value to the variable:
@Value("default value")
private String testDefaultValue;
Spring @Value annotation used to read the property values:
@Value("${mail.email}")
private String email;
@Value("${mail.host}")
private String host;
@Value("${mail.password}")
private String password;
Reading spring environment variables as well as system variables using @Value annotation:
@Value("${java.home}")
private String javaHome;
@Value("${HOME}")
private String homeDir;
Testing:
Next, let's test the usage of @Value annotation:
import net.javaguides.springboot.config.ValueAnnotationDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
var context = SpringApplication.run(SpringbootDockerDemoApplication.class, args);
ValueAnnotationDemo valueAnnotationDemo = context.getBean(ValueAnnotationDemo.class);
System.out.println(valueAnnotationDemo.getTestDefaultValue());
System.out.println(valueAnnotationDemo.getHost());
System.out.println(valueAnnotationDemo.getEmail());
System.out.println(valueAnnotationDemo.getPassword());
System.out.println(valueAnnotationDemo.getJavaHome());
System.out.println(valueAnnotationDemo.getEmail());
System.out.println(valueAnnotationDemo.getHomeDir());
}
}
Output:
default value
gmail.com
javaguides@gmail.com
password
/Users/rameshfadatare/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home
javaguides@gmail.com
/Users/rameshfadatare
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment