spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)

  • 2020 年 4 月 13 日
  • 筆記

在上篇部落格中分享了InstantiationAwareBeanPostProcessor介面中的四個方法,分別對其進行了詳細的介紹,在文末留下了一個問題,那就是postProcessProperties方法,說到此方法是用來進行屬性填充的,並且引出了CommonAnnotationBeanPostProcessor類。

一、概述

CommonAnnotationBeanPostProcessor類在spring中是一個極其重要的類,它負責解析@Resource、@WebServiceRef、@EJB三個註解,既然要解析這三個屬性,那麼在CommonAnnotationBeanPostProcessor類中肯定要有這三個註解,下面看這段程式碼,

static {
        try {
            @SuppressWarnings("unchecked")
            Class<? extends Annotation> clazz = (Class<? extends Annotation>)
                    ClassUtils.forName("javax.xml.ws.WebServiceRef", CommonAnnotationBeanPostProcessor.class.getClassLoader());
            webServiceRefClass = clazz;
        }
        catch (ClassNotFoundException ex) {
            webServiceRefClass = null;
        }

        try {
            @SuppressWarnings("unchecked")
            Class<? extends Annotation> clazz = (Class<? extends Annotation>)
                    ClassUtils.forName("javax.ejb.EJB", CommonAnnotationBeanPostProcessor.class.getClassLoader());
            ejbRefClass = clazz;
        }
        catch (ClassNotFoundException ex) {
            ejbRefClass = null;
        }
        //添加@Resource註解
        resourceAnnotationTypes.add(Resource.class);
        if (webServiceRefClass != null) {
//添加@WebServiceRef註解 resourceAnnotationTypes.add(webServiceRefClass); }
if (ejbRefClass != null) {
//添加@EJB註解 resourceAnnotationTypes.add(ejbRefClass); } }

上面是一個靜態程式碼塊,我們知道靜態程式碼塊在什麼時候會執行,那就是在類被載入的時候,也就說在CommonAnnotationBeanPostProcessor類載入的時候就會執行上面的靜態程式碼塊,我們看靜態程式碼塊中的邏輯,可以看出就是向resourceAnnotationTypes中添加了三個註解,就是這個類要解析的註解所代表的介面。

在CommonAnnoatationBeanPostProcessor類中還有一個重要的屬性即injectionMetaDataCache,這個屬性會存儲類中的被CommonAnnoatationBeanPostProcessor解析的欄位或方法資訊,也就是標註了三個屬性的欄位或方法的元資訊。

二、詳述

1、方法概述

下面看CommonAnnoatationBeanPostProcessor類中的方法,

上面是CommonAnnoatationBeanPostProcessor中比較重要的方法,有兩個比較熟悉的方法,在前面已經分析過即postProcessBeforeInstantiation和postProcessAfterInstantiation方法。

@Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
        return null;
    }

上面是postProcessBeforeInstantiation方法,該方法的返回值為null,即不會在bean實例化前產生一個代理對象。

@Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) {
        return true;
    }

上面是postProcessAfterInstantiation方法,該方法的返回值為true,也就是說該類不會阻止屬性的注入。

在CommonAnnoatationBeanPostProcessor中的postProcessProperties方法,

@Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
        InjectionMetadata metadata = findResourceMetadata(beanName, bean.getClass(), pvs);
        try {
            metadata.inject(bean, beanName, pvs);
        }
        catch (Throwable ex) {
            throw new BeanCreationException(beanName, "Injection of resource dependencies failed", ex);
        }
        return pvs;
    }

先看下這個方法,今天這個方法不是主角。主角是下面這個方法,

@Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        super.postProcessMergedBeanDefinition(beanDefinition, beanType, beanName);
        InjectionMetadata metadata = findResourceMetadata(beanName, beanType, null);
        metadata.checkConfigMembers(beanDefinition);
    }

這個方法首先會調用父類的postProcessMergedBeanDefinition方法,然後調用findResourceMetadata方法。

CommonAnnoatationBeanPostProcessor的父類是InitDestoryAnnoatationBeanPostProcessor,在InitDestoryAnnoatationBeanPostProcessor類中postProcessMergedBeanDefinition方法主要完成的解析和初始化(@PostConstruct)和銷毀(@PreDestory)相關的註解並快取到lifecycleMetadataCache中。

CommonAnnoatationBeanPostProcessor的findResourceMetadata方法主要完成的是解析@Resource、@WebServiceRef、@EJB三個註解並快取到injectionMetadataCache中。今天重點分析CommonAnnoatationBeanPostProcessor的postProcessMergedBeanDefinition方法。

2、postProcessMergedBeanDefinition方法

此方法是在什麼時候調用的,從spring的源碼中可以找到如下,在doCreateBean方法中,下面僅貼出部分程式碼,

synchronized (mbd.postProcessingLock) {
            if (!mbd.postProcessed) {
                try {
                    //2、調用beanPostProcessor即bean後置處理器MergedBeanDefinitionPostProcessor,執行其postProcessMergedBeanDefinition方法
                    applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                }
                catch (Throwable ex) {
                    throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                            "Post-processing of merged bean definition failed", ex);
                }
                mbd.postProcessed = true;
            }
        }

上面的程式碼中調用了applyMergedBeanDefinitionPostProcessor方法,applyMergedBeanDefinitionPostProcessor方法如下,

protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof MergedBeanDefinitionPostProcessor) {
                MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
                bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
            }
        }
    }

這裡會循環beanFactory中的beanPostProcessor,這裡肯定會有CommonAnnoatationBeanPostProcessor後置處理器,那麼就會調用到其postProcessMergedBeanDefinition方法。

postProcessMergedBeanDefinition方法如下,

@Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        super.postProcessMergedBeanDefinition(beanDefinition, beanType, beanName);
        InjectionMetadata metadata = findResourceMetadata(beanName, beanType, null);
        metadata.checkConfigMembers(beanDefinition);
    }

上面已經提到先不分析其父類,那麼重點分析的就是findResourceMetadata和checkConfigMembers。findResourceMetadata如下

private InjectionMetadata findResourceMetadata(String beanName, final Class<?> clazz, @Nullable PropertyValues pvs) {
        // Fall back to class name as cache key, for backwards compatibility with custom callers.
        String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
        // Quick check on the concurrent map first, with minimal locking.
        InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
        if (InjectionMetadata.needsRefresh(metadata, clazz)) {
            synchronized (this.injectionMetadataCache) {
                metadata = this.injectionMetadataCache.get(cacheKey);
                if (InjectionMetadata.needsRefresh(metadata, clazz)) {
                    if (metadata != null) {
                        metadata.clear(pvs);
                    }
//重要程式碼,將返回的metadata對象放入injectionMetadatCache快取中,快取key為beanName,供後續方法從快取中取出 metadata
= buildResourceMetadata(clazz); this.injectionMetadataCache.put(cacheKey, metadata); } } } return metadata; }

下面看buildResourceMetadata方法,

private InjectionMetadata buildResourceMetadata(final Class<?> clazz) {
        if (!AnnotationUtils.isCandidateClass(clazz, resourceAnnotationTypes)) {
            return InjectionMetadata.EMPTY;
        }

        List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
        Class<?> targetClass = clazz;

        do {
            final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
             //判斷屬性上是否有註解
            ReflectionUtils.doWithLocalFields(targetClass, field -> {
                if (webServiceRefClass != null && field.isAnnotationPresent(webServiceRefClass)) {
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalStateException("@WebServiceRef annotation is not supported on static fields");
                    }
                    currElements.add(new WebServiceRefElement(field, field, null));
                }
                else if (ejbRefClass != null && field.isAnnotationPresent(ejbRefClass)) {
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalStateException("@EJB annotation is not supported on static fields");
                    }
                    currElements.add(new EjbRefElement(field, field, null));
                }
                else if (field.isAnnotationPresent(Resource.class)) {
                    if (Modifier.isStatic(field.getModifiers())) {
                        throw new IllegalStateException("@Resource annotation is not supported on static fields");
                    }
                    if (!this.ignoredResourceTypes.contains(field.getType().getName())) {
                        currElements.add(new ResourceElement(field, field, null));
                    }
                }
            });
             //判斷方法上是否有註解
            ReflectionUtils.doWithLocalMethods(targetClass, method -> {
                Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
                if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                    return;
                }
                if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                    if (webServiceRefClass != null && bridgedMethod.isAnnotationPresent(webServiceRefClass)) {
                        if (Modifier.isStatic(method.getModifiers())) {
                            throw new IllegalStateException("@WebServiceRef annotation is not supported on static methods");
                        }
                        if (method.getParameterCount() != 1) {
                            throw new IllegalStateException("@WebServiceRef annotation requires a single-arg method: " + method);
                        }
                        PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                        currElements.add(new WebServiceRefElement(method, bridgedMethod, pd));
                    }
                    else if (ejbRefClass != null && bridgedMethod.isAnnotationPresent(ejbRefClass)) {
                        if (Modifier.isStatic(method.getModifiers())) {
                            throw new IllegalStateException("@EJB annotation is not supported on static methods");
                        }
                        if (method.getParameterCount() != 1) {
                            throw new IllegalStateException("@EJB annotation requires a single-arg method: " + method);
                        }
                        PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                        currElements.add(new EjbRefElement(method, bridgedMethod, pd));
                    }
                    else if (bridgedMethod.isAnnotationPresent(Resource.class)) {
                        if (Modifier.isStatic(method.getModifiers())) {
                            throw new IllegalStateException("@Resource annotation is not supported on static methods");
                        }
                        Class<?>[] paramTypes = method.getParameterTypes();
                        if (paramTypes.length != 1) {
                            throw new IllegalStateException("@Resource annotation requires a single-arg method: " + method);
                        }
                        if (!this.ignoredResourceTypes.contains(paramTypes[0].getName())) {
                            PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                            currElements.add(new ResourceElement(method, bridgedMethod, pd));
                        }
                    }
                }
            });

            elements.addAll(0, currElements);
            targetClass = targetClass.getSuperclass();
        }
        while (targetClass != null && targetClass != Object.class);

        return InjectionMetadata.forElements(elements, clazz);
    }

此方法較長,不過主要完成了兩件事,分別從屬性和方法上判斷是否有@Resource、@WebServiceRef、@EJB註解,如果存在則放入injectionMetadataCache中。從這裡還可以看出這三個註解可以用在方法和屬性上。

到這裡CommonAnnoatationBeanPostProcessor類的postProcssMergedBeanDefinition方法已經分析完畢,其作用(不包含其父類的作用)分別從解析類中的@Resource、@WebServiceRef、@EJB三個註解,並放入injectionMetadataCache快取中,以便postProcessProperties方法使用。

三、使用場景

從上面的分析中已經知道了CommonAnnoatationBeanProcessor的postProcessMergedBeanDefinition方法的作用。就是解析@Resource、@WebServiceRef、@EJB三個註解並快取元數據資訊。下面會分析如何使用快取在injectionMetadataCache中的資訊,也就是postProcessPerties方法的邏輯。

 

原創不易,有不正之處歡迎指正。