How can I inject value in a static final field from application.properties file
Injecting in just static field is possible but injecting in static final field, how it can be achieved
@Value and System.getenv do not work.
How can I inject value in a static final field from application.properties file
Injecting in just static field is possible but injecting in static final field, how it can be achieved
@Value and System.getenv do not work.
Share Improve this question asked Feb 17 at 11:32 Akash BharambeAkash Bharambe 11 bronze badge 3 |1 Answer
Reset to default 1There is no simple reliable way to achieve that.
I suggest you to redesign your code.
static
field inject doesn't work either without hacks.static final
is a constant and is resolved during compile time. – M. Deinum Commented Feb 17 at 12:35static final
is a constant and is resolved during compile time." depends on the expression¹ being assigned (e.g.static final LocalDate today = LocalDate.now();
) (not sure what you mean by "resolved") || ¹ see JLS 15.29. Constant Expressions – user85421 Commented Feb 17 at 14:59String
or a primitive it will be resolved at compile time, so if you have something like thisstatic final String FOO = SomeOtherClass.FOO_VALUE
it will copy the value ofSomeOtherClass.FOO_VALUE
into your class. It will not change it during loadtime or runtime. And as the question was about@Value
/System.getenv
which mainly concernsString
values that would apply. Although theSystem.getenv
would probably stick around. – M. Deinum Commented Feb 17 at 15:11