Understanding the @Value Annotation | Spring Boot | Java Spring

Spring Boot (5 Part Series)

1 Let’s Decode ORM, ORM Tools, JPA and Spring Data JPA | Spring Boot | Java Spring
2 ResponseEntity in Spring Boot | HttpStatus | REST API | Java Spring
3 Project Lombok | Java Library | Simplify Your Code | Spring Boot | Java Spring
4 JUnit Testing | Spring Boot | Java Spring
5 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!
Hello, Spring Boot!
Hello, Spring Boot!

Enter fullscreen mode Exit fullscreen mode

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!
Welcome to Spring Boot!
Welcome to Spring Boot!

Enter fullscreen mode Exit fullscreen mode

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]
[USA, UK, Canada, India]
[USA, UK, Canada, India]

Enter fullscreen mode Exit fullscreen mode

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!

Spring Boot (5 Part Series)

1 Let’s Decode ORM, ORM Tools, JPA and Spring Data JPA | Spring Boot | Java Spring
2 ResponseEntity in Spring Boot | HttpStatus | REST API | Java Spring
3 Project Lombok | Java Library | Simplify Your Code | Spring Boot | Java Spring
4 JUnit Testing | Spring Boot | Java Spring
5 Understanding the @Value Annotation | Spring Boot | Java Spring

原文链接:Understanding the @Value Annotation | Spring Boot | Java Spring

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
To do the useful thing, to say the courageous thing, to contemplate the beautiful thing: that’s enough for one man’s life.
做有用的事,说勇敢的话,想美好的事,一生足矣
评论 抢沙发

请登录后发表评论

    暂无评论内容