【Spring 源码】AOP 的加载原理(一)
文章目录
一、创建并获取 AOP
1. 初始化 Bean 并且调用前后置方法
主要代码:AbstractAutowireCapableBeanFactory#initializeBean()
在依赖注入的过程中,注入的过程结束后会调用 Bean 在配置文件中的 init-method
中注册的初始化方法,在这个过程中会调用前后置处理方法进行初始方法的前后置处理。
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 {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
// init-method 的前置方法
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
// 调用 init-method 配置的方法
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException((mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex);
}
// init-method 的后置方法
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization