spring AOP何时添加AnnotationAwareAspectJAutoProxyCreator

1.spring AOP的原理都知道是从解析自定义标签时注册一个beanDefiniton到beanFactory,然后在bean加载过程中做动态代理(initializeBean时会做一些最后的初始化,包括aware接口的调用,applyBeanPostProcessorsBefore/AfterXXX,initMethod,InitializingBean接口的调用)

2.但是真正添加AnnotationAwareAspectJAutoProxyCreator是在 ClassPathXmlApplicationContext("..")初始化时refresh()方法中的registerBeanPostProcessors()添加.

3.创建AOP代理时Spring会判断当前bean是否适合做代理:(if(!earlyProxyReferences.contains(cacheKey))),也就是不能和earlyProxyReferences冲突,确保代理只有一个。

4.我是学物理的,为了方便理解,我习惯把AOP叫成“代码短路”设计,spring中有很多针对特殊灵活的配置和场景设计了“短路”的代码。

5.spring aop的增强器采用的是链式设计:具体可以看JdkDynamicAopProxy中invoke方法

关键代码:
registerBeanPostProcessors(beanFactory);



public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();

// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);

try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.
initMessageSource();

// Initialize event multicaster for this context.
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
onRefresh();

// Check for listener beans and register them.
registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
finishRefresh();
}

catch (BeansException ex) {
logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);

// Destroy already created singletons to avoid dangling resources.
destroyBeans();

// Reset 'active' flag.
cancelRefresh(ex);

// Propagate exception to caller.
throw ex;
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值