mohican93
mohican93

Reputation: 121

Why in the annotation-based Spring app @Value default value is getting resolved to null?

In my class, I have 2 private fields that are pre-populated via @Value annotation. Although the value is getting read correctly from .properties file, its default value is never applied if in .properties file it's not set.

I have tried with creating PropertySourcesPlaceholderConfigurer bean and specifying location to the file: "classpath:application.properties". These are my properties:

@Value("${year:2019}")
private Integer year;

resources/application.properties:

year=

When the year is set, Integer year receives the right value. If it remains empty, I expect default value (2019) to be set, but it remains null.

Spring version: 5.1.8.RELEASE

Upvotes: 0

Views: 93

Answers (1)

mwarren
mwarren

Reputation: 759

It is null because your properties file sets it to blank. If you remove the year= from the properties file you should get the default value.

Upvotes: 3

Related Questions