CommonAnnotationBeanPostProcessor 通用注解分析

330 阅读9分钟

Chapter 4 CommonAnnotationBeanPostProcessor 通用注解分析

1 CommonAnnotationBeanPostProcessor 能处理的注解类型

static {
    // WebServiceRef 注解
    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;
    }
​
    // EJB 注解
    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) {
        resourceAnnotationTypes.add(webServiceRefClass);
    }
    if (ejbRefClass != null) {
        resourceAnnotationTypes.add(ejbRefClass);
    }
}

Source Path

2 InstantiationAwareBeanPostProcessorAdapter 适配器说明

AutowiredAnnotationBeanPostProcessor 继承了 InstantiationAwareBeanPostProcessorAdapter 这个适配器,该适配器实现了 SmartInstantiationAwareBeanPostProcessor 的所有方法,但没有进行其他操作,这样 AutowiredAnnotationBeanPostProcessor 只要重写需要的方法就行,不必将所有方法一一实现。CommonAnnotatio

public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
public abstract class InstantiationAwareBeanPostProcessorAdapter implements SmartInstantiationAwareBeanPostProcessor {
​
    @Override
    @Nullable
    public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }
​
    @Override
    @Nullable
    public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }
​
    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        return bean;
    }
​
    @Override
    @Nullable
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }
​
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return true;
    }
​
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
            throws BeansException {
​
        return null;
    }
​
    @Deprecated
    @Override
    public PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
​
        return pvs;
    }
​
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
​
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
​
}
​
​
​

Source Path

3 InstantiationAwareBeanPostProcessor 接口说明

CommonAnnotationBeanPostProcessor 实现了 InstantiationAwareBeanPostProcessor 接口,由于 java8 以后,接口可以由默认实现。该方式优于 InstantiationAwareBeanPostProcessorAdapter 这种适配器模式

public class CommonAnnotationBeanPostProcessor implements InstantiationAwareBeanPostProcessor;
​
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
​
    /**
     * Apply this BeanPostProcessor <i>before the target bean gets instantiated</i>.
     * The returned bean object may be a proxy to use instead of the target bean,
     * effectively suppressing default instantiation of the target bean.
     * <p>If a non-null object is returned by this method, the bean creation process
     * will be short-circuited. The only further processing applied is the
     * {@link #postProcessAfterInitialization} callback from the configured
     * {@link BeanPostProcessor BeanPostProcessors}.
     * <p>This callback will be applied to bean definitions with their bean class,
     * as well as to factory-method definitions in which case the returned bean type
     * will be passed in here.
     * <p>Post-processors may implement the extended
     * {@link SmartInstantiationAwareBeanPostProcessor} interface in order
     * to predict the type of the bean object that they are going to return here.
     * <p>The default implementation returns {@code null}.
     * @param beanClass the class of the bean to be instantiated
     * @param beanName the name of the bean
     * @return the bean object to expose instead of a default instance of the target bean,
     * or {@code null} to proceed with default instantiation
     * @throws org.springframework.beans.BeansException in case of errors
     * @see #postProcessAfterInstantiation
     * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getBeanClass()
     * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getFactoryMethodName()
     */
    @Nullable
    default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }
​
    /**
     * Perform operations after the bean has been instantiated, via a constructor or factory method,
     * but before Spring property population (from explicit properties or autowiring) occurs.
     * <p>This is the ideal callback for performing custom field injection on the given bean
     * instance, right before Spring's autowiring kicks in.
     * <p>The default implementation returns {@code true}.
     * @param bean the bean instance created, with properties not having been set yet
     * @param beanName the name of the bean
     * @return {@code true} if properties should be set on the bean; {@code false}
     * if property population should be skipped. Normal implementations should return {@code true}.
     * Returning {@code false} will also prevent any subsequent InstantiationAwareBeanPostProcessor
     * instances being invoked on this bean instance.
     * @throws org.springframework.beans.BeansException in case of errors
     * @see #postProcessBeforeInstantiation
     */
    default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return true;
    }
​
    /**
     * Post-process the given property values before the factory applies them
     * to the given bean, without any need for property descriptors.
     * <p>Implementations should return {@code null} (the default) if they provide a custom
     * {@link #postProcessPropertyValues} implementation, and {@code pvs} otherwise.
     * In a future version of this interface (with {@link #postProcessPropertyValues} removed),
     * the default implementation will return the given {@code pvs} as-is directly.
     * @param pvs the property values that the factory is about to apply (never {@code null})
     * @param bean the bean instance created, but whose properties have not yet been set
     * @param beanName the name of the bean
     * @return the actual property values to apply to the given bean (can be the passed-in
     * PropertyValues instance), or {@code null} which proceeds with the existing properties
     * but specifically continues with a call to {@link #postProcessPropertyValues}
     * (requiring initialized {@code PropertyDescriptor}s for the current bean class)
     * @throws org.springframework.beans.BeansException in case of errors
     * @since 5.1
     * @see #postProcessPropertyValues
     */
    @Nullable
    default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
            throws BeansException {
​
        return null;
    }
​
    /**
     * Post-process the given property values before the factory applies them
     * to the given bean. Allows for checking whether all dependencies have been
     * satisfied, for example based on a "Required" annotation on bean property setters.
     * <p>Also allows for replacing the property values to apply, typically through
     * creating a new MutablePropertyValues instance based on the original PropertyValues,
     * adding or removing specific values.
     * <p>The default implementation returns the given {@code pvs} as-is.
     * @param pvs the property values that the factory is about to apply (never {@code null})
     * @param pds the relevant property descriptors for the target bean (with ignored
     * dependency types - which the factory handles specifically - already filtered out)
     * @param bean the bean instance created, but whose properties have not yet been set
     * @param beanName the name of the bean
     * @return the actual property values to apply to the given bean (can be the passed-in
     * PropertyValues instance), or {@code null} to skip property population
     * @throws org.springframework.beans.BeansException in case of errors
     * @see #postProcessProperties
     * @see org.springframework.beans.MutablePropertyValues
     * @deprecated as of 5.1, in favor of {@link #postProcessProperties(PropertyValues, Object, String)}
     */
    @Deprecated
    @Nullable
    default PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
​
        return pvs;
    }
​
}
​

Source Path

4 CommonAnnotationBeanPostProcessor#postProcessMergedBeanDefinition

这里和 AutowiredAnnotationBeanPostProcessor 的区别在于查找的元数据不一样,调用的为 findResourceMetadata 方法

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

Source Path

5 CommonAnnotationBeanPostProcessor#findResourceMetadata

这里和 AutowiredAnnotationBeanPostProcessor 的区别在于构建缓存数据这里 buildResourceMetadata

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 = buildResourceMetadata(clazz);
                this.injectionMetadataCache.put(cacheKey, metadata);
            }
        }
    }
    return metadata;
}

Source Path

6 CommonAnnotationBeanPostProcessor#buildResourceMetadata

该方法在构建元数据时,逻辑基本和 AutowiredAnnotationBeanPostProcessor#buildAutowiringMetadata 方法一致,不同点在于 CommonAnnotationBeanPostProcessor 在处理时判断的类型为:

  • webServiceRefClass
  • ejbRefClass
  • Resource 这几个类型
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 -> {
            // 如果是 webServiceRefClass 则添加找到的元素
            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));
            }
            // 如果是 ejbRefClass 则添加找到的元素
            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));
            }
            // 如果是 Resource 则添加找到的元素
            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);
}

Source Path

7 CommonAnnotationBeanPostProcessor#postProcessProperties

populateBean 填充实例属性时调用该方法

@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;
}

Source Path

8 InjectionMetadata#inject

public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
    Collection<InjectedElement> checkedElements = this.checkedElements;
    Collection<InjectedElement> elementsToIterate =
            (checkedElements != null ? checkedElements : this.injectedElements);
    if (!elementsToIterate.isEmpty()) {
        for (InjectedElement element : elementsToIterate) {
            if (logger.isTraceEnabled()) {
                logger.trace("Processing injected element of bean '" + beanName + "': " + element);
            }
            // 进行注入
            element.inject(target, beanName, pvs);
        }
    }
}

Source Path

9 InjectionMetadata

getResourceToInject 是一个模板方法,具体的实现由 EjbRefElement、ResourceElement、WebServiceRefElement 三个内部类实现

/**
 * Either this or {@link #getResourceToInject} needs to be overridden.
 */
protected void inject(Object target, @Nullable String requestingBeanName, @Nullable PropertyValues pvs)
        throws Throwable {
​
    if (this.isField) {
        // 反射设置字段的值
        Field field = (Field) this.member;
        ReflectionUtils.makeAccessible(field);
        field.set(target, getResourceToInject(target, requestingBeanName));
    }
    else {
        if (checkPropertySkipping(pvs)) {
            return;
        }
        try {
            // 反射执行方法
            Method method = (Method) this.member;
            ReflectionUtils.makeAccessible(method);
            method.invoke(target, getResourceToInject(target, requestingBeanName));
        }
        catch (InvocationTargetException ex) {
            throw ex.getTargetException();
        }
    }
}

Source Path

10 InitDestroyAnnotationBeanPostProcessor 生命周期处理

CommonAnnotationBeanPostProcessor 继承自 InitDestroyAnnotationBeanPostProcessor 类,该类主要为生命周期相关的处理。

InitDestroyAnnotationBeanPostProcessor 该类也实现了 MergedBeanDefinitionPostProcessor,扩展了 postProcessMergedBeanDefinition 将类和父类合并的扩展方法

public class InitDestroyAnnotationBeanPostProcessor
implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, PriorityOrdered, Serializable {
​
    @Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
        // 查找元数据(包含生命周期)
        LifecycleMetadata metadata = findLifecycleMetadata(beanType);
        metadata.checkConfigMembers(beanDefinition);
    }
}

Source Path

11 findLifecycleMetadata

缓存方法,缓存的数据为生命周期元数据

private LifecycleMetadata findLifecycleMetadata(Class<?> clazz) {
    if (this.lifecycleMetadataCache == null) {
        // Happens after deserialization, during destruction...
        return buildLifecycleMetadata(clazz);
    }
    // Quick check on the concurrent map first, with minimal locking.
    LifecycleMetadata metadata = this.lifecycleMetadataCache.get(clazz);
    if (metadata == null) {
        synchronized (this.lifecycleMetadataCache) {
            metadata = this.lifecycleMetadataCache.get(clazz);
            if (metadata == null) {
                metadata = buildLifecycleMetadata(clazz);
                this.lifecycleMetadataCache.put(clazz, metadata);
            }
            return metadata;
        }
    }
    return metadata;
}

Source Path

12 buildLifecycleMetadata

构建生命周期元数据

private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
    // 是否为候选者
    if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {
        return this.emptyLifecycleMetadata;
    }
    
    // 初始化方法集合
    List<LifecycleElement> initMethods = new ArrayList<>();
    // 销毁方法集合
    List<LifecycleElement> destroyMethods = new ArrayList<>();
    // 需要解析的目标类
    Class<?> targetClass = clazz;
​
    do {
        // 当前类上的初始化方法集合
        final List<LifecycleElement> currInitMethods = new ArrayList<>();
        // 当前类上的销毁方法集合
        final List<LifecycleElement> currDestroyMethods = new ArrayList<>();
​
        // 遍历目标类及其父类
        ReflectionUtils.doWithLocalMethods(targetClass, method -> {
            // 判断目标类上是否有标识了初始化方法的注解
            if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
                // 构建生命走呼气元素
                LifecycleElement element = new LifecycleElement(method);
                // 添加到初始化方法集合中
                currInitMethods.add(element);
                if (logger.isTraceEnabled()) {
                    logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
                }
            }
            // 判断类的方法上是否有销毁的注解,如果存在,则添加到销毁方法集合中
            if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
                currDestroyMethods.add(new LifecycleElement(method));
                if (logger.isTraceEnabled()) {
                    logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);
                }
            }
        });
        // 将当前类查找到的存入总的集合中
        initMethods.addAll(0, currInitMethods);
        destroyMethods.addAll(currDestroyMethods);
        // 递归父类
        targetClass = targetClass.getSuperclass();
    }
    // 父类不存在
    while (targetClass != null && targetClass != Object.class);
​
    // 如果都是空的,则返回一个空的生命周期元数据否则返回新建一个包含初始化方法和销毁方法的生源周期元数据
    return (initMethods.isEmpty() && destroyMethods.isEmpty() ? this.emptyLifecycleMetadata :
            new LifecycleMetadata(clazz, initMethods, destroyMethods));
}

Source Path

13 CommonAnnotationBeanPostProcessor 构造设置需要找的元数据注解

public CommonAnnotationBeanPostProcessor() {
        setOrder(Ordered.LOWEST_PRECEDENCE - 3);
        setInitAnnotationType(PostConstruct.class);
        setDestroyAnnotationType(PreDestroy.class);
        ignoreResourceType("javax.xml.ws.WebServiceContext");
    }

Source Path

14 LifecycleMetadata

包含生命周期的元数据

/**
 * Class representing information about annotated init and destroy methods.
 */
private class LifecycleMetadata {
​
    private final Class<?> targetClass;
​
    // 该字段是一个集合,存放一个类的多个初始化方法或者类以及父类的初始化方法
    private final Collection<LifecycleElement> initMethods;
​
    // 该字段是一个集合,存放一个类的多个销毁方法或者类以及父类的销毁方法
    private final Collection<LifecycleElement> destroyMethods;
​
    @Nullable
    private volatile Set<LifecycleElement> checkedInitMethods;
​
    @Nullable
    private volatile Set<LifecycleElement> checkedDestroyMethods;
​
    public LifecycleMetadata(Class<?> targetClass, Collection<LifecycleElement> initMethods,
            Collection<LifecycleElement> destroyMethods) {
​
        this.targetClass = targetClass;
        this.initMethods = initMethods;
        this.destroyMethods = destroyMethods;
    }
​
    public void checkConfigMembers(RootBeanDefinition beanDefinition) {
        Set<LifecycleElement> checkedInitMethods = new LinkedHashSet<>(this.initMethods.size());
        for (LifecycleElement element : this.initMethods) {
            String methodIdentifier = element.getIdentifier();
            if (!beanDefinition.isExternallyManagedInitMethod(methodIdentifier)) {
                beanDefinition.registerExternallyManagedInitMethod(methodIdentifier);
                checkedInitMethods.add(element);
                if (logger.isTraceEnabled()) {
                    logger.trace("Registered init method on class [" + this.targetClass.getName() + "]: " + element);
                }
            }
        }
        Set<LifecycleElement> checkedDestroyMethods = new LinkedHashSet<>(this.destroyMethods.size());
        for (LifecycleElement element : this.destroyMethods) {
            String methodIdentifier = element.getIdentifier();
            if (!beanDefinition.isExternallyManagedDestroyMethod(methodIdentifier)) {
                beanDefinition.registerExternallyManagedDestroyMethod(methodIdentifier);
                checkedDestroyMethods.add(element);
                if (logger.isTraceEnabled()) {
                    logger.trace("Registered destroy method on class [" + this.targetClass.getName() + "]: " + element);
                }
            }
        }
        this.checkedInitMethods = checkedInitMethods;
        this.checkedDestroyMethods = checkedDestroyMethods;
    }
​
    public void invokeInitMethods(Object target, String beanName) throws Throwable {
        Collection<LifecycleElement> checkedInitMethods = this.checkedInitMethods;
        Collection<LifecycleElement> initMethodsToIterate =
                (checkedInitMethods != null ? checkedInitMethods : this.initMethods);
        if (!initMethodsToIterate.isEmpty()) {
            for (LifecycleElement element : initMethodsToIterate) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Invoking init method on bean '" + beanName + "': " + element.getMethod());
                }
                element.invoke(target);
            }
        }
    }
​
    public void invokeDestroyMethods(Object target, String beanName) throws Throwable {
        Collection<LifecycleElement> checkedDestroyMethods = this.checkedDestroyMethods;
        Collection<LifecycleElement> destroyMethodsToUse =
                (checkedDestroyMethods != null ? checkedDestroyMethods : this.destroyMethods);
        if (!destroyMethodsToUse.isEmpty()) {
            for (LifecycleElement element : destroyMethodsToUse) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Invoking destroy method on bean '" + beanName + "': " + element.getMethod());
                }
                element.invoke(target);
            }
        }
    }
​
    public boolean hasDestroyMethods() {
        Collection<LifecycleElement> checkedDestroyMethods = this.checkedDestroyMethods;
        Collection<LifecycleElement> destroyMethodsToUse =
                (checkedDestroyMethods != null ? checkedDestroyMethods : this.destroyMethods);
        return !destroyMethodsToUse.isEmpty();
    }
}

Source Path

15 InitDestroyAnnotationBeanPostProcessor#postProcessBeforeInitialization

该方法在初始化之前进行的扩展回调,会去执行已经缓存的初始化方法

@Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
        try {
            metadata.invokeInitMethods(bean, beanName);
        }
        catch (InvocationTargetException ex) {
            throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException());
        }
        catch (Throwable ex) {
            throw new BeanCreationException(beanName, "Failed to invoke init method", ex);
        }
        return bean;
    }

Source Path

16 InitDestroyAnnotationBeanPostProcessor#invokeInitMethods

遍历初始化方法, 再通过反射去执行

public void invokeInitMethods(Object target, String beanName) throws Throwable {
    Collection<LifecycleElement> checkedInitMethods = this.checkedInitMethods;
    Collection<LifecycleElement> initMethodsToIterate =
            (checkedInitMethods != null ? checkedInitMethods : this.initMethods);
    if (!initMethodsToIterate.isEmpty()) {
        for (LifecycleElement element : initMethodsToIterate) {
            if (logger.isTraceEnabled()) {
                logger.trace("Invoking init method on bean '" + beanName + "': " + element.getMethod());
            }
            element.invoke(target);
        }
    }
}
​
public void invoke(Object target) throws Throwable {
    ReflectionUtils.makeAccessible(this.method);
    this.method.invoke(target, (Object[]) null);
}

Source Path

17 InitDestroyAnnotationBeanPostProcessor#postProcessBeforeDestruction

对象销毁前执行的方法

@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
    LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
    try {
        metadata.invokeDestroyMethods(bean, beanName);
    }
    catch (InvocationTargetException ex) {
        String msg = "Destroy method on bean with name '" + beanName + "' threw an exception";
        if (logger.isDebugEnabled()) {
            logger.warn(msg, ex.getTargetException());
        }
        else {
            logger.warn(msg + ": " + ex.getTargetException());
        }
    }
    catch (Throwable ex) {
        logger.warn("Failed to invoke destroy method on bean with name '" + beanName + "'", ex);
    }
​
// 执行销毁方法
public void invokeDestroyMethods(Object target, String beanName) throws Throwable {
    Collection<LifecycleElement> checkedDestroyMethods = this.checkedDestroyMethods;
    Collection<LifecycleElement> destroyMethodsToUse =
            (checkedDestroyMethods != null ? checkedDestroyMethods : this.destroyMethods);
    if (!destroyMethodsToUse.isEmpty()) {
        for (LifecycleElement element : destroyMethodsToUse) {
            if (logger.isTraceEnabled()) {
                logger.trace("Invoking destroy method on bean '" + beanName + "': " + element.getMethod());
            }
            element.invoke(target);
        }
    }
}
​
// 反射执行
public void invoke(Object target) throws Throwable {
    ReflectionUtils.makeAccessible(this.method);
    this.method.invoke(target, (Object[]) null);
}

Source Path

18 PostProcessor 根据order的优先级决定执行顺序

CommonAnnotationBeanPostProcessor 在构造方法中设置 Order = Integer.MAX_VALUE - 3; AutowiredAnnotationBeanPostProcessor 中 private int Order = Integer.MAX_VALUE - 2; Order 值越大,优先级越低,所以在调试的时候,先执行 CommonAnnotationBeanPostProcessor 再执行 AutowiredAnnotationBeanPostProcessor;

int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
public CommonAnnotationBeanPostProcessor() {
    // LOWEST_PRECEDENCE 代表最小优先级 -3 = 倒数第三位执行
    setOrder(Ordered.LOWEST_PRECEDENCE - 3);
    setInitAnnotationType(PostConstruct.class);
    setDestroyAnnotationType(PreDestroy.class);
    ignoreResourceType("javax.xml.ws.WebServiceContext");
}
​
​
AutowiredAnnotationBeanPostProcessor
// Ordered.LOWEST_PRECEDENCE - 2 = 倒数第四位执行;
private int order = Ordered.*/ - 2;

Source Path