What you can do is @Autowired
a setter method and have it set a new static field.
public class Boo {
@Autowired
Foo foo;
static Foo staticFoo;
@Autowired
public void setStaticFoo(Foo foo) {
Boo.staticFoo = foo;
}
public static void randomMethod() {
staticFoo.doStuff();
}
}
When the bean gets processed, Spring will inject a Foo
implementation instance into the instance field foo
. It will then also inject the same Foo
instance into the setStaticFoo()
argument list, which will be used to set the static field.
This is a terrible workaround and will fail if you try to use randomMethod()
before Spring has processed an instance of Boo
.