在上篇博客中分享了InstantiationAwareBeanPostProcessor接口中的四個方法,分別對其進行了詳細的介紹,在文末留下了一個問題,那就是postProcessProperties方法,說到此方法是用來進行屬性填充的,而且引出了CommonAnnotationBeanPostProcessor類。css
CommonAnnotationBeanPostProcessor類在spring中是一個極其重要的類,它負責解析@Resource、@WebServiceRef、@EJB三個註解,既然要解析這三個屬性,那麼在CommonAnnotationBeanPostProcessor類中確定要有這三個註解,下面看這段代碼,java
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中添加了三個註解,就是這個類要解析的註解所表明的接口。web
在CommonAnnoatationBeanPostProcessor類中還有一個重要的屬性即injectionMetaDataCache,這個屬性會存儲類中的被CommonAnnoatationBeanPostProcessor解析的字段或方法信息,也就是標註了三個屬性的字段或方法的元信息。spring
下面看CommonAnnoatationBeanPostProcessor類中的方法,緩存
上面是CommonAnnoatationBeanPostProcessor中比較重要的方法,有兩個比較熟悉的方法,在前面已經分析過即postProcessBeforeInstantiation和postProcessAfterInstantiation方法。app
@Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) { return null; }
上面是postProcessBeforeInstantiation方法,該方法的返回值爲null,即不會在bean實例化前產生一個代理對象。ide
@Override public boolean postProcessAfterInstantiation(Object bean, String beanName) { return true; }
上面是postProcessAfterInstantiation方法,該方法的返回值爲true,也就是說該類不會阻止屬性的注入。post
在CommonAnnoatationBeanPostProcessor中的postProcessProperties方法,ui
@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; }
先看下這個方法,今天這個方法不是主角。主角是下面這個方法,this
@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方法。
此方法是在何時調用的,從spring的源碼中能夠找到以下,在doCreateBean方法中,下面僅貼出部分代碼,
synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { //二、調用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方法的邏輯。
原創不易,有不正之處歡迎指正。