It's not possible to use @Value
on a static variable.
@Value("${some.value}")
static private int someValue;
static public void useValue() {
System.out.println(someValue);
}
When I do this, 0
is printed. So what is a good alternative to this?
Use this simple trick to achieve what you want (way better than having the value injected into non-static setters and writing so a static field - as suggested in the accepted answer):
@Service
public class ConfigUtil {
public static ConfigUtil INSTANCE;
@Value("${some.value})
private String value;
@PostConstruct
public void init() {
INSTANCE = this;
}
public String getValue() {
return value;
}
}
Use like:
ConfigUtil.INSTANCE.getValue();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With