← Back to Questions
Spring Boot

What is @Value annotation in Spring?

Learn What is @Value annotation in Spring? with simple explanations, real-time examples, interview tips and practical use cases.

What is @Value Annotation in Spring?

The @Value annotation in Spring is used to inject values into variables from:

  • application.properties
  • application.yml
  • Environment variables
  • System properties
  • Direct hardcoded values

It allows developers to externalize configuration and dynamically inject values into Spring Beans.

In simple terms:

  • @Value injects configuration values
  • It reads values from property files
  • It supports dynamic configuration
  • It avoids hardcoding application settings

The @Value annotation is heavily used in:

  • Spring Framework
  • Spring Boot
  • Microservices Architecture
  • Cloud-Native Systems
  • Enterprise Applications

Simple Definition

The @Value annotation is used to inject configuration values into Spring-managed beans.


Why @Value Annotation is Important

Enterprise applications require:

  • Dynamic configuration
  • Environment-specific settings
  • Secure secret management
  • Flexible deployment configuration

Hardcoding values directly inside Java classes creates problems:

  • Poor maintainability
  • Difficult deployments
  • Security risks
  • Environment dependency issues

The @Value annotation solves these problems.


Real-Time Banking Example

Consider a banking application requiring:

  • Database URLs
  • API keys
  • JWT secrets
  • Email server configuration
  • Payment gateway keys

Instead of hardcoding:

private String apiKey =
    "abcd1234";
    

Developers use:

@Value("${payment.api.key}")
    

Values are loaded dynamically from configuration files.


How @Value Works Internally

Application Starts
      |
Spring Loads Property Files
      |
@Value Annotation Detected
      |
Property Key Resolved
      |
Value Injected into Bean
      |
Application Ready
    

Basic @Value Example

application.properties

app.name=Dhanish Empower
    

Java Class

@Component
public class AppService {

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

}
    

Spring injects:

Dhanish Empower
    

into:

appName
    

Injecting Direct Values

The @Value annotation can inject hardcoded values directly.

Example

@Value("Spring Boot")
private String framework;
    

Injecting Integer Values

application.properties

server.port=9090
    

Java Class

@Value("${server.port}")
private int port;
    

Injecting Boolean Values

application.properties

feature.enabled=true
    

Java Class

@Value("${feature.enabled}")
private boolean enabled;
    

Injecting Default Values

Spring supports default fallback values.

Example

@Value("${server.timeout:30}")
private int timeout;
    

If:

server.timeout
    

is missing, Spring uses:

30
    

Injecting Environment Variables

Example

@Value("${JAVA_HOME}")
private String javaHome;
    

Using @Value with Constructor Injection

Constructor Injection is the recommended approach.

Example

@Component
public class PaymentService {

    private final String apiKey;

    public PaymentService(

        @Value("${payment.api.key}")
        String apiKey) {

        this.apiKey = apiKey;
    }

}
    

Using @Value with Setter Injection

@Value("${app.name}")
public void setAppName(String appName) {

    this.appName = appName;
}
    

Using @Value with application.yml

application.yml

server:
  port: 9090
    

Java Class

@Value("${server.port}")
private int port;
    

Using @Value with Expressions

Spring supports Spring Expression Language (SpEL).

Example

@Value("#{10 + 20}")
private int total;
    

Injected value:

30
    

Using @Value with Lists

application.properties

app.countries=India,USA,UK
    

Java Class

@Value("#{'${app.countries}'.split(',')}")
private List<String> countries;
    

@Value vs Hardcoded Values

Feature @Value Hardcoded Values
Flexibility High Low
Environment Support Yes No
Maintainability Better Difficult
Security Better Risky

Advantages of @Value Annotation

  • Externalized configuration
  • Environment flexibility
  • Improved maintainability
  • Better security
  • Dynamic property management

1. Externalized Configuration

Configuration values remain outside Java code.


2. Environment Flexibility

Different environments can use different property values.

Example:

  • Development
  • Testing
  • Production

3. Better Security

Sensitive data such as:

  • Passwords
  • API keys
  • JWT secrets

can remain outside source code.


4. Easier Deployment

Configuration changes do not require recompiling the application.


Disadvantages of @Value Annotation

  • Large numbers of properties become difficult to manage
  • Scattered configuration values
  • Limited validation support

@Value vs @ConfigurationProperties

Feature @Value @ConfigurationProperties
Usage Single Properties Grouped Properties
Validation Limited Better
Maintainability Good for Small Configs Better for Large Configs

@Value in Spring Boot

Spring Boot heavily uses:

@Value
    

for:

  • Server configuration
  • Database URLs
  • API keys
  • JWT secrets
  • Cloud configurations

Spring Boot Example

application.properties

spring.datasource.url=
jdbc:mysql://localhost:3306/bank_db
    

Java Class

@Value("${spring.datasource.url}")
private String databaseUrl;
    

@Value in Microservices

Modern microservices architectures heavily rely on:

@Value
    

because:

  • Configurations differ across services
  • Cloud environments require dynamic values
  • Secrets management becomes important
  • Containerized deployments need flexibility

Microservices Banking Example

@Value("${gateway.internal.admin.secret}")
private String adminSecret;
    
@Value("${service.payment.url}")
private String paymentServiceUrl;
    

Values are loaded dynamically during startup.


Common Spring Annotations Related to @Value

  • @Value
  • @PropertySource
  • @ConfigurationProperties
  • @Configuration
  • @Bean

Real-World Enterprise Use Cases

  • Database configuration
  • Payment gateway setup
  • Cloud service integration
  • API key management
  • Security configuration

Common Interview Questions

What is @Value annotation in Spring?

The @Value annotation is used to inject configuration values into Spring Beans.

Can @Value read values from application.properties?

Yes. @Value can read values from application.properties and application.yml files.

What is the difference between @Value and @ConfigurationProperties?

@Value is used for individual properties, while @ConfigurationProperties is used for grouped property configurations.


Professional Interview Answer

The @Value annotation in Spring is used to inject configuration values into Spring-managed beans from property files, environment variables, system properties, or hardcoded expressions. It supports dynamic application configuration and helps externalize environment-specific settings such as database URLs, API keys, JWT secrets, and service endpoints. The @Value annotation is heavily used in Spring Boot and microservices architectures for flexible and maintainable configuration management.


Conclusion

The @Value annotation is one of the most important configuration annotations in Spring Framework and Spring Boot.

It enables dynamic property injection, externalized configuration management, and environment-specific application customization.

Modern enterprise systems, banking platforms, cloud-native applications, REST APIs, and microservices architectures heavily rely on @Value for secure, scalable, and maintainable configuration handling.

Understanding the @Value annotation is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.

Why this Spring Boot question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.