• springboot 源码赏析 # spring后置处理器 beanPostProcessor


     1.

    /**
         * Initialize the given bean instance, applying factory callbacks
         * as well as init methods and bean post processors.
         * <p>Called from {@link #createBean} for traditionally defined beans,
         * and from {@link #initializeBean} for existing bean instances.
         * @param beanName the bean name in the factory (for debugging purposes)
         * @param bean the new bean instance we may need to initialize
         * @param mbd the bean definition that the bean was created with
         * (can also be {@code null}, if given an existing bean instance)
         * @return the initialized bean instance (potentially wrapped)
         * @see BeanNameAware
         * @see BeanClassLoaderAware
         * @see BeanFactoryAware
         * @see #applyBeanPostProcessorsBeforeInitialization
         * @see #invokeInitMethods
         * @see #applyBeanPostProcessorsAfterInitialization
         */
        protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                    invokeAwareMethods(beanName, bean);
                    return null;
                }, getAccessControlContext());
            }
            else {
                invokeAwareMethods(beanName, bean);
            }
    
            Object wrappedBean = bean;
            if (mbd == null || !mbd.isSynthetic()) {
                wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
            }
    
            try {
            //#1 先去执行 实现了 InitializingBean 接口的方法 invokeInitMethods(beanName, wrappedBean, mbd); }
    catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) {
            //#2. 调用 bean 的后置处理器的 after方法 wrappedBean
    = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }

     

    2.

    /**
         * Give a bean a chance to react now all its properties are set,
         * and a chance to know about its owning bean factory (this object).
         * This means checking whether the bean implements InitializingBean or defines
         * a custom init method, and invoking the necessary callback(s) if it does.
         * @param beanName the bean name in the factory (for debugging purposes)
         * @param bean the new bean instance we may need to initialize
         * @param mbd the merged bean definition that the bean was created with
         * (can also be {@code null}, if given an existing bean instance)
         * @throws Throwable if thrown by init methods or by the invocation process
         * @see #invokeCustomInitMethod
         */
        protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
                throws Throwable {
    
            boolean isInitializingBean = (bean instanceof InitializingBean);
            if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
                }
                if (System.getSecurityManager() != null) {
                    try {
                        AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
                            ((InitializingBean) bean).afterPropertiesSet();
                            return null;
                        }, getAccessControlContext());
                    }
                    catch (PrivilegedActionException pae) {
                        throw pae.getException();
                    }
                }
                else {
                    ((InitializingBean) bean).afterPropertiesSet();
                }
            }
    
            if (mbd != null && bean.getClass() != NullBean.class) {
                String initMethodName = mbd.getInitMethodName();
                if (StringUtils.hasLength(initMethodName) &&
                        !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                        !mbd.isExternallyManagedInitMethod(initMethodName)) {
    // 调用客户自定义的 方法 invokeCustomInitMethod(beanName, bean, mbd); } } }

    第一步: 构造方法 Bean后置处理器的 before, InitializingBean   init-method   Bean的后置处理器的after方法

    createBeanInstance

      applyBeanPostProcessorsBeforeInitialization

        InitializingBean.afterPropertieSet 方法

                          init-method 

            applyBeanPostProcessorsAfterInitialization

  • 相关阅读:
    AJAX中所谓的异步
    前端性能优化方案
    文字超出隐藏
    创建值的两种方式及其区别
    单例模式
    自定义数据属性
    时间字符串的处理
    日期函数及时钟案例
    很low的四位验证码实现
    使用Ajax发送http请求(get&post请求)--转载
  • 原文地址:https://www.cnblogs.com/lyr-2000/p/13848700.html
Copyright © 2020-2023  润新知