spring framework

spring中无法使用 @Autowired注解注入静态变量的原

2021-01-03  本文已影响0人  Poison毒药_d97d
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
    // 存放当前类包括父类中带@Autowired注解的字段和方法
    List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
    Class<?> targetClass = clazz;

    do {
        // 存放targetClass中所有带了@Autowired注解的字段和方法
        final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();

        ReflectionUtils.doWithLocalFields(targetClass, field -> {
            // JDK1.8 新特性,传入一个方法进去,在方法内部就是获取当前类的所有字段(不包括父类,
            // 包括自己定义的私有变量),并循环调用传入的方法,
            // 即当前方法

            // 判断当前字段是否有@Autowired注解
            AnnotationAttributes ann = findAutowiredAnnotation(field);
            if (ann != null) {
                // 判断当前字段是否为static修饰  ===>  静态变量, 如果是,则只是返回了
                if (Modifier.isStatic(field.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static fields: " + field);
                    }
                    return;
                }
                boolean required = determineRequiredStatus(ann);
                // 将字段包装成AutowiredFieldElement对象,并存入一开始创建的list中
                currElements.add(new AutowiredFieldElement(field, required));
            }
        });

        ReflectionUtils.doWithLocalMethods(targetClass, method -> {
            // 同上,此时获取的是当前class内部的method(不包括父类, 包括自己定义的私有方法
            // ),并挨个遍历执行当前传入的方法

            // 判断遍历的方法是否为桥接方法
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                return;
            }

            // 拿到当前方法的@Autowired注解,并进行校验拿到真实方法(因为有可能当前要处理的类是一个代理对象,或者接口等等)
            AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
            if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                // 判断当前方法是否为静态方法
                if (Modifier.isStatic(method.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static methods: " + method);
                    }
                    return;
                }
                if (method.getParameterCount() == 0) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation should only be used on methods with parameters: " +
                                method);
                    }
                }
                boolean required = determineRequiredStatus(ann);

                // 找到当前方法中的参数描述器
                PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                // 将set方法和要传入的属性的描述器包装成AutowiredMethodElement类并添加至一开始创建的list集合中
                currElements.add(new AutowiredMethodElement(method, required, pd));
            }
        });

        elements.addAll(0, currElements);
        // 获取父类的class,针对父类的属性和方法在做筛选
        targetClass = targetClass.getSuperclass();
    }
    while (targetClass != null && targetClass != Object.class);

    // 最终返回一个InjectionMetadata对象,其中包含当前类及其父类(不包含Object类)的所有带
    
    return new InjectionMetadata(clazz, elements);
}   

由此可见在处理@Autowired时其会直接过滤掉静态方法和变量,因此如果使用静态变量就必须提供一个非静态的方法去给其提供值

上一篇 下一篇

猜你喜欢

热点阅读