spring aop 流程
2022-04-15 11:27:41 0 举报
spring aop 流程
作者其他创作
大纲/内容
如果没有Advice,则直接调用对应方法
advisors.isEmpty()
private final List<AdvisorAdapter> adapters = new ArrayList<>(3); public DefaultAdvisorAdapterRegistry() { registerAdvisorAdapter(new MethodBeforeAdviceAdapter()); registerAdvisorAdapter(new AfterReturningAdviceAdapter()); registerAdvisorAdapter(new ThrowsAdviceAdapter()); }
// 先判断类\t\tif (!pc.getClassFilter().matches(targetClass)) {\t\t\treturn false;\t\t}
其他类型
// 找到所有的Advisor\t\tList<Advisor> candidateAdvisors = findCandidateAdvisors();
return advisors.toArray();
继承
ProxyFactory proxyFactory = new ProxyFactory();//spring 生成代理类的工具类
直接调用MethodInterceptor,传入this,在内部会再次调用proceed()方法进行递归 ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this)
if (mm.isRuntime()) 如果该匹配条件是动态的,需要根据运行时参数觉得是否匹配那么需要区分下,包装成特殊的 MethodInterceptorfor (MethodInterceptor interceptor : interceptors) { interceptorList.add(new font color=\"#ff0000\
修改AnnotationAwareAspectJAutoProxyCreator中对应的属性
是
//根据proxyFactory 中的属性判断使用哪种代理方式生成代理类getAopProxyFactory().createAopProxy(this)
JdkDynamicAopProxy.getProxy(classLoader)或CglibAopProxy.getProxy(classLoader)
if (!eligibleAdvisors.isEmpty()) { eligibleAdvisors = sortAdvisors(eligibleAdvisors); }// 对Advisor进行排序,按Ordered接口、@Order注解进行排序
没找到记录当前bean不需要aop
// 将advisors 加入到proxyFactory,后面根据advisors 构建切面逻辑链路proxyFactory.addAdvisors(advisors); proxyFactory.setTargetSource(targetSource);
代理类执行具体方法时执行JdkDynamicAopProxy.invoke()
// 再从所有切面中解析得到Advisor对象 if (this.aspectJAdvisorsBuilder != null) { advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors()); }//解析@Aspectl 注解的类
postProcessAfterInitialization()初始化后
匹配
UserService target = new UserService(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(target); proxyFactory.addAdvisor(new PointcutAdvisor() { @Override public Pointcut getPointcut() { return new Pointcut() { @Override public ClassFilter getClassFilter() { return new ClassFilter() { @Override public boolean matches(Class<?> clazz) { return clazz.getName().equals(\"UserService\
// 被代理对象和代理类 target = targetSource.getTarget();
interceptorList.addAll(Arrays.asList(interceptors));
Interceptor[] interceptors = registry.getInterceptors(advisor);
AnnotationAwareAspectJAutoProxyCreator
advisor instanceof PointcutAdvisor
如果不是动态匹配
// 当前interceptor是InterceptorAndDynamicMethodMatcher,则先进行匹配,匹配成功后再调用该interceptor // 如果没有匹配则递归调用proceed()方法,调用下一个interceptor if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher)
method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)不需要执行下面的aop 逻辑
在判断方法,如果Bean只要有一个方法匹配那就需要aop
ReflectiveMethodInvocation.proceed()
找到了就可以生成代理类
根据bean的信息设置proxyFactory的属性,在后面可以用来判断是jdk的动态代理还是cglb 动态代理
advisor instanceof IntroductionAdvisor
Advice advice = advisor.getAdvice(); if (advice instanceof MethodInterceptor) { interceptors.add((MethodInterceptor) advice); }
createProxy()//生成代理类
//通过上面导入配置类注册BeanDifinitionAspectJAutoProxyRegistrar.registerBeanDefinitions();
// 先找到所有Advisor类型的Bean对象\t\tList<Advisor> advisors = super.findCandidateAdvisors();
// 将Advice适配成MethodInterceptor for (AdvisorAdapter adapter : this.adapters) { if (adapter.supportsAdvice(advice)) { interceptors.add(adapter.getInterceptor(advisor)); } }
for (Advisor advisor : advisors)
//生成代理类proxyFactory.getProxy(classLoader)
// advisedBeans表示已经判断过了的bean,false表示此bean不需要进行Aop if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) { return bean; }
if (specificInterceptors != DO_NOT_PROXY)
// 如果匹配则将Advisor封装成为Interceptor,当前Advisor中的Advice可能即是MethodBeforeAdvice,也是ThrowsAdvice MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
// 子类AnnotationAwareAspectJAutoProxyCreator的findCandidateAdvisors()
AbstractAdvisorAutoProxyCreator
不是
// font color=\"#ff0000\
// 先匹配类\t\t\t\tif (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)
执行初始化后方法
//导入AspectJAutoProxyRegistrar@Import(AspectJAutoProxyRegistrar.class)public @interface EnableAspectJAutoProxy
// currentInterceptorIndex初始值为-1 ,标记当前 执行到advisors 链路的哪个位置 Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
// 从ProxyFactory中拿到所设置的Advice(添加时被封装成了DefaultPointcutAdvisor)\t\t// 添加的时候会控制顺序\t\tAdvisor[] advisors = config.getAdvisors();
// 当调用完了最后一个interceptor后就会执行被代理方法 if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { return invokeJoinpoint(); }
//实现了SmartInstantiationAwareBeanPostProcessorAbstractAutoProxyCreator
if (enableAspectJAutoProxy.getBoolean(\"proxyTargetClass\")) { AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); } if (enableAspectJAutoProxy.getBoolean(\"exposeProxy\")) { AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry); }
AspectJAwareAdvisorAutoProxyCreator
protected boolean isInfrastructureClass(Class<?> beanClass) { boolean retVal = Advice.class.isAssignableFrom(beanClass) || Pointcut.class.isAssignableFrom(beanClass) || Advisor.class.isAssignableFrom(beanClass) || AopInfrastructureBean.class.isAssignableFrom(beanClass);子类可以实现shouldSkip 方法 程序员判断不需要aop的bean
下面以jdk 为例
// 再匹配方法\t\t\t\t\tMethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
if (!NativeDetector.inNativeImage() &&\t\t\t\t(config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)))
结束
1.不是在GraalVM虚拟机上运行2.如果ProxyFactory的isOptimize为true,Spring认为cglib比jdk动态代理要快3.或者isProxyTargetClass为true4. 或者被代理对象没有实现接口5.或者只实现了SpringProxy这个接口满足上述条件会生成cglb 代理类不满足则生成jdk代理类
Interceptor[] interceptors = registry.getInterceptors(advisor);\t\t\t\tinterceptorList.addAll(Arrays.asList(interceptors));
chain.isEmpty()
return DO_NOT_PROXY;
0 条评论
回复 删除
下一页