Understanding the @Value Annotation | Spring Boot | Java Spring

Spring Boot makes it super easy to inject values into variables using the @Value annotation. Whether you need to read values from application.properties, set default values, or inject environment variables, @Value is your go-to solution!

What…


This content originally appeared on DEV Community and was authored by Debesh P.

Spring Boot makes it super easy to inject values into variables using the @Value annotation. Whether you need to read values from application.properties, set default values, or inject environment variables, @Value is your go-to solution!

What is @Value in Spring Boot?

The @Value annotation in Spring Boot allows us to inject values from property files, environment variables, or even directly as literals. It is commonly used to fetch configuration values from application.properties or application.yml so that we can keep our code clean and flexible.

Why Use @Value?

  • It helps keep configuration separate from code.

  • It allows easy modification of values without changing the source code.

  • It supports setting default values in case a property is missing.

  • It makes the application configurable and adaptable.

How to Use @Value Annotation?

1. Injecting a Simple String Value

We can directly assign a string value using @Value:

demo

Output:

Hello, Spring Boot!

2. Injecting Values from application.properties

First, define a property in application.properties:

demo

Now, inject this value into your class:

demo

Output:

Welcome to Spring Boot!

3. Setting Default Values

What if the property is missing from application.properties? We can set a default value:

demo

If app.welcomeMessage is not found, Spring Boot will use "Default Welcome Message" instead.

4. Injecting Numeric and Boolean Values

Define values in application.properties:

demo

Inject them in your Spring Boot class:

demo

Now you can use port and isFeatureEnabled in your application.

5. Injecting Lists and Arrays

Define a comma-separated list in application.properties:

demo

Inject it as a List or an Array:

demo

Output:

[USA, UK, Canada, India]

When to Use @Value and When Not To?

Use @Value when:

  • Injecting simple values like Strings, numbers, and Booleans.

  • Fetching environment-specific configurations.

  • Setting default values when the property might be missing.

Avoid @Value when:

  • Injecting complex configurations (Use @ConfigurationProperties instead).

  • Managing large numbers of properties (Use a dedicated configuration class).

Conclusion

The @Value annotation is a powerful tool in Spring Boot for injecting configuration values into your application. It helps keep your code clean, configurable, and easy to maintain.

Now go ahead and experiment with @Value in your Spring Boot project.

happy coding!


This content originally appeared on DEV Community and was authored by Debesh P.


Print Share Comment Cite Upload Translate Updates
APA

Debesh P. | Sciencx (2025-02-17T18:42:30+00:00) Understanding the @Value Annotation | Spring Boot | Java Spring. Retrieved from https://www.scien.cx/2025/02/17/understanding-the-value-annotation-spring-boot-java-spring/

MLA
" » Understanding the @Value Annotation | Spring Boot | Java Spring." Debesh P. | Sciencx - Monday February 17, 2025, https://www.scien.cx/2025/02/17/understanding-the-value-annotation-spring-boot-java-spring/
HARVARD
Debesh P. | Sciencx Monday February 17, 2025 » Understanding the @Value Annotation | Spring Boot | Java Spring., viewed ,<https://www.scien.cx/2025/02/17/understanding-the-value-annotation-spring-boot-java-spring/>
VANCOUVER
Debesh P. | Sciencx - » Understanding the @Value Annotation | Spring Boot | Java Spring. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/17/understanding-the-value-annotation-spring-boot-java-spring/
CHICAGO
" » Understanding the @Value Annotation | Spring Boot | Java Spring." Debesh P. | Sciencx - Accessed . https://www.scien.cx/2025/02/17/understanding-the-value-annotation-spring-boot-java-spring/
IEEE
" » Understanding the @Value Annotation | Spring Boot | Java Spring." Debesh P. | Sciencx [Online]. Available: https://www.scien.cx/2025/02/17/understanding-the-value-annotation-spring-boot-java-spring/. [Accessed: ]
rf:citation
» Understanding the @Value Annotation | Spring Boot | Java Spring | Debesh P. | Sciencx | https://www.scien.cx/2025/02/17/understanding-the-value-annotation-spring-boot-java-spring/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.