@Value

In Spring Framework, the @Value annotation is used to inject values into fields, methods, or constructor parameters of a Spring bean. It allows you to externalize configuration or specify property values directly in your code, rather than hardcoding them. This can be particularly useful for configuring various aspects of your application without modifying the source code.

Here's how you can use the @Value annotation in different contexts:

Injecting into Fields:

import org.springframework.beans.factory.annotation.Value;

public class MyBean {
    @Value("${property.name}")
    private String propertyName;

    // rest of the class
}

In this example, the value of propertyName will be injected from the property named property.name in the configuration file (e.g., application.properties).

Remember to enable property placeholder resolution in your Spring configuration. In a typical Spring Boot application, this is automatically enabled, and you can use properties in the application.properties or application.yml files.

# application.yml
property:
  name: my-property-value