Annotation @Value

The annotation @Value is to inject Spring External Configuration into the application, for example

In application.yml in resources/ folder

name: "Austin" # or name: Austin is ok as well

And then we can read this by using:

@Component  
public class MyComponent {  
  private static final Logger logger = LoggerFactory.getLogger(MyComponent.class);  
  
  @Value("${name}")  
  private String nameAfterConstructorInitialised;  
  
  public MyComponent(@Value("${name}") String name) {  
    logger.info(logger.getClass().getName());  
    logger.info("My component is created, name is {}", name);  
  }  
  
  public String getInfo() {  
    return nameAfterConstructorInitialised;  
  }  
}

Note that in here the field nameAfterConstructorInitialised will not be initialise during the start of the constructor.

Therefore if we want to use this field during constructor phase, we need to inject into constructor parameter.

[!danger]
Don't mistake with lombok @Value which is an immutable state of lombok @Data which is for completely different purpose: https://projectlombok.org/features/Value