Introduction
@Value annotation can inject environment variable. For instance we can use ${VARIABLE_NAME} to get variable. If we want to get default value if variable is not defined we have to put this after : char e.g. ${VARIABLE_NAME:defaultValue}. If variable and default value is not defined it throws error.
Problem
The problem is with using application.yml.
Project Example
/resources/application.yml
app:
bug:
key: ${VARIABLE_NAME}
BugProperties.java
@ConfigurationProperties("app.bug")
public class BugProperties {
private String key;
// getter & setter
}
How it behaviour
In this example bugProperties.getKey() if variable is not defined it returns ${VARIABLE_NAME} instead throw error or set just null.
Solution
I think that there are two option to consider.
Be consistent with @Value(...) annotation
Then it should throw an exception to be consistent with @Value(...) annotation.
Personally Preferred Solution
Then value should be set as null, I think that it is the best option for configuration properties because it is very often that configuration properties are validated using jakarta annotations like @NotNull, @NotBlank, @Size(...), and so on.
I would love to do a PR which fixes this functionality.
Comment From: snicoll
This is working as designed, please see the reference documentation.