Spring `@Value` Property Injection

Spring’s `@Value` annotation injects individual configuration values into Spring-managed objects. It is commonly used for simple properties such as URLs, feature flags, ports, or other application settings.

`@Value` can place a value from Spring’s `Environment` into a field, constructor parameter, method parameter, or method. A common form is `@Value("${app.name}")`, where `${app.name}` is resolved from the application’s configuration sources.

The property can come from sources such as `application.properties`, `application.yml`, environment variables, or other configured property sources. Spring resolves the placeholder when it creates the bean and performs the injection.

A default value can be supplied after a colon, such as `@Value("${app.timeout:30}")`. If the property is absent, Spring uses `30` rather than failing because the placeholder has no value.

The target type does not have to be `String`. Spring can convert compatible property text to types such as `int`, `boolean`, or other supported conversion targets.

For a small number of unrelated configuration values, `@Value` is convenient. When an application has a group of related settings, `@ConfigurationProperties` is often easier to organize and validate.

Key points

Interview questions

What does `@Value("${server.port}")` do in Spring?

It tells Spring to resolve the `server.port` configuration property and inject the result into the annotated injection point. The property may come from Spring’s configured property sources. If the value can be converted to the target type, Spring performs that conversion. If the property cannot be resolved and no default is supplied, bean creation can fail.

How do you provide a default value with `@Value`?

Use a colon inside the placeholder, for example `@Value("${server.port:8080}")`. Spring uses `8080` when `server.port` is not available from the configured property sources. This is useful for optional settings with a sensible fallback. Without the default, an unresolved placeholder can prevent the bean from being created.

Where can an `@Value` property come from?

The value is resolved through Spring’s environment and its configured property sources. Common sources include application property or YAML files and environment variables. Spring Boot also defines precedence rules among its configuration sources. The important interview point is that `@Value` does not inherently mean the value must come from one specific file.

When would you choose `@ConfigurationProperties` instead of `@Value`?

I would consider `@ConfigurationProperties` when several related settings form a configuration object. It gives the configuration a clear structure and supports typed binding, which can make larger configuration models easier to maintain. `@Value` remains convenient for a small number of individual values. For configuration that needs systematic validation and organization, `@ConfigurationProperties` is usually a better fit.