Spring中Bean的实例化与DI的过程 (3)

PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
if (hasInstAwareBpps) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
if (pvs == null) {
return;
}
}
}
}

spring中默认注册了以下几种beanPostProcessor,如下: ![beanPostProcessor](https://user-gold-cdn.xitu.io/2019/6/22/16b7cfcd1669ee2a?w=695&h=426&f=png&s=32220) 每一个beanPostProcessor都会在实例化前后相应的地方进行调用。在这里我们需要关注的是```AutowiredAnnotationBeanPostProcessor```,```RequiredAnnotationBeanPostProcessor```,```CommonAnnotationBeanPostProcessor```这几个注解。 顾名思义这几个beanPostProcessor都是用来处理注解的,对应用来处理```@Autowired```,```@Required```,```@Common```几个注解。 这里以```@Autowored```的处理为例。 #### AutowiredAnnotationBeanPostProcessor public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass()); try { metadata.inject(bean, beanName, pvs); }catch (Throwable ex) { throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex); } return pvs; } 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()) { AnnotationAttributes annotation = findAutowiredAnnotation(field); if (annotation != null) { if (Modifier.isStatic(field.getModifiers())) { if (logger.isWarnEnabled()) { logger.warn("Autowired annotation is not supported on static fields: " + field); } continue; } boolean required = determineRequiredStatus(annotation); currElements.add(new AutowiredFieldElement(field, required)); } } for (Method method : targetClass.getDeclaredMethods()) { Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { continue; } 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); } continue; } if (method.getParameterTypes().length == 0) { if (logger.isWarnEnabled()) { logger.warn("Autowired annotation should be used on methods with actual parameters: " + method); } } boolean required = determineRequiredStatus(ann); PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); currElements.add(new AutowiredMethodElement(method, required, pd)); } } elements.addAll(0, currElements); targetClass = targetClass.getSuperclass(); } while (targetClass != null && targetClass != Object.class); return new InjectionMetadata(clazz, elements); } 该方法主要就是通过解析当前bean有哪些属性被```@Autowired```注解修饰,将其封装为一个```InjectionMetadata```对象。解析的过程通过```findAutowiringMetadata(String beanName, Class<?> clazz)```完成,但实际上主要还是通过上面的```buildAutowiringMetadata(Class<?> clazz)```完成。 方法比较简单,首先就是遍历bean的所有字段(Field),通过反射的方法查找字段上的注解,如果存在匹配的注解(```AutowiredAnnotationBeanPostProcessor```匹配的就是```@Autowired```)。如果存在该注解就将这个字段记录表示需要进行注入。 当将字段遍历完成后就取出所有的方法,查找方法上是否有该注解,如果存在的话同理记录下该方法。 对于被记录下的字段或者方法会被封装成一个```InjectedElement```对象。通过该对象的inject方法完成注入工作。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyyfsj.html