doCreateBean - initializeBean

當spring完成屬性注入以後, 就要開始 bean 的初始化了html

代碼片斷:spring

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBeanapp

// Initialize the bean instance.
Object exposedObject = bean; try { //設置屬性,很是重要
 populateBean(beanName, mbd, instanceWrapper); //執行後置處理器,一部分aop就是在這裏完成的處理
    exposedObject = initializeBean(beanName, exposedObject, mbd); } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } }

進去看裏面的內容:this

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareMethods(beanName, bean); return null; }, getAccessControlContext()); } else { //調用 BeanNameAware BeanClassLoaderAware BeanFactoryAware 定義的方法
 invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { //初始化以前, 執行後置處理的 before方法
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { //執行bean的聲明週期回調中的init方法
 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()) { //初始化以後, 執行後置處理器的 after方法
        wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }

這裏在初始化以前和初始化以後, 都調用了 Bean 的後置處理器響應的處理方法.url

 

invokeAwareMethods

這個方法, 主要是處理幾個 Aware 的. 分別調用他們定義的接口方法, 實現一些屬性的注入工做spa

private void invokeAwareMethods(final String beanName, final Object bean) { if (bean instanceof Aware) { if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } if (bean instanceof BeanClassLoaderAware) { ClassLoader bcl = getBeanClassLoader(); if (bcl != null) { ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl); } } if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this); } } }

 

applyBeanPostProcessorsBeforeInitialization

初始化以前的後置處理.net

 https://www.cnblogs.com/elvinle/p/13385304.htmlcode

invokeInitMethods

protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); //這裏主要處理實現了 InitializingBean 接口的類, 調用其 afterPropertiesSet(), 進行初始化工做
    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(); } } //調用用戶自定義的初始化方法, 如 xml 中配置的 init-method 方法
    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); } } }

 

applyBeanPostProcessorsAfterInitialization

初始化以後的後置處理xml

 https://www.cnblogs.com/elvinle/p/13385317.htmlhtm

相關文章
相關標籤/搜索