Hugh Lee Hugh Lee - 1 year ago 136
Java Question

An alternative to @Value annotation in static function

It's not possible to use

on a static variable.

static private int someValue;

static public void useValue() {

When I do this,
is printed. So what is a good alternative to this?

Answer Source

Spring inject noting in static field (by default).

So you have two alternatives:

  • (the better one) make the field non static
  • (the ugly hack) add an none static setter which writes in the static field, and add the @Value annotation to the setter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download