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 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: Output: Hello, Spring Boot! 2. Injecting Values from application.properties First, define a property in application.properties: Now, inject this value into your class: Output: Welcome to Spring Boot! 3. Setting Default Values What if the property is missing from application.properties? We can set a default value: 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: Inject them in your Spring Boot class: Now you can use port and isFeatureEnabled in your application. 5. Injecting Lists and Arrays Define a comma-separated list in application.properties: Inject it as a List or an Array: 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!

Feb 17, 2025 - 20:00
 0
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 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!