在此记录下,以便后面用到,再者,如果需要还可以继续更新此文。有些细节步骤被我省略了,建议读者自己去读源码。
getProxy()方法,依据我们对Proxy属性的设置,生成JdkDynamicAopProxy或者CglibAopProxy,我们这里分析JdkDynamicAopProxy的场景。原图在Gtihub上。
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor
org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor
org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor
ProxyFactoryBean不同于ProxyFactory,ProxyFactoryBean是个FactoryBean(如果是熟悉这个interface接口的同学看到这个应该能想到点什么了吧,这里),它有个getObject()方法,Spring IOC在容器中由BeanDefinition生成bean对象时,会调用此方法,将getObject()的返回值作为bean对象。所以我们从getObjects()开始分析起。
这里我们分析JdkDynamicAopProxy时候的场景。原图在Github上。
for (String name : this.interceptorNames) {
if (logger.isTraceEnabled()) {
logger.trace("Configuring advisor or advice '" + name + "'");
}
if (name.endsWith(GLOBAL_SUFFIX)) {
if (!(this.beanFactory instanceof ListableBeanFactory)) {
throw new AopConfigException(
"Can only use global advisors or interceptors with a ListableBeanFactory");
}
addGlobalAdvisor((ListableBeanFactory) this.beanFactory,
name.substring(0, name.length() - GLOBAL_SUFFIX.length()));
} else {
Object advice;
if (this.singleton || this.beanFactory.isSingleton(name)) {
//name是bean的Id,所以从beanFactory中直接getBean()即可。
advice = this.beanFactory.getBean(name);
} else {
// It's a prototype Advice or Advisor: replace with a prototype.
// Avoid unnecessary creation of prototype bean just for advisor chain initialization.
advice = new PrototypePlaceholderAdvisor(name);
}
//将得到的advice加入到拦截器链中
addAdvisorOnChainCreation(advice, name);
}
}
还有很多关键点没有写出来,我个人觉得还是自己去看源码比较好,有些地方很难用有限的语言清晰的描述出来。
(adsbygoogle = window.adsbygoogle || []).push({});