AOP是基于IOC的Bean加载来实现的,将切面类的所有切面方法根据使用的注解生成对应的Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor,为后续交给代理增强实现做准备
这里我们可以很明确的知道,AOP也是在Bean容器中被Spring管理的,根据初始化过程打断点定位到如下:
@Nullable
public BeanDefinition parseCustomElement(Element ele) {
return this.parseCustomElement(ele, (BeanDefinition)null);
}
@Nullable
public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
String namespaceUri = this.getNamespaceURI(ele);
if (namespaceUri == null) {
return null;
} else {
//这里的handler就是AopNamespaceHandler,也就是aop-autoproxy对应的handler
NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
if (handler == null) {
this.error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
return null;
} else {
return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}
}
}
aop配置标签的解析
AopNamespaceHandler可以理解为注册BeanDefinition的解析器BeanDefinitionParser,将aop:xxx配置标签交给指定的parser来处理
public class AopNamespaceHandler extends NamespaceHandlerSupport {
public AopNamespaceHandler() {
}
public void init() {
//注册解析<aop:config>配置
this.registerBeanDefinitionParser("config", new ConfigBeanDefinitionParser());
//注册解析<aop:aspectj-autoproxy>配置
this.registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
//注册解析<aop:scoped-proxy>配置
this.registerBeanDefinitionDecorator("scoped-proxy", new ScopedProxyBeanDefinitionDecorator());
//注册解析<aop:spring-configured>配置
this.registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
}
}
config配置标签的解析
aop:config/由ConfigBeanDefinitionParser这个类处理,作为parser类最重要的就是parse方法
<aop:config>
<aop:aspect ref="logAspect">
<aop:pointcut id = pointCutMethod> expression="xxxxxxx"/>
</aop:aspect>
</aop:config>
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
parserContext.pushContainingComponent(compositeDef);
this.configureAutoProxyCreator(parserContext, element);
List<Element> childElts = DomUtils.getChildElements(element);
Iterator var5 = childElts.iterator();
while(var5.hasNext()) {
Element elt = (Element)var5.next();
String localName = parserContext.getDelegate().getLocalName(elt);
//如果<aop:config>的下级节点标签为<aop:pointcut>则执行parsePointcut(elt,parserContext)方法
if ("pointcut".equals(localName)) {
this.parsePointcut(elt, parserContext);
//如果<aop:config>的下级节点标签为<aop:advisor>则执行parseAdvisor(elt,parserContext)方法
} else if ("advisor".equals(localName)) {
this.parseAdvisor(elt, parserContext);
//如果<aop:config>的下级节点标签为<aop:advisor>则执行parseAspect(elt,parserContext)方法
} else if ("aspect".equals(localName)) {
this.parseAspect(elt, parserContext);//一步步去解析元素,不做过多代码的解析
}
}
parserContext.popAndRegisterContainingComponent();
return null;
}
aspectj-autoproxy配置标签的解析
<aop:aspectj-autoproxy/>
则由AspectJAutoProxyBeanDefinitionParser这个类处理的,我们看下parse 方法
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
//注册AspectJAnnotationAutoProxyCreator
AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);
//扩展BeanDefinition
this.extendBeanDefinition(element, parserContext);
return null;
}
public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary(ParserContext parserContext, Element sourceElement) {
//核心业务处理
BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext.getRegistry(), parserContext.extractSource(sourceElement));
//代理类
useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
//注册组件
registerComponentIfNecessary(beanDefinition, parserContext);
}
@Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, @Nullable Object source) {
return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
}
一层层解析,最终发现registerOrEscalateApcAsRequired方法是依赖于AnnotationAwareAspectJAutoProxyCreator类来完成的
AnnotationAwareAspectJAutoProxyCreator(注解切面代理创建类)
AnnotationAwareAspectJAutoProxyCreator实现了两类接口
-
BeanFactoryAware属于Bean级生命周期接口方法
-
InstantiationBeanPostProcessor和BeanPostProcessor这两个接口实现,一般称它们的实现类为后处理器,是容器级生命周期接口方法
具体参考:Bean的生命周期
根据Bean的生命周期,我们得知,AOP主要的初始化方法就是postProcessBeforeInstantiation和postProcessAfterInitialization中
如下图中红框所示:
postProcessBeforeInstantiation
解析AOP基础类的流程
AbstractAutoProxyCreator
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
Object cacheKey = this.getCacheKey(beanClass, beanName);
if (!StringUtils.hasLength</
标签:parserContext,element,源码,切面,AOP,解析,public,elt,BeanDefinition
From: https://blog.csdn.net/qq_39052339/article/details/139749170