@See AutowiredAnnotationBeanPostProcessor
private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, this.autowiredAnnotationTypes)) {
return InjectionMetadata.EMPTY;
} else {
List<InjectedElement> elements = new ArrayList();
Class targetClass = clazz;
do {
List<InjectedElement> currElements = new ArrayList();
ReflectionUtils.doWithLocalFields(targetClass, (field) -> {
MergedAnnotation<?> ann = this.findAutowiredAnnotation(field);
if (ann != null) {
if (Modifier.isStatic(field.getModifiers())) {
if (this.logger.isInfoEnabled()) {
this.logger.info("Autowired annotation is not supported on static fields: " + field);
}
return;
}
源码中说的很清楚,静态字段不支持自动注入.因而无法构建一个Injectmetadata.而自动注入的前提就是存在这样的InjectMetadata对象.同样的道理,想再静态方法上使用
注入注解也是不支持的.
@ 解决方法
另外写一个非静态方法,通过方法上的注入注解,间接为静态属性赋值.