当spring完成属性注入之后, 就要开始 bean 的初始化了
代码片段:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
// 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); } }
进去看里面的内容:
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction
这里在初始化之前和初始化之后, 都调用了 Bean 的后置处理器响应的处理方法.
invokeAwareMethods
这个方法, 主要是处理几个 Aware 的. 分别调用他们定义的接口方法, 实现一些属性的注入工作
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
初始化之前的后置处理
https://www.cnblogs.com/elvinle/p/13385304.html
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
applyBeanPostProcessorsAfterInitialization
初始化之后的后置处理
https://www.cnblogs.com/elvinle/p/13385317.html