1. 程式人生 > >Spring MVC 解讀——@Autowired、@Controller、@Service從原理層面來分析

Spring MVC 解讀——@Autowired、@Controller、@Service從原理層面來分析

?
//雖然PropertyValues屬性傳遞過去了,但是並沒有使用它直接賦值給屬性變數(還不清楚為什麼會傳遞它,其實沒用到)
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, 
                PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        //呼叫下面的方法獲取InjectionMetadata物件(其實InjectionElement集合)
        InjectionMetadata metadata = findAutowiringMetadata(bean.getClass());
        try {
            metadata.inject(bean, beanName, pvs);
        }
        return pvs;
    }
 
private InjectionMetadata findAutowiringMetadata(Class<?> clazz) {
        // 先找快取
        InjectionMetadata metadata = this.injectionMetadataCache.get(clazz);
        if (metadata == null) {
            synchronized (this.injectionMetadataCache) {
                metadata = this.injectionMetadataCache.get(clazz);
                if (metadata == null) {
                    //快取沒有,呼叫buildAutowiringMetadata方法構建
                    metadata = buildAutowiringMetadata(clazz);
                    this.injectionMetadataCache.put(clazz, metadata);
                }
            }
        }
        return metadata;
    }
 
private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
        LinkedList<InjectionMetadata.InjectedElement> elements = 
                                        new LinkedList<InjectionMetadata.InjectedElement>();
        Class<?> targetClass = clazz;
        do {//這裡一個迴圈,因為要考慮父類的欄位和方法
            LinkedList<InjectionMetadata.InjectedElement> currElements = 
                                    new LinkedList<InjectionMetadata.InjectedElement>();
            for (Field field : targetClass.getDeclaredFields()) {
                //遍歷每一個field,找到被標記為@Autowired的field
                Annotation annotation = findAutowiredAnnotation(field);
                if (annotation != null) {
                    if (Modifier.isStatic(field.getModifiers())) {
                        continue;//不可一世static的。
                    }
                    boolean required = determineRequiredStatus(annotation);
                    //建立AutowiredFieldElement。
                    currElements.add(new AutowiredFieldElement(field, required));
                }
            }
            for (Method method : targetClass.getDeclaredMethods()) {
                //遍歷所有方法,這裡有個橋方法的處理,我們不關心
                Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
                Annotation annotation = BridgeMethodResolver
                                    .isVisibilityBridgeMethodPair(method, bridgedMethod) ?
                                        findAutowiredAnnotation(bridgedMethod) :
                                     findAutowiredAnnotation(method);
                if (annotation != null && 
                            method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        continue;
                    }
                    if (method.getParameterTypes().length == 0) {
                    }
                    boolean required = determineRequiredStatus(annotation);
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
                    //建立AutowiredMethodElement。
                    currElements.add(new AutowiredMethodElement(method, required, pd));
                }
            }
            elements.addAll(0, currElements);
            targetClass = targetClass.getSuperclass();
        }
        while (targetClass != null && targetClass != Object.class);
        //將InjectionElement集合新增到新建的InjectionMetadata中。
        return new InjectionMetadata(clazz, elements);
    }