Why can't we autowire static fields in spring?

Ashu picture Ashu · Jun 7, 2012 · Viewed 58.5k times · Source

Why can't we autowire the static instance variable in the Spring bean. I know there is another way to achieve this but just want to know why cant we do it in below way.

e.g.

@Autowired
public static Test test;

Answer

Andrea T picture Andrea T · Feb 19, 2014

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.