Spring allows users to dependancy inject / autowire beans into objects, essentially by doing.
public class Something{
@Autowired
private InjectMe injectMe
}
And upon creating a new instance of this class, Spring makes sure that injectMe gets filled with the current bean if present, or a new instance if not.Useful for injecting DAOs in your DAL and various other places where it's useful.
Problem is, I need this autowired object inside a static method. Meaning to object gets created, and Spring.. as far as I know does not autowire static fields.
public class Something{
@Autowired
private static InjectMe injectMe
public static useInjectMe(){
injectMe.foo();
}
}
^ That's what I want.v This is what's the current workaround:
public class Something{
@Autowired
private InjectMe injectMe
public static useInjectMe(){
new Someting().injectMe.foo();
}
}
Which looks awful, and there's got to be a better way.Anyone ever been in this situation before?


Sign In
Create Account


Back to top









