• 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

  • 相关阅读:
    mysql数据库汉字首字母简拼全拼
    window.showModalDialog刷新父窗口和本窗口的方法及注意
    c#.net语句e.Row.RowType == DataControlRowType.DataRow是什么含义?
    SQL 拿到一天内的数据
    在线脚本编辑器
    输入正确的邮箱地址
    jquery中的$(document).ready()方法和window.onload方法区别
    转载从XML文件中读取数据绑定到DropDownList
    GridView中DropDownList联动
    For 循环 和Foreach 的区别
  • 原文地址:https://www.cnblogs.com/lyr-2000/p/13848700.html
Copyright © 2020-2023  润新知