Because when the class loader loads the static values, the Spring context is not yet necessarily loaded. So the class loader won't properly inject the static fields in the bean and will fail.
Its basically an "ANTI-PATTERN", Still we have a hack to work around this limitation, Here it is.
https://stackoverflow.com/questions/1018797/can-you-use-autowired-with-static-fields/5991240#5991240
@Component("NewClass") public class NewClass{ private static SomeThing someThing; @Autowired public void setSomeThing(SomeThing someThing){ NewClass.someThing = someThing; } }
No comments:
Post a Comment