這一次搞懂Spring代理創(chuàng)建及AOP鏈?zhǔn)秸{(diào)用過(guò)程操作
前言
AOP,也就是面向切面編程,它可以將公共的代碼抽離出來(lái),動(dòng)態(tài)的織入到目標(biāo)類、目標(biāo)方法中,大大提高我們編程的效率,也使程序變得更加優(yōu)雅。如事務(wù)、操作日志等都可以使用AOP實(shí)現(xiàn)。這種織入可以是在運(yùn)行期動(dòng)態(tài)生成代理對(duì)象實(shí)現(xiàn),也可以在編譯期、類加載時(shí)期靜態(tài)織入到代碼中。而Spring正是通過(guò)第一種方法實(shí)現(xiàn),且在代理類的生成上也有兩種方式:JDK Proxy和CGLIB,默認(rèn)當(dāng)類實(shí)現(xiàn)了接口時(shí)使用前者,否則使用后者;另外Spring AOP只能實(shí)現(xiàn)對(duì)方法的增強(qiáng)。
正文
基本概念
AOP的術(shù)語(yǔ)很多,雖然不清楚術(shù)語(yǔ)我們也能很熟練地使用AOP,但是要理解分析源碼,術(shù)語(yǔ)就需要深刻體會(huì)其含義。
增強(qiáng)(Advice):就是我們想要額外增加的功能
目標(biāo)對(duì)象(Target):就是我們想要增強(qiáng)的目標(biāo)類,如果沒有AOP,我們需要在每個(gè)目標(biāo)對(duì)象中實(shí)現(xiàn)日志、事務(wù)管理等非業(yè)務(wù)邏輯
連接點(diǎn)(JoinPoint):程序執(zhí)行時(shí)的特定時(shí)機(jī),如方法執(zhí)行前、后以及拋出異常后等等。
切點(diǎn)(Pointcut):連接點(diǎn)的導(dǎo)航,我們?nèi)绾握业侥繕?biāo)對(duì)象呢?切點(diǎn)的作用就在于此,在Spring中就是匹配表達(dá)式。
引介(Introduction):引介是一種特殊的增強(qiáng),它為類添加一些屬性和方法。這樣,即使一個(gè)業(yè)務(wù)類原本沒有實(shí)現(xiàn)某個(gè)接口,通過(guò)AOP的引介功能,我們可以動(dòng)態(tài)地為該業(yè)務(wù)類添加接口的實(shí)現(xiàn)邏輯,讓業(yè)務(wù)類成為這個(gè)接口的實(shí)現(xiàn)類。
織入(Weaving):即如何將增強(qiáng)添加到目標(biāo)對(duì)象的連接點(diǎn)上,有動(dòng)態(tài)(運(yùn)行期生成代理)、靜態(tài)(編譯期、類加載時(shí)期)兩種方式。
代理(Proxy):目標(biāo)對(duì)象被織入增強(qiáng)后,就會(huì)產(chǎn)生一個(gè)代理對(duì)象,該對(duì)象可能是和原對(duì)象實(shí)現(xiàn)了同樣的一個(gè)接口(JDK),也可能是原對(duì)象的子類(CGLIB)。
切面(Aspect、Advisor):切面由切點(diǎn)和增強(qiáng)組成,包含了這兩者的定義。
代理對(duì)象的創(chuàng)建
在熟悉了AOP術(shù)語(yǔ)后,下面就來(lái)看看Spring是如何創(chuàng)建代理對(duì)象的,是否還記得上一篇提到的AOP的入口呢?在AbstractAutowireCapableBeanFactory類的applyBeanPostProcessorsAfterInitialization方法中循環(huán)調(diào)用了BeanPostProcessor的postProcessAfterInitialization方法,其中一個(gè)就是我們創(chuàng)建代理對(duì)象的入口。
這里是Bean實(shí)例化完成去創(chuàng)建代理對(duì)象,理所當(dāng)然應(yīng)該這樣,但實(shí)際上在Bean實(shí)例化之前調(diào)用了一個(gè)resolveBeforeInstantiation方法,這里實(shí)際上我們也是有機(jī)會(huì)可以提前創(chuàng)建代理對(duì)象的,這里放到最后來(lái)分析,先來(lái)看主入口,進(jìn)入到AbstractAutoProxyCreator類中:
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
//創(chuàng)建當(dāng)前bean的代理,如果這個(gè)bean有advice的話,重點(diǎn)看
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
//如果有切面,則生成該bean的代理
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
//把被代理對(duì)象bean實(shí)例封裝到SingletonTargetSource對(duì)象中
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
先從緩存中拿,沒有則調(diào)用wrapIfNecessary方法創(chuàng)建。在這個(gè)方法里面主要看兩個(gè)地方:getAdvicesAndAdvisorsForBean和createProxy。簡(jiǎn)單一句話概括就是先掃描后創(chuàng)建,問(wèn)題是掃描什么呢?你可以先結(jié)合上面的概念思考下,換你會(huì)怎么做。進(jìn)入到子類AbstractAdvisorAutoProxyCreator的getAdvicesAndAdvisorsForBean方法中:
protected Object[] getAdvicesAndAdvisorsForBean(
Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {
//找到合格的切面
List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
if (advisors.isEmpty()) {
return DO_NOT_PROXY;
}
return advisors.toArray();
}
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
//找到候選的切面,其實(shí)就是一個(gè)尋找有@Aspectj注解的過(guò)程,把工程中所有有這個(gè)注解的類封裝成Advisor返回
List<Advisor> candidateAdvisors = findCandidateAdvisors();
//判斷候選的切面是否作用在當(dāng)前beanClass上面,就是一個(gè)匹配過(guò)程?,F(xiàn)在就是一個(gè)匹配
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
extendAdvisors(eligibleAdvisors);
if (!eligibleAdvisors.isEmpty()) {
//對(duì)有@Order@Priority進(jìn)行排序
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
}
return eligibleAdvisors;
}
在findEligibleAdvisors方法中可以看到有兩個(gè)步驟,第一先找到所有的切面,即掃描所有帶有@Aspect注解的類,并將其中的切點(diǎn)(表達(dá)式)和增強(qiáng)封裝為切面,掃描完成后,自然是要判斷哪些切面能夠連接到當(dāng)前Bean實(shí)例上。下面一步步來(lái)分析,首先是掃描過(guò)程,進(jìn)入到AnnotationAwareAspectJAutoProxyCreator類中:
protected List<Advisor> findCandidateAdvisors() {
// 先通過(guò)父類AbstractAdvisorAutoProxyCreator掃描,這里不重要
List<Advisor> advisors = super.findCandidateAdvisors();
// 主要看這里
if (this.aspectJAdvisorsBuilder != null) {
advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
}
return advisors;
}
這里委托給了BeanFactoryAspectJAdvisorsBuilderAdapter類,并調(diào)用其父類的buildAspectJAdvisors方法創(chuàng)建切面對(duì)象:
public List<Advisor> buildAspectJAdvisors() {
List<String> aspectNames = this.aspectBeanNames;
if (aspectNames == null) {
synchronized (this) {
aspectNames = this.aspectBeanNames;
if (aspectNames == null) {
List<Advisor> advisors = new ArrayList<>();
aspectNames = new ArrayList<>();
//獲取spring容器中的所有bean的名稱BeanName
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
this.beanFactory, Object.class, true, false);
for (String beanName : beanNames) {
if (!isEligibleBean(beanName)) {
continue;
}
Class<?> beanType = this.beanFactory.getType(beanName);
if (beanType == null) {
continue;
}
//判斷類上是否有@Aspect注解
if (this.advisorFactory.isAspect(beanType)) {
aspectNames.add(beanName);
AspectMetadata amd = new AspectMetadata(beanType, beanName);
if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
// 當(dāng)@Aspect的value屬性為""時(shí)才會(huì)進(jìn)入到這里
// 創(chuàng)建獲取有@Aspect注解類的實(shí)例工廠,負(fù)責(zé)獲取有@Aspect注解類的實(shí)例
MetadataAwareAspectInstanceFactory factory =
new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName);
//創(chuàng)建切面advisor對(duì)象
List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory);
if (this.beanFactory.isSingleton(beanName)) {
this.advisorsCache.put(beanName, classAdvisors);
}
else {
this.aspectFactoryCache.put(beanName, factory);
}
advisors.addAll(classAdvisors);
}
else {
MetadataAwareAspectInstanceFactory factory =
new PrototypeAspectInstanceFactory(this.beanFactory, beanName);
this.aspectFactoryCache.put(beanName, factory);
advisors.addAll(this.advisorFactory.getAdvisors(factory));
}
}
}
this.aspectBeanNames = aspectNames;
return advisors;
}
}
}
return advisors;
}
這個(gè)方法里面首先從IOC中拿到所有Bean的名稱,并循環(huán)判斷該類上是否帶有@Aspect注解,如果有則將BeanName和Bean的Class類型封裝到BeanFactoryAspectInstanceFactory中,并調(diào)用ReflectiveAspectJAdvisorFactory.getAdvisors創(chuàng)建切面對(duì)象:
public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
//從工廠中獲取有@Aspect注解的類Class
Class<?> aspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
//從工廠中獲取有@Aspect注解的類的名稱
String aspectName = aspectInstanceFactory.getAspectMetadata().getAspectName();
validate(aspectClass);
// 創(chuàng)建工廠的裝飾類,獲取實(shí)例只會(huì)獲取一次
MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory);
List<Advisor> advisors = new ArrayList<>();
//這里循環(huán)沒有@Pointcut注解的方法
for (Method method : getAdvisorMethods(aspectClass)) {
//非常重要重點(diǎn)看看
Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
if (advisor != null) {
advisors.add(advisor);
}
}
if (!advisors.isEmpty() && lazySingletonAspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
Advisor instantiationAdvisor = new SyntheticInstantiationAdvisor(lazySingletonAspectInstanceFactory);
advisors.add(0, instantiationAdvisor);
}
//判斷屬性上是否有引介注解,這里可以不看
for (Field field : aspectClass.getDeclaredFields()) {
//判斷屬性上是否有DeclareParents注解,如果有返回切面
Advisor advisor = getDeclareParentsAdvisor(field);
if (advisor != null) {
advisors.add(advisor);
}
}
return advisors;
}
private List<Method> getAdvisorMethods(Class<?> aspectClass) {
final List<Method> methods = new ArrayList<>();
ReflectionUtils.doWithMethods(aspectClass, method -> {
// Exclude pointcuts
if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) {
methods.add(method);
}
});
methods.sort(METHOD_COMPARATOR);
return methods;
}
根據(jù)Aspect的Class拿到所有不帶@Pointcut注解的方法對(duì)象(為什么是不帶@Pointcut注解的方法?仔細(xì)想想不難理解),另外要注意這里對(duì)method進(jìn)行了排序,看看這個(gè)METHOD_COMPARATOR比較器:
private static final Comparator<Method> METHOD_COMPARATOR;
static {
Comparator<Method> adviceKindComparator = new ConvertingComparator<>(
new InstanceComparator<>(
Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class),
(Converter<Method, Annotation>) method -> {
AspectJAnnotation<?> annotation =
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method);
return (annotation != null ? annotation.getAnnotation() : null);
});
Comparator<Method> methodNameComparator = new ConvertingComparator<>(Method::getName);
METHOD_COMPARATOR = adviceKindComparator.thenComparing(methodNameComparator);
}
關(guān)注InstanceComparator構(gòu)造函數(shù)參數(shù),記住它們的順序,這就是AOP鏈?zhǔn)秸{(diào)用中同一個(gè)@Aspect類中Advice的執(zhí)行順序。接著往下看,在getAdvisors方法中循環(huán)獲取到的methods,分別調(diào)用getAdvisor方法,也就是根據(jù)方法逐個(gè)去創(chuàng)建切面:
public Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aspectInstanceFactory,
int declarationOrderInAspect, String aspectName) {
validate(aspectInstanceFactory.getAspectMetadata().getAspectClass());
//獲取pointCut對(duì)象,最重要的是從注解中獲取表達(dá)式
AspectJExpressionPointcut expressionPointcut = getPointcut(
candidateAdviceMethod, aspectInstanceFactory.getAspectMetadata().getAspectClass());
if (expressionPointcut == null) {
return null;
}
//創(chuàng)建Advisor切面類,這才是真正的切面類,一個(gè)切面類里面肯定要有1、pointCut 2、advice
//這里pointCut是expressionPointcut, advice 增強(qiáng)方法是 candidateAdviceMethod
return new InstantiationModelAwarePointcutAdvisorImpl(expressionPointcut, candidateAdviceMethod,
this, aspectInstanceFactory, declarationOrderInAspect, aspectName);
}
private static final Class<?>[] ASPECTJ_ANNOTATION_CLASSES = new Class<?>[] {
Pointcut.class, Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class};
private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class<?> candidateAspectClass) {
//從候選的增強(qiáng)方法里面 candidateAdviceMethod 找有有注解
//Pointcut.class, Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class
//并把注解信息封裝成AspectJAnnotation對(duì)象
AspectJAnnotation<?> aspectJAnnotation =
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
if (aspectJAnnotation == null) {
return null;
}
//創(chuàng)建一個(gè)PointCut類,并且把前面從注解里面解析的表達(dá)式設(shè)置進(jìn)去
AspectJExpressionPointcut ajexp =
new AspectJExpressionPointcut(candidateAspectClass, new String[0], new Class<?>[0]);
ajexp.setExpression(aspectJAnnotation.getPointcutExpression());
if (this.beanFactory != null) {
ajexp.setBeanFactory(this.beanFactory);
}
return ajexp;
}
之前就說(shuō)過(guò)切面的定義,是切點(diǎn)和增強(qiáng)的組合,所以這里首先通過(guò)getPointcut獲取到注解對(duì)象,然后new了一個(gè)Pointcut對(duì)象,并將表達(dá)式設(shè)置進(jìn)去。然后在getAdvisor方法中最后new了一個(gè)InstantiationModelAwarePointcutAdvisorImpl對(duì)象:
public InstantiationModelAwarePointcutAdvisorImpl(AspectJExpressionPointcut declaredPointcut,
Method aspectJAdviceMethod, AspectJAdvisorFactory aspectJAdvisorFactory,
MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
this.declaredPointcut = declaredPointcut;
this.declaringClass = aspectJAdviceMethod.getDeclaringClass();
this.methodName = aspectJAdviceMethod.getName();
this.parameterTypes = aspectJAdviceMethod.getParameterTypes();
this.aspectJAdviceMethod = aspectJAdviceMethod;
this.aspectJAdvisorFactory = aspectJAdvisorFactory;
this.aspectInstanceFactory = aspectInstanceFactory;
this.declarationOrder = declarationOrder;
this.aspectName = aspectName;
if (aspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
// Static part of the pointcut is a lazy type.
Pointcut preInstantiationPointcut = Pointcuts.union(
aspectInstanceFactory.getAspectMetadata().getPerClausePointcut(), this.declaredPointcut);
// Make it dynamic: must mutate from pre-instantiation to post-instantiation state.
// If it's not a dynamic pointcut, it may be optimized out
// by the Spring AOP infrastructure after the first evaluation.
this.pointcut = new PerTargetInstantiationModelPointcut(
this.declaredPointcut, preInstantiationPointcut, aspectInstanceFactory);
this.lazy = true;
}
else {
// A singleton aspect.
this.pointcut = this.declaredPointcut;
this.lazy = false;
//這個(gè)方法重點(diǎn)看看,創(chuàng)建advice對(duì)象
this.instantiatedAdvice = instantiateAdvice(this.declaredPointcut);
}
}
這個(gè)就是我們的切面類,在其構(gòu)造方法的最后通過(guò)instantiateAdvice創(chuàng)建了Advice對(duì)象。注意這里傳進(jìn)來(lái)的declarationOrder參數(shù),它就是循環(huán)method時(shí)的序號(hào),其作用就是賦值給這里的declarationOrder屬性以及Advice的declarationOrder屬性,在后面排序時(shí)就會(huì)通過(guò)這個(gè)序號(hào)來(lái)比較,因此Advice的執(zhí)行順序是固定的,至于為什么要固定,后面分析完AOP鏈?zhǔn)秸{(diào)用過(guò)程自然就明白了。
public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut,
MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
//獲取有@Aspect注解的類
Class<?> candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
validate(candidateAspectClass);
//找到candidateAdviceMethod方法上面的注解,并且包裝成AspectJAnnotation對(duì)象,這個(gè)對(duì)象中就有注解類型
AspectJAnnotation<?> aspectJAnnotation =
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
if (aspectJAnnotation == null) {
return null;
}
AbstractAspectJAdvice springAdvice;
//根據(jù)不同的注解類型創(chuàng)建不同的advice類實(shí)例
switch (aspectJAnnotation.getAnnotationType()) {
case AtPointcut:
if (logger.isDebugEnabled()) {
logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
}
return null;
case AtAround:
//實(shí)現(xiàn)了MethodInterceptor接口
springAdvice = new AspectJAroundAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
break;
case AtBefore:
//實(shí)現(xiàn)了MethodBeforeAdvice接口,沒有實(shí)現(xiàn)MethodInterceptor接口
springAdvice = new AspectJMethodBeforeAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
break;
case AtAfter:
//實(shí)現(xiàn)了MethodInterceptor接口
springAdvice = new AspectJAfterAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
break;
case AtAfterReturning:
//實(shí)現(xiàn)了AfterReturningAdvice接口,沒有實(shí)現(xiàn)MethodInterceptor接口
springAdvice = new AspectJAfterReturningAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
if (StringUtils.hasText(afterReturningAnnotation.returning())) {
springAdvice.setReturningName(afterReturningAnnotation.returning());
}
break;
case AtAfterThrowing:
//實(shí)現(xiàn)了MethodInterceptor接口
springAdvice = new AspectJAfterThrowingAdvice(
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
}
break;
default:
throw new UnsupportedOperationException(
"Unsupported advice type on method: " + candidateAdviceMethod);
}
// Now to configure the advice...
springAdvice.setAspectName(aspectName);
springAdvice.setDeclarationOrder(declarationOrder);
String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod);
if (argNames != null) {
springAdvice.setArgumentNamesFromStringArray(argNames);
}
//計(jì)算argNames和類型的對(duì)應(yīng)關(guān)系
springAdvice.calculateArgumentBindings();
return springAdvice;
}
這里邏輯很清晰,就是拿到方法上的注解類型,根據(jù)類型創(chuàng)建不同的增強(qiáng)Advice對(duì)象:AspectJAroundAdvice、AspectJMethodBeforeAdvice、AspectJAfterAdvice、AspectJAfterReturningAdvice、AspectJAfterThrowingAdvice。完成之后通過(guò)calculateArgumentBindings方法進(jìn)行參數(shù)綁定,感興趣的可自行研究。這里主要看看幾個(gè)Advice的繼承體系:

可以看到有兩個(gè)Advice是沒有實(shí)現(xiàn)MethodInterceptor接口的:AspectJMethodBeforeAdvice和AspectJAfterReturningAdvice。而MethodInterceptor有一個(gè)invoke方法,這個(gè)方法就是鏈?zhǔn)秸{(diào)用的核心方法,但那兩個(gè)沒有實(shí)現(xiàn)該方法的Advice怎么處理呢?稍后會(huì)分析。
到這里切面對(duì)象就創(chuàng)建完成了,接下來(lái)就是判斷當(dāng)前創(chuàng)建的Bean實(shí)例是否和這些切面匹配以及對(duì)切面排序。匹配過(guò)程比較復(fù)雜,對(duì)理解主流程也沒什么幫助,所以這里就不展開分析,感興趣的自行分析(AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply())。
下面看看排序的過(guò)程,回到AbstractAdvisorAutoProxyCreator.findEligibleAdvisors方法:
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
//找到候選的切面,其實(shí)就是一個(gè)尋找有@Aspectj注解的過(guò)程,把工程中所有有這個(gè)注解的類封裝成Advisor返回
List<Advisor> candidateAdvisors = findCandidateAdvisors();
//判斷候選的切面是否作用在當(dāng)前beanClass上面,就是一個(gè)匹配過(guò)程。?,F(xiàn)在就是一個(gè)匹配
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
extendAdvisors(eligibleAdvisors);
if (!eligibleAdvisors.isEmpty()) {
//對(duì)有@Order@Priority進(jìn)行排序
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
}
return eligibleAdvisors;
}
sortAdvisors方法就是排序,但這個(gè)方法有兩個(gè)實(shí)現(xiàn):當(dāng)前類AbstractAdvisorAutoProxyCreator和子類AspectJAwareAdvisorAutoProxyCreator,應(yīng)該走哪個(gè)呢?

通過(guò)類圖我們可以肯定是進(jìn)入的AspectJAwareAdvisorAutoProxyCreator類,因?yàn)锳nnotationAwareAspectJAutoProxyCreator的父類是它。
protected List<Advisor> sortAdvisors(List<Advisor> advisors) {
List<PartiallyComparableAdvisorHolder> partiallyComparableAdvisors = new ArrayList<>(advisors.size());
for (Advisor element : advisors) {
partiallyComparableAdvisors.add(
new PartiallyComparableAdvisorHolder(element, DEFAULT_PRECEDENCE_COMPARATOR));
}
List<PartiallyComparableAdvisorHolder> sorted = PartialOrder.sort(partiallyComparableAdvisors);
if (sorted != null) {
List<Advisor> result = new ArrayList<>(advisors.size());
for (PartiallyComparableAdvisorHolder pcAdvisor : sorted) {
result.add(pcAdvisor.getAdvisor());
}
return result;
}
else {
return super.sortAdvisors(advisors);
}
}
這里排序主要是委托給PartialOrder進(jìn)行的,而在此之前將所有的切面都封裝成了PartiallyComparableAdvisorHolder對(duì)象,注意傳入的DEFAULT_PRECEDENCE_COMPARATOR參數(shù),這個(gè)就是比較器對(duì)象:
private static final Comparator<Advisor> DEFAULT_PRECEDENCE_COMPARATOR = new AspectJPrecedenceComparator();
所以我們直接看這個(gè)比較器的compare方法:
public int compare(Advisor o1, Advisor o2) {
int advisorPrecedence = this.advisorComparator.compare(o1, o2);
if (advisorPrecedence == SAME_PRECEDENCE && declaredInSameAspect(o1, o2)) {
advisorPrecedence = comparePrecedenceWithinAspect(o1, o2);
}
return advisorPrecedence;
}
private final Comparator<? super Advisor> advisorComparator;
public AspectJPrecedenceComparator() {
this.advisorComparator = AnnotationAwareOrderComparator.INSTANCE;
}
第一步先通過(guò)AnnotationAwareOrderComparator去比較,點(diǎn)進(jìn)去看可以發(fā)現(xiàn)是對(duì)實(shí)現(xiàn)了PriorityOrdered和Ordered接口以及標(biāo)記了Priority和Order注解的非同一個(gè)@Aspect類中的切面進(jìn)行排序。這個(gè)和之前分析BeanFacotryPostProcessor類是一樣的原理。而對(duì)同一個(gè)@Aspect類中的切面排序主要是comparePrecedenceWithinAspect方法:
private int comparePrecedenceWithinAspect(Advisor advisor1, Advisor advisor2) {
boolean oneOrOtherIsAfterAdvice =
(AspectJAopUtils.isAfterAdvice(advisor1) || AspectJAopUtils.isAfterAdvice(advisor2));
int adviceDeclarationOrderDelta = getAspectDeclarationOrder(advisor1) - getAspectDeclarationOrder(advisor2);
if (oneOrOtherIsAfterAdvice) {
// the advice declared last has higher precedence
if (adviceDeclarationOrderDelta < 0) {
// advice1 was declared before advice2
// so advice1 has lower precedence
return LOWER_PRECEDENCE;
}
else if (adviceDeclarationOrderDelta == 0) {
return SAME_PRECEDENCE;
}
else {
return HIGHER_PRECEDENCE;
}
}
else {
// the advice declared first has higher precedence
if (adviceDeclarationOrderDelta < 0) {
// advice1 was declared before advice2
// so advice1 has higher precedence
return HIGHER_PRECEDENCE;
}
else if (adviceDeclarationOrderDelta == 0) {
return SAME_PRECEDENCE;
}
else {
return LOWER_PRECEDENCE;
}
}
}
private int getAspectDeclarationOrder(Advisor anAdvisor) {
AspectJPrecedenceInformation precedenceInfo =
AspectJAopUtils.getAspectJPrecedenceInformationFor(anAdvisor);
if (precedenceInfo != null) {
return precedenceInfo.getDeclarationOrder();
}
else {
return 0;
}
}
這里就是通過(guò)precedenceInfo.getDeclarationOrder拿到在創(chuàng)建InstantiationModelAwarePointcutAdvisorImpl對(duì)象時(shí)設(shè)置的declarationOrder屬性,這就驗(yàn)證了之前的說(shuō)法(實(shí)際上這里排序過(guò)程非常復(fù)雜,不是簡(jiǎn)單的按照這個(gè)屬性進(jìn)行排序)。
當(dāng)上面的一切都進(jìn)行完成后,就該創(chuàng)建代理對(duì)象了,回到AbstractAutoProxyCreator.wrapIfNecessary,看關(guān)鍵部分代碼:
//如果有切面,則生成該bean的代理
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
//把被代理對(duì)象bean實(shí)例封裝到SingletonTargetSource對(duì)象中
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
注意這里將被代理對(duì)象封裝成了一個(gè)SingletonTargetSource對(duì)象,它是TargetSource的實(shí)現(xiàn)類。
protected Object createProxy(Class<?> beanClass, @Nullable String beanName,
@Nullable Object[] specificInterceptors, TargetSource targetSource) {
if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
}
//創(chuàng)建代理工廠
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.copyFrom(this);
if (!proxyFactory.isProxyTargetClass()) {
if (shouldProxyTargetClass(beanClass, beanName)) {
//proxyTargetClass 是否對(duì)類進(jìn)行代理,而不是對(duì)接口進(jìn)行代理,設(shè)置為true時(shí),使用CGLib代理。
proxyFactory.setProxyTargetClass(true);
}
else {
evaluateProxyInterfaces(beanClass, proxyFactory);
}
}
//把a(bǔ)dvice類型的增強(qiáng)包裝成advisor切面
Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
proxyFactory.addAdvisors(advisors);
proxyFactory.setTargetSource(targetSource);
customizeProxyFactory(proxyFactory);
用來(lái)控制代理工廠被配置后,是否還允許修改代理的配置,默認(rèn)為false
proxyFactory.setFrozen(this.freezeProxy);
if (advisorsPreFiltered()) {
proxyFactory.setPreFiltered(true);
}
//獲取代理實(shí)例
return proxyFactory.getProxy(getProxyClassLoader());
}
這里通過(guò)ProxyFactory對(duì)象去創(chuàng)建代理實(shí)例,這是工廠模式的體現(xiàn),但在創(chuàng)建代理對(duì)象之前還有幾個(gè)準(zhǔn)備動(dòng)作:需要判斷是JDK代理還是CGLIB代理以及通過(guò)buildAdvisors方法將擴(kuò)展的Advice封裝成Advisor切面。準(zhǔn)備完成則通過(guò)getProxy創(chuàng)建代理對(duì)象:
public Object getProxy(@Nullable ClassLoader classLoader) {
//根據(jù)目標(biāo)對(duì)象是否有接口來(lái)判斷采用什么代理方式,cglib代理還是jdk動(dòng)態(tài)代理
return createAopProxy().getProxy(classLoader);
}
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
return getAopProxyFactory().createAopProxy(this);
}
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
首先通過(guò)配置拿到對(duì)應(yīng)的代理類:ObjenesisCglibAopProxy和JdkDynamicAopProxy,然后再通過(guò)getProxy創(chuàng)建Bean的代理,這里以JdkDynamicAopProxy為例:
public Object getProxy(@Nullable ClassLoader classLoader) {
//advised是代理工廠對(duì)象
Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);
findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}
這里的代碼你應(yīng)該不陌生了,就是JDK的原生API,newProxyInstance方法傳入的InvocationHandler對(duì)象是this,因此,最終AOP代理的調(diào)用就是從該類中的invoke方法開始。至此,代理對(duì)象的創(chuàng)建就完成了,下面來(lái)看下整個(gè)過(guò)程的時(shí)序圖:

小結(jié)
代理對(duì)象的創(chuàng)建過(guò)程整體來(lái)說(shuō)并不復(fù)雜,首先找到所有帶有@Aspect注解的類,并獲取其中沒有@Pointcut注解的方法,循環(huán)創(chuàng)建切面,而創(chuàng)建切面需要切點(diǎn)和增強(qiáng)兩個(gè)元素,其中切點(diǎn)可簡(jiǎn)單理解為我們寫的表達(dá)式,增強(qiáng)則是根據(jù)@Before、@Around、@After等注解創(chuàng)建的對(duì)應(yīng)的Advice類。切面創(chuàng)建好后則需要循環(huán)判斷哪些切面能對(duì)當(dāng)前的Bean實(shí)例的方法進(jìn)行增強(qiáng)并排序,最后通過(guò)ProxyFactory創(chuàng)建代理對(duì)象。
AOP鏈?zhǔn)秸{(diào)用
熟悉JDK動(dòng)態(tài)代理的都知道通過(guò)代理對(duì)象調(diào)用方法時(shí),會(huì)進(jìn)入到InvocationHandler對(duì)象的invoke方法,所以我們直接從JdkDynamicAopProxy的這個(gè)方法開始:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodInvocation invocation;
Object oldProxy = null;
boolean setProxyContext = false;
//從代理工廠中拿到TargetSource對(duì)象,該對(duì)象包裝了被代理實(shí)例bean
TargetSource targetSource = this.advised.targetSource;
Object target = null;
try {
//被代理對(duì)象的equals方法和hashCode方法是不能被代理的,不會(huì)走切面
.......
Object retVal;
// 可以從當(dāng)前線程中拿到代理對(duì)象
if (this.advised.exposeProxy) {
// Make invocation available if necessary.
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
//這個(gè)target就是被代理實(shí)例
target = targetSource.getTarget();
Class<?> targetClass = (target != null ? target.getClass() : null);
//從代理工廠中拿過(guò)濾器鏈 Object是一個(gè)MethodInterceptor類型的對(duì)象,其實(shí)就是一個(gè)advice對(duì)象
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
//如果該方法沒有執(zhí)行鏈,則說(shuō)明這個(gè)方法不需要被攔截,則直接反射調(diào)用
if (chain.isEmpty()) {
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
retVal = invocation.proceed();
}
// Massage return value if necessary.
Class<?> returnType = method.getReturnType();
if (retVal != null && retVal == target &&
returnType != Object.class && returnType.isInstance(proxy) &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
retVal = proxy;
}
return retVal;
}
finally {
if (target != null && !targetSource.isStatic()) {
// Must have come from TargetSource.
targetSource.releaseTarget(target);
}
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
這段代碼比較長(zhǎng),我刪掉了不關(guān)鍵的地方。首先來(lái)看this.advised.exposeProxy這個(gè)屬性,這在@EnableAspectJAutoProxy注解中可以配置,當(dāng)為true時(shí),會(huì)將該代理對(duì)象設(shè)置到當(dāng)前線程的ThreadLocal對(duì)象中,這樣就可以通過(guò)AopContext.currentProxy拿到代理對(duì)象。這個(gè)有什么用呢?我相信有經(jīng)驗(yàn)的Java開發(fā)都遇到過(guò)這樣一個(gè)BUG,在Service實(shí)現(xiàn)類中調(diào)用本類中的另一個(gè)方法時(shí),事務(wù)不會(huì)生效,這是因?yàn)橹苯油ㄟ^(guò)this調(diào)用就不會(huì)調(diào)用到代理對(duì)象的方法,而是原對(duì)象的,所以事務(wù)切面就沒有生效。因此這種情況下就可以從當(dāng)前線程的ThreadLocal對(duì)象拿到代理對(duì)象,不過(guò)實(shí)際上直接使用@Autowired注入自己本身也可以拿到代理對(duì)象。
接下來(lái)就是通過(guò)getInterceptorsAndDynamicInterceptionAdvice拿到執(zhí)行鏈,看看具體做了哪些事情:
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
Advised config, Method method, @Nullable Class<?> targetClass) {
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
//從代理工廠中獲得該被代理類的所有切面advisor,config就是代理工廠對(duì)象
Advisor[] advisors = config.getAdvisors();
List<Object> interceptorList = new ArrayList<>(advisors.length);
Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
Boolean hasIntroductions = null;
for (Advisor advisor : advisors) {
//大部分走這里
if (advisor instanceof PointcutAdvisor) {
// Add it conditionally.
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
//如果切面的pointCut和被代理對(duì)象是匹配的,說(shuō)明是切面要攔截的對(duì)象
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
boolean match;
if (mm instanceof IntroductionAwareMethodMatcher) {
if (hasIntroductions == null) {
hasIntroductions = hasMatchingIntroductions(advisors, actualClass);
}
match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions);
}
else {
//接下來(lái)判斷方法是否是切面pointcut需要攔截的方法
match = mm.matches(method, actualClass);
}
//如果類和方法都匹配
if (match) {
//獲取到切面advisor中的advice,并且包裝成MethodInterceptor類型的對(duì)象
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
if (mm.isRuntime()) {
for (MethodInterceptor interceptor : interceptors) {
interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
}
}
else {
interceptorList.addAll(Arrays.asList(interceptors));
}
}
}
}
//如果是引介切面
else if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
else {
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
return interceptorList;
}
這也是個(gè)長(zhǎng)方法,看關(guān)鍵的部分,因?yàn)橹拔覀儎?chuàng)建的基本上都是InstantiationModelAwarePointcutAdvisorImpl對(duì)象,該類是PointcutAdvisor的實(shí)現(xiàn)類,所以會(huì)進(jìn)入第一個(gè)if判斷里,這里首先進(jìn)行匹配,看切點(diǎn)和當(dāng)前對(duì)象以及該對(duì)象的哪些方法匹配,如果能匹配上,則調(diào)用getInterceptors獲取執(zhí)行鏈:
private final List<AdvisorAdapter> adapters = new ArrayList<>(3);
public DefaultAdvisorAdapterRegistry() {
registerAdvisorAdapter(new MethodBeforeAdviceAdapter());
registerAdvisorAdapter(new AfterReturningAdviceAdapter());
registerAdvisorAdapter(new ThrowsAdviceAdapter());
}
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<>(3);
Advice advice = advisor.getAdvice();
//如果是MethodInterceptor類型的,如:AspectJAroundAdvice
//AspectJAfterAdvice
//AspectJAfterThrowingAdvice
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
}
//處理 AspectJMethodBeforeAdvice AspectJAfterReturningAdvice
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
interceptors.add(adapter.getInterceptor(advisor));
}
}
if (interceptors.isEmpty()) {
throw new UnknownAdviceTypeException(advisor.getAdvice());
}
return interceptors.toArray(new MethodInterceptor[0]);
}
這里我們可以看到如果是MethodInterceptor的實(shí)現(xiàn)類,則直接添加到鏈中,如果不是,則需要通過(guò)適配器去包裝后添加,剛好這里有MethodBeforeAdviceAdapter和AfterReturningAdviceAdapter兩個(gè)適配器對(duì)應(yīng)上文兩個(gè)沒有實(shí)現(xiàn)MethodInterceptor接口的類。最后將Interceptors返回。
if (chain.isEmpty()) {
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
// We need to create a method invocation...
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
// Proceed to the joinpoint through the interceptor chain.
retVal = invocation.proceed();
}
返回到invoke方法后,如果執(zhí)行鏈為空,說(shuō)明該方法不需要被增強(qiáng),所以直接反射調(diào)用原對(duì)象的方法(注意傳入的是TargetSource封裝的被代理對(duì)象);反之,則通過(guò)ReflectiveMethodInvocation類進(jìn)行鏈?zhǔn)秸{(diào)用,關(guān)鍵方法就是proceed:
private int currentInterceptorIndex = -1;
public Object proceed() throws Throwable {
//如果執(zhí)行鏈中的advice全部執(zhí)行完,則直接調(diào)用joinPoint方法,就是被代理方法
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
return proceed();
}
}
else {
//調(diào)用MethodInterceptor中的invoke方法
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
這個(gè)方法的核心就在兩個(gè)地方:invokeJoinpoint和interceptorOrInterceptionAdvice.invoke(this)。當(dāng)增強(qiáng)方法調(diào)用完后就會(huì)通過(guò)前者調(diào)用到被代理的方法,否則則是依次調(diào)用Interceptor的invoke方法。下面就分別看看每個(gè)Interceptor是怎么實(shí)現(xiàn)的。
AspectJAroundAdvice
public Object invoke(MethodInvocation mi) throws Throwable {
if (!(mi instanceof ProxyMethodInvocation)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
}
ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi;
ProceedingJoinPoint pjp = lazyGetProceedingJoinPoint(pmi);
JoinPointMatch jpm = getJoinPointMatch(pmi);
return invokeAdviceMethod(pjp, jpm, null, null);
}
MethodBeforeAdviceInterceptor -> AspectJMethodBeforeAdvice
public Object invoke(MethodInvocation mi) throws Throwable {
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
return mi.proceed();
}
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
invokeAdviceMethod(getJoinPointMatch(), null, null);
}
AspectJAfterAdvice
public Object invoke(MethodInvocation mi) throws Throwable {
try {
return mi.proceed();
}
finally {
invokeAdviceMethod(getJoinPointMatch(), null, null);
}
}
AfterReturningAdviceInterceptor -> AspectJAfterReturningAdvice
public Object invoke(MethodInvocation mi) throws Throwable {
Object retVal = mi.proceed();
this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
return retVal;
}
public void afterReturning(@Nullable Object returnValue, Method method, Object[] args, @Nullable Object target) throws Throwable {
if (shouldInvokeOnReturnValueOf(method, returnValue)) {
invokeAdviceMethod(getJoinPointMatch(), returnValue, null);
}
}
AspectJAfterThrowingAdvice
public Object invoke(MethodInvocation mi) throws Throwable {
try {
return mi.proceed();
}
catch (Throwable ex) {
if (shouldInvokeOnThrowing(ex)) {
invokeAdviceMethod(getJoinPointMatch(), null, ex);
}
throw ex;
}
}
這里的調(diào)用順序是怎樣的呢?其核心就是通過(guò)proceed方法控制流程,每執(zhí)行完一個(gè)Advice就會(huì)回到proceed方法中調(diào)用下一個(gè)Advice??梢运伎家幌?,怎么才能讓調(diào)用結(jié)果滿足如下圖的執(zhí)行順序。

以上就是AOP的鏈?zhǔn)秸{(diào)用過(guò)程,但是這只是只有一個(gè)切面類的情況,如果有多個(gè)@Aspect類呢,這個(gè)調(diào)用過(guò)程又是怎樣的?其核心思想和“?!币粯樱褪恰跋冗M(jìn)后出,后進(jìn)先出”。
AOP擴(kuò)展知識(shí)
一、自定義全局?jǐn)r截器Interceptor
在上文創(chuàng)建代理對(duì)象的時(shí)候有這樣一個(gè)方法:
protected Advisor[] buildAdvisors(@Nullable String beanName, @Nullable Object[] specificInterceptors) {
//自定義MethodInterceptor.拿到setInterceptorNames方法注入的Interceptor對(duì)象
Advisor[] commonInterceptors = resolveInterceptorNames();
List<Object> allInterceptors = new ArrayList<>();
if (specificInterceptors != null) {
allInterceptors.addAll(Arrays.asList(specificInterceptors));
if (commonInterceptors.length > 0) {
if (this.applyCommonInterceptorsFirst) {
allInterceptors.addAll(0, Arrays.asList(commonInterceptors));
}
else {
allInterceptors.addAll(Arrays.asList(commonInterceptors));
}
}
}
Advisor[] advisors = new Advisor[allInterceptors.size()];
for (int i = 0; i < allInterceptors.size(); i++) {
//對(duì)自定義的advice要進(jìn)行包裝,把a(bǔ)dvice包裝成advisor對(duì)象,切面對(duì)象
advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i));
}
return advisors;
}
這個(gè)方法的作用就在于我們可以擴(kuò)展我們自己的Interceptor,首先通過(guò)resolveInterceptorNames方法獲取到通過(guò)setInterceptorNames方法設(shè)置的Interceptor,然后調(diào)用DefaultAdvisorAdapterRegistry.wrap方法將其包裝為DefaultPointcutAdvisor對(duì)象并返回:
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
if (adviceObject instanceof Advisor) {
return (Advisor) adviceObject;
}
if (!(adviceObject instanceof Advice)) {
throw new UnknownAdviceTypeException(adviceObject);
}
Advice advice = (Advice) adviceObject;
if (advice instanceof MethodInterceptor) {
return new DefaultPointcutAdvisor(advice);
}
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
return new DefaultPointcutAdvisor(advice);
}
}
throw new UnknownAdviceTypeException(advice);
}
public DefaultPointcutAdvisor(Advice advice) {
this(Pointcut.TRUE, advice);
}
需要注意DefaultPointcutAdvisor構(gòu)造器里面?zhèn)魅肓艘粋€(gè)Pointcut.TRUE,表示這種擴(kuò)展的Interceptor是全局的攔截器。下面來(lái)看看如何使用:
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("自定義攔截器");
return invocation.proceed();
}
}
首先寫一個(gè)類實(shí)現(xiàn)MethodInterceptor 接口,在invoke方法中實(shí)現(xiàn)我們的攔截邏輯,然后通過(guò)下面的方式測(cè)試,只要UserService 有AOP攔截就會(huì)發(fā)現(xiàn)自定義的MyMethodInterceptor也生效了。
public void costomInterceptorTest() {
AnnotationAwareAspectJAutoProxyCreator bean = applicationContext.getBean(AnnotationAwareAspectJAutoProxyCreator.class);
bean.setInterceptorNames("myMethodInterceptor ");
UserService userService = applicationContext.getBean(UserService.class);
userService.queryUser("dark");
}
但是如果換個(gè)順序,像下面這樣:
public void costomInterceptorTest() {
UserService userService = applicationContext.getBean(UserService.class);
AnnotationAwareAspectJAutoProxyCreator bean = applicationContext.getBean(AnnotationAwareAspectJAutoProxyCreator.class);
bean.setInterceptorNames("myMethodInterceptor ");
userService.queryUser("dark");
}
這時(shí)自定義的全局?jǐn)r截器就沒有作用了,這是為什么呢?因?yàn)楫?dāng)執(zhí)行g(shù)etBean的時(shí)候,如果有切面匹配就會(huì)通過(guò)ProxyFactory去創(chuàng)建代理對(duì)象,注意Interceptor是存到這個(gè)Factory對(duì)象中的,而這個(gè)對(duì)象和代理對(duì)象是一一對(duì)應(yīng)的,因此調(diào)用getBean時(shí),還沒有myMethodInterceptor這個(gè)對(duì)象,自定義攔截器就沒有效果了,也就是說(shuō)要想自定義攔截器生效,就必須在代理對(duì)象生成之前注冊(cè)進(jìn)去。
二、循環(huán)依賴三級(jí)緩存存在的必要性
在上一篇文章我分析了Spring是如何通過(guò)三級(jí)緩存來(lái)解決循環(huán)依賴的問(wèn)題的,但你是否考慮過(guò)第三級(jí)緩存為什么要存在?我直接將bean存到二級(jí)不就行了么,為什么還要存一個(gè)ObjectFactory對(duì)象到第三級(jí)緩存中?一個(gè)是因?yàn)椴皇敲總€(gè)Bean都會(huì)出現(xiàn)循環(huán)依賴,所以三級(jí)緩存只存了一個(gè)工廠對(duì)象;二是我們?cè)贎Autowired對(duì)象時(shí),想要注入的不一定是Bean本身,而是想要注入一個(gè)修改過(guò)后的對(duì)象,如代理對(duì)象。在AbstractAutowireCapableBeanFactory.getEarlyBeanReference方法中循環(huán)調(diào)用了SmartInstantiationAwareBeanPostProcessor.getEarlyBeanReference方法,AbstractAutoProxyCreator對(duì)象就實(shí)現(xiàn)了該方法:
public Object getEarlyBeanReference(Object bean, String beanName) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.contains(cacheKey)) {
this.earlyProxyReferences.add(cacheKey);
}
// 創(chuàng)建代理對(duì)象
return wrapIfNecessary(bean, beanName, cacheKey);
}
因此,當(dāng)我們想要對(duì)循壞依賴的Bean做出修改時(shí),就可以像AOP這樣做。
三、如何在Bean創(chuàng)建之前提前創(chuàng)建代理對(duì)象
Spring的代理對(duì)象基本上都是在Bean實(shí)例化完成之后創(chuàng)建的,但在文章開始我就說(shuō)過(guò),Spring也提供了一個(gè)機(jī)會(huì)在創(chuàng)建Bean對(duì)象之前就創(chuàng)建代理對(duì)象,在AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation方法中:
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
Object bean = null;
if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
// Make sure bean class is actually resolved at this point.
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
Class<?> targetType = determineTargetType(beanName, mbd);
if (targetType != null) {
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
}
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
}
return null;
}
主要是InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation方法中,這里又會(huì)進(jìn)入到AbstractAutoProxyCreator類中:
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
if (StringUtils.hasLength(beanName)) {
this.targetSourcedBeans.add(beanName);
}
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
return null;
}
protected TargetSource getCustomTargetSource(Class<?> beanClass, String beanName) {
// We can't create fancy target sources for directly registered singletons.
if (this.customTargetSourceCreators != null &&
this.beanFactory != null && this.beanFactory.containsBean(beanName)) {
for (TargetSourceCreator tsc : this.customTargetSourceCreators) {
TargetSource ts = tsc.getTargetSource(beanClass, beanName);
if (ts != null) {
return ts;
}
}
}
// No custom TargetSource found.
return null;
}
看到這里大致應(yīng)該明白了,先是獲取到一個(gè)自定義的TargetSource對(duì)象,然后創(chuàng)建代理對(duì)象,所以我們首先需要自己實(shí)現(xiàn)一個(gè)TargetSource類,這里直接繼承一個(gè)抽象類,getTarget方法則返回原始對(duì)象:
public class MyTargetSource extends AbstractBeanFactoryBasedTargetSource {
@Override
public Object getTarget() throws Exception {
return getBeanFactory().getBean(getTargetBeanName());
}
}
但這還不夠,上面首先判斷了customTargetSourceCreators!=null,而這個(gè)屬性是個(gè)數(shù)組,可以通過(guò)下面這個(gè)方法設(shè)置進(jìn)來(lái):
public void setCustomTargetSourceCreators(TargetSourceCreator... targetSourceCreators) {
this.customTargetSourceCreators = targetSourceCreators;
}
所以我們還要實(shí)現(xiàn)一個(gè)TargetSourceCreator類,同樣繼承一個(gè)抽象類實(shí)現(xiàn),并只對(duì)userServiceImpl對(duì)象進(jìn)行攔截:
public class MyTargetSourceCreator extends AbstractBeanFactoryBasedTargetSourceCreator {
@Override
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(Class<?> beanClass, String beanName) {
if (getBeanFactory() instanceof ConfigurableListableBeanFactory) {
if(beanName.equalsIgnoreCase("userServiceImpl")) {
return new MyTargetSource();
}
}
return null;
}
}
createBeanFactoryBasedTargetSource方法是在AbstractBeanFactoryBasedTargetSourceCreator.getTargetSource中調(diào)用的,而getTargetSource就是在上面getCustomTargetSource中調(diào)用的。以上工作做完后,還需要將其設(shè)置到AnnotationAwareAspectJAutoProxyCreator對(duì)象中,因此需要我們注入這個(gè)對(duì)象:
@Configuration
public class TargetSourceCreatorBean {
@Autowired
private BeanFactory beanFactory;
@Bean
public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {
AnnotationAwareAspectJAutoProxyCreator creator = new AnnotationAwareAspectJAutoProxyCreator();
MyTargetSourceCreator myTargetSourceCreator = new MyTargetSourceCreator();
myTargetSourceCreator.setBeanFactory(beanFactory);
creator.setCustomTargetSourceCreators(myTargetSourceCreator);
return creator;
}
}
這樣,當(dāng)我們通過(guò)getBean獲取userServiceImpl的對(duì)象時(shí),就會(huì)優(yōu)先生成代理對(duì)象,然后在調(diào)用執(zhí)行鏈的過(guò)程中再通過(guò)TargetSource.getTarget獲取到被代理對(duì)象。但是,為什么我們?cè)趃etTarget方法中調(diào)用getBean就能拿到被代理對(duì)象呢?
繼續(xù)探究,通過(guò)斷點(diǎn)我發(fā)現(xiàn)從getTarget進(jìn)入時(shí),在resolveBeforeInstantiation方法中返回的bean就是null了,而getBeanPostProcessors方法返回的Processors中也沒有了AnnotationAwareAspectJAutoProxyCreator對(duì)象,也就是沒有進(jìn)入到AbstractAutoProxyCreator.postProcessBeforeInstantiation方法中,所以不會(huì)再次獲取到代理對(duì)象,那AnnotationAwareAspectJAutoProxyCreator對(duì)象是在什么時(shí)候移除的呢?
帶著問(wèn)題,我開始反推,發(fā)現(xiàn)在AbstractBeanFactoryBasedTargetSourceCreator類中有這樣一個(gè)方法buildInternalBeanFactory:
protected DefaultListableBeanFactory buildInternalBeanFactory(ConfigurableBeanFactory containingFactory) {
DefaultListableBeanFactory internalBeanFactory = new DefaultListableBeanFactory(containingFactory);
// Required so that all BeanPostProcessors, Scopes, etc become available.
internalBeanFactory.copyConfigurationFrom(containingFactory);
// Filter out BeanPostProcessors that are part of the AOP infrastructure,
// since those are only meant to apply to beans defined in the original factory.
internalBeanFactory.getBeanPostProcessors().removeIf(beanPostProcessor ->
beanPostProcessor instanceof AopInfrastructureBean);
return internalBeanFactory;
}
在這里移除掉了所有AopInfrastructureBean的子類,而AnnotationAwareAspectJAutoProxyCreator就是其子類,那這個(gè)方法是在哪里調(diào)用的呢?繼續(xù)反推:
protected DefaultListableBeanFactory getInternalBeanFactoryForBean(String beanName) {
synchronized (this.internalBeanFactories) {
DefaultListableBeanFactory internalBeanFactory = this.internalBeanFactories.get(beanName);
if (internalBeanFactory == null) {
internalBeanFactory = buildInternalBeanFactory(this.beanFactory);
this.internalBeanFactories.put(beanName, internalBeanFactory);
}
return internalBeanFactory;
}
}
public final TargetSource getTargetSource(Class<?> beanClass, String beanName) {
AbstractBeanFactoryBasedTargetSource targetSource =
createBeanFactoryBasedTargetSource(beanClass, beanName);
// 創(chuàng)建完targetSource后就移除掉AopInfrastructureBean類型的BeanPostProcessor對(duì)象,如AnnotationAwareAspectJAutoProxyCreator
DefaultListableBeanFactory internalBeanFactory = getInternalBeanFactoryForBean(beanName);
......
return targetSource;
}
至此,關(guān)于TargetSource接口擴(kuò)展的原理就搞明白了。
總結(jié)
本篇篇幅比較長(zhǎng),主要搞明白Spring代理對(duì)象是如何創(chuàng)建的以及AOP鏈?zhǔn)秸{(diào)用過(guò)程,而后面的擴(kuò)展則是對(duì)AOP以及Bean創(chuàng)建過(guò)程中一些疑惑的補(bǔ)充,可根據(jù)實(shí)際情況學(xué)習(xí)掌握。希望大家多多支持腳本之家。
相關(guān)文章
使用java獲取指定鏈接的網(wǎng)頁(yè)內(nèi)容
Java提供了許多用于網(wǎng)絡(luò)通信的庫(kù),其中最常用的是HttpURLConnection和HttpClient,本文將使用HttpURLConnection進(jìn)行爬取指定鏈接的網(wǎng)頁(yè)內(nèi)容,感興趣的可以了解下2023-09-09
使用JavaWeb webSocket實(shí)現(xiàn)簡(jiǎn)易的點(diǎn)對(duì)點(diǎn)聊天功能實(shí)例代碼
這篇文章主要介紹了使用JavaWeb webSocket實(shí)現(xiàn)簡(jiǎn)易的點(diǎn)對(duì)點(diǎn)聊天功能實(shí)例代碼的相關(guān)資料,內(nèi)容介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
SpringBoot集成Quartz實(shí)現(xiàn)持久化定時(shí)接口調(diào)用任務(wù)
Quartz是功能強(qiáng)大的開源作業(yè)調(diào)度庫(kù),幾乎可以集成到任何?Java?應(yīng)用程序中,從最小的獨(dú)立應(yīng)用程序到最大的電子商務(wù)系統(tǒng),本文將通過(guò)代碼示例給大家介紹SpringBoot集成Quartz實(shí)現(xiàn)持久化定時(shí)接口調(diào)用任務(wù),需要的朋友可以參考下2023-07-07
Flowable?ReceiveTask使用場(chǎng)景分析
這篇文章主要為大家介紹了Flowable?ReceiveTask使用場(chǎng)景分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
MyBatis-Plus實(shí)現(xiàn)對(duì)查詢結(jié)果進(jìn)行分頁(yè)的基本步驟
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,MyBatis-Plus 支持多種數(shù)據(jù)庫(kù)的分頁(yè)查詢,其分頁(yè)功能是通過(guò) Page 類實(shí)現(xiàn)的,本文介紹了使用 MyBatis-Plus 實(shí)現(xiàn)分頁(yè)查詢的基本步驟,需要的朋友可以參考下2024-08-08

