
离线 TA的专栏
- 打卡等级:无名新人
- 打卡总天数:1
- 打卡月天数:0
- 打卡总奖励:18
- 最近打卡:2024-01-20 00:17:53
|
目录- aop动态代理源码剖析
- 创建动态代理底层逻辑createProxy()
- 使用jdk的方式创建动态代理
- 使用cglib的方式创建动态代理
aop动态代理源码剖析
aop增强逻辑的执行时机是在initializeBean方法中
- protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
- if (beanName.indexOf("my") >= 0) {
- System.out.println("============= [initializeBean] beanName=" + beanName + " =============");
- }
- if (System.getSecurityManager() != null) {
- AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
- invokeAwareMethods(beanName, bean);
- return null;
- }, getAccessControlContext());
- }
- else {
- invokeAwareMethods(beanName, bean);
- }
- Object wrappedBean = bean;
- if (mbd == null || !mbd.isSynthetic()) {
- wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
- }
- try {
- invokeInitMethods(beanName, wrappedBean, mbd);
- }
- catch (Throwable ex) {
- throw new BeanCreationException(
- (mbd != null ? mbd.getResourceDescription() : null),
- beanName, "Invocation of init method failed", ex);
- }
- if (mbd == null || !mbd.isSynthetic()) {
- // aop增强逻辑的执行时机
- // 说白了就是在BeanPostProcessor#postProcessAfterInitialization
- wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
- }
- return wrappedBean;
- }
复制代码
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization
- @Override
- public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
- if (bean != null) {
- Object cacheKey = getCacheKey(bean.getClass(), beanName);
- if (this.earlyProxyReferences.remove(cacheKey) != bean) {
- // 如果它有资格被代理
- return wrapIfNecessary(bean, beanName, cacheKey);
- }
- }
- return bean;
- }
复制代码- protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
- // 以下三种情况,判断,如果不需要增强,就直接返回
- if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
- return bean;
- }
- if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
- return bean;
- }
- // 如果是 基础设施类(Pointcut、Advice、Advisor 等) 或 需要 skip, 则不需要增强
- if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
- this.advisedBeans.put(cacheKey, Boolean.FALSE);
- return bean;
- }
- // Create proxy if we have advice.
- // 为目标 bean 查找合适的通知器,并封装成一个排好顺序的List<Advisor>集合
- // 底层是基于拓扑排序
- Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
- if (specificInterceptors != DO_NOT_PROXY) {
- this.advisedBeans.put(cacheKey, Boolean.TRUE);
- // 创建代理
- 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;
- }
复制代码- protected boolean isInfrastructureClass(Class<?> beanClass) {
- // 下面这些是基础设施类,说白了就是aop的一些配置类,是不需要被增强的
- boolean retVal = Advice.class.isAssignableFrom(beanClass) ||
- Pointcut.class.isAssignableFrom(beanClass) ||
- Advisor.class.isAssignableFrom(beanClass) ||
- AopInfrastructureBean.class.isAssignableFrom(beanClass);
- if (retVal && logger.isTraceEnabled()) {
- logger.trace("Did not attempt to auto-proxy infrastructure class [" + beanClass.getName() + "]");
- }
- return retVal;
- }
复制代码
创建动态代理底层逻辑createProxy()
- 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);
- }
- // 通过代理工厂创建代理
- ProxyFactory proxyFactory = new ProxyFactory();
- proxyFactory.copyFrom(this);
- // 设置代理模式
- // 在EnableAspectJAutoProxy注解中,有一个属性proxyTargetClass
- // boolean proxyTargetClass() default false;
- // 表示默认使用的是jdk动态代理,如果设置为true则会使用cglib动态代理
- if (proxyFactory.isProxyTargetClass()) {
- // Explicit handling of JDK proxy targets and lambdas (for introduction advice scenarios)
- if (Proxy.isProxyClass(beanClass) || ClassUtils.isLambdaClass(beanClass)) {
- // Must allow for introductions; can't just set interfaces to the proxy's interfaces only.
- for (Class<?> ifc : beanClass.getInterfaces()) {
- proxyFactory.addInterface(ifc);
- }
- }
- }
- else {
- // No proxyTargetClass flag enforced, let's apply our default checks...
- // 如果没有接口,则会赋值为true,强制使用cglib动态代理
- if (shouldProxyTargetClass(beanClass, beanName)) {
- proxyFactory.setProxyTargetClass(true);
- }
- else {
- evaluateProxyInterfaces(beanClass, proxyFactory);
- }
- }
- Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
- proxyFactory.addAdvisors(advisors);
- proxyFactory.setTargetSource(targetSource);
- customizeProxyFactory(proxyFactory);
- proxyFactory.setFrozen(this.freezeProxy);
- if (advisorsPreFiltered()) {
- proxyFactory.setPreFiltered(true);
- }
- // Use original ClassLoader if bean class not locally loaded in overriding class loader
- ClassLoader classLoader = getProxyClassLoader();
- if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) {
- classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
- }
- // 使用工厂模式真正创建动态代理
- return proxyFactory.getProxy(classLoader);
- }
复制代码
proxyFactory.getProxy(classLoader);底层逻辑
- public Object getProxy(@Nullable ClassLoader classLoader) {
- // 创建 AopProxy 对象
- // 调用 AopProxy.getProxy 创建代理对象
- return createAopProxy().getProxy(classLoader);
- }
复制代码- @Override
- public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
- if (!NativeDetector.inNativeImage() &&
- (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) || ClassUtils.isLambdaClass(targetClass)) {
- return new JdkDynamicAopProxy(config);// 使用jdk创建动态代理
- }
- return new ObjenesisCglibAopProxy(config); // 使用cglib创建动态代理
- }
- else {
- return new JdkDynamicAopProxy(config);// 使用jdk创建动态代理
- }
- }
复制代码
使用jdk的方式创建动态代理
org.springframework.aop.framework.JdkDynamicAopProxy#getProxy(java.lang.ClassLoader)
- @Override
- public Object getProxy(@Nullable ClassLoader classLoader) {
- if (logger.isTraceEnabled()) {
- logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());
- }
- return Proxy.newProxyInstance(classLoader, this.proxiedInterfaces, this);
- }
复制代码
因为jdk动态代理的最重要的方法是java.lang.reflect.InvocationHandler#invoke
重点看该类中的invoke方法
- @Override
- @Nullable
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- Object oldProxy = null;
- boolean setProxyContext = false;
- TargetSource targetSource = this.advised.targetSource;
- Object target = null;
- try {
- if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
- // The target does not implement the equals(Object) method itself.
- return equals(args[0]);
- }
- else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
- // The target does not implement the hashCode() method itself.
- return hashCode();
- }
- else if (method.getDeclaringClass() == DecoratingProxy.class) {
- // There is only getDecoratedClass() declared -> dispatch to proxy config.
- return AopProxyUtils.ultimateTargetClass(this.advised);
- }
- else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
- method.getDeclaringClass().isAssignableFrom(Advised.class)) {
- // Service invocations on ProxyConfig with the proxy config...
- return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
- }
- Object retVal;
- if (this.advised.exposeProxy) {
- // Make invocation available if necessary.
- oldProxy = AopContext.setCurrentProxy(proxy);
- setProxyContext = true;
- }
- // Get as late as possible to minimize the time we "own" the target,
- // in case it comes from a pool.
- target = targetSource.getTarget();
- Class<?> targetClass = (target != null ? target.getClass() : null);
- // Get the interception chain for this method.
- // 获取到所有的通知拦截器
- // 0 = {ExposeInvocationInterceptor}
- // 1 = {AspectJAroundAdvice}
- // 2 = {MethodBeforeAdviceInterceptor}
- // 3 = {AspectJAfterAdvice}
- // 4 = {AfterReturningAdviceInterceptor}
- // 5 = {AspectJAfterThrowingAdvice}
- List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
- // Check whether we have any advice. If we don't, we can fallback on direct
- // reflective invocation of the target, and avoid creating a MethodInvocation.
- if (chain.isEmpty()) {
- // We can skip creating a MethodInvocation: just invoke the target directly
- // Note that the final invoker must be an InvokerInterceptor so we know it does
- // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
- // 如果没有拦截器,则直接调用目标方法
- Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
- retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
- }
- else {
- // We need to create a method invocation...
- // 创建一个方法调用器,将拦截器链传入
- MethodInvocation invocation =
- new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
- // Proceed to the joinpoint through the interceptor 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())) {
- // Special case: it returned "this" and the return type of the method
- // is type-compatible. Note that we can't help if the target sets
- // a reference to itself in another returned object.
- retVal = proxy;
- }
- else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
- throw new AopInvocationException(
- "Null return value from advice does not match primitive return type for: " + method);
- }
- return retVal;
- }
- finally {
- if (target != null && !targetSource.isStatic()) {
- // Must have come from TargetSource.
- targetSource.releaseTarget(target);
- }
- if (setProxyContext) {
- // Restore old proxy.
- AopContext.setCurrentProxy(oldProxy);
- }
- }
- }
复制代码
org.springframework.aop.framework.ReflectiveMethodInvocation#proceed
- /**
- * Index from 0 of the current interceptor we're invoking.
- * -1 until we invoke: then the current interceptor.
- */
- // 我们正在调用的拦截器的索引
- private int currentInterceptorIndex = -1;
- @Override
- @Nullable
- public Object proceed() throws Throwable {
- // We start with an index of -1 and increment early.
- // 也就是说当所有拦截器都执行完成后,执行目标方法
- if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
- return invokeJoinpoint(); // 目标方法的执行
- }
- // interceptorsAndDynamicMethodMatchers表示我们通过构造方法传过来的拦截器
- // interceptorOrInterceptionAdvice:渠道的拦截器(或者叫做切面逻辑)
- Object interceptorOrInterceptionAdvice =
- this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
- if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
- // Evaluate dynamic method matcher here: static part will already have
- // been evaluated and found to match.
- 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 {
- // Dynamic matching failed.
- // Skip this interceptor and invoke the next in the chain.
- return proceed();
- }
- }
- else {
- // It's an interceptor, so we just invoke it: The pointcut will have
- // been evaluated statically before this object was constructed.
- // 通过不同的拦截器完成切面逻辑
- // 也就是说执行该拦截器的切面逻辑
- return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
- }
- }
复制代码
解下来看看这6个切面的执行逻辑: // 0 = {ExposeInvocationInterceptor} // 1 = {AspectJAroundAdvice} // 2 = {MethodBeforeAdviceInterceptor} // 3 = {AspectJAfterAdvice} // 4 = {AfterReturningAdviceInterceptor} // 5 = {AspectJAfterThrowingAdvice}
org.springframework.aop.interceptor.ExposeInvocationInterceptor#invoke
- @Override
- @Nullable
- public Object invoke(MethodInvocation mi) throws Throwable {
- MethodInvocation oldInvocation = invocation.get();
- invocation.set(mi);
- try {
- return mi.proceed(); // 没有任何执行逻辑,直接调用目标方法
- }
- finally {
- invocation.set(oldInvocation);
- }
- }
复制代码
org.springframework.aop.aspectj.AspectJAroundAdvice#invoke
- @Override
- @Nullable
- 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); // 直接调用自定义的逻辑
- }
复制代码
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor#invoke
- @Override
- @Nullable
- public Object invoke(MethodInvocation mi) throws Throwable {
- // 先调用自定的before方法
- this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
- // 在调用目标方法
- return mi.proceed();
- }
复制代码
org.springframework.aop.aspectj.AspectJAfterAdvice#invoke
- @Override
- @Nullable
- public Object invoke(MethodInvocation mi) throws Throwable {
- try {
- return mi.proceed(); // 先调用目标方法
- }
- finally {
- // 再调用自定义的after方法
- // 这个方法放在了finally块中,不然会执行
- invokeAdviceMethod(getJoinPointMatch(), null, null);
- }
- }
复制代码
org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor#invoke
- @Override
- @Nullable
- public Object invoke(MethodInvocation mi) throws Throwable {
- // 先执行目标方法
- Object retVal = mi.proceed();
- // 没有异常的情况下,再执行自定义的afterReturning逻辑,
- this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
- return retVal;
- }
复制代码
org.springframework.aop.aspectj.AspectJAfterThrowingAdvice#invoke
- @Override
- @Nullable
- public Object invoke(MethodInvocation mi) throws Throwable {
- try {
- // 先执行目标方法
- return mi.proceed();
- }
- catch (Throwable ex) {
- if (shouldInvokeOnThrowing(ex)) {
- // 如果出现异常的时候,执行自定义的afterThrowing方法
- invokeAdviceMethod(getJoinPointMatch(), null, ex);
- }
- throw ex;
- }
- }
复制代码
自己最好debug看看这样理解的更加深刻
使用cglib的方式创建动态代理
cglib动态代理主要是Enhancer,是创建目标类的子类来完成的动态代理。
重点关注:intercept方法
org.springframework.aop.framework.CglibAopProxy#getProxy(java.lang.ClassLoader)
- @Override
- public Object getProxy(@Nullable ClassLoader classLoader) {
- if (logger.isTraceEnabled()) {
- logger.trace("Creating CGLIB proxy: " + this.advised.getTargetSource());
- }
- try {
- Class<?> rootClass = this.advised.getTargetClass();
- Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");
- Class<?> proxySuperClass = rootClass;
- if (rootClass.getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)) {
- proxySuperClass = rootClass.getSuperclass();
- Class<?>[] additionalInterfaces = rootClass.getInterfaces();
- for (Class<?> additionalInterface : additionalInterfaces) {
- this.advised.addInterface(additionalInterface);
- }
- }
- // Validate the class, writing log messages as necessary.
- validateClassIfNecessary(proxySuperClass, classLoader);
- // 配置 CGLIB 增强器...
- // Configure CGLIB Enhancer...
- Enhancer enhancer = createEnhancer();
- if (classLoader != null) {
- enhancer.setClassLoader(classLoader);
- if (classLoader instanceof SmartClassLoader &&
- ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
- enhancer.setUseCache(false);
- }
- }
- // 目标代理类 class com.coding.spring.aop.bean.MyAOPBean
- enhancer.setSuperclass(proxySuperClass);
- enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
- enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
- enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(classLoader));
- // 0 = DynamicAdvisedInterceptor
- // 1 = StaticUnadvisedInterceptor
- // 2 = SerializableNoOp
- // 3 = StaticDispatcher
- // 4 = AdvisedDispatcher
- // 5 = EqualsInterceptor
- // 6 = HashCodeInterceptor
- Callback[] callbacks = getCallbacks(rootClass);
- Class<?>[] types = new Class<?>[callbacks.length];
- for (int x = 0; x < types.length; x++) {
- types[x] = callbacks[x].getClass();
- }
- // fixedInterceptorMap only populated at this point, after getCallbacks call above
- enhancer.setCallbackFilter(new ProxyCallbackFilter(
- this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
- enhancer.setCallbackTypes(types);
- // 生成代理类并创建代理实例。
- // Generate the proxy class and create a proxy instance.
- return createProxyClassAndInstance(enhancer, callbacks);
- }
- catch (CodeGenerationException | IllegalArgumentException ex) {
- throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() +
- ": Common causes of this problem include using a final class or a non-visible class",
- ex);
- }
- catch (Throwable ex) {
- // TargetSource.getTarget() failed
- throw new AopConfigException("Unexpected AOP exception", ex);
- }
- }
复制代码- private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
- // Parameters used for optimization choices...
- boolean exposeProxy = this.advised.isExposeProxy();
- boolean isFrozen = this.advised.isFrozen();
- boolean isStatic = this.advised.getTargetSource().isStatic();
- // Choose an "aop" interceptor (used for AOP calls).
- // AOP 回调接口 DynamicAdvisedInterceptor
- // private static class DynamicAdvisedInterceptor implements MethodInterceptor, Serializable {
- Callback aopInterceptor = new DynamicAdvisedInterceptor(this.advised);
- // Choose a "straight to target" interceptor. (used for calls that are
- // unadvised but can return this). May be required to expose the proxy.
- Callback targetInterceptor;
- if (exposeProxy) {
- targetInterceptor = (isStatic ?
- new StaticUnadvisedExposedInterceptor(this.advised.getTargetSource().getTarget()) :
- new DynamicUnadvisedExposedInterceptor(this.advised.getTargetSource()));
- }
- else {
- targetInterceptor = (isStatic ?
- new StaticUnadvisedInterceptor(this.advised.getTargetSource().getTarget()) :
- new DynamicUnadvisedInterceptor(this.advised.getTargetSource()));
- }
- // Choose a "direct to target" dispatcher (used for
- // unadvised calls to static targets that cannot return this).
- Callback targetDispatcher = (isStatic ?
- new StaticDispatcher(this.advised.getTargetSource().getTarget()) : new SerializableNoOp());
- Callback[] mainCallbacks = new Callback[] {
- aopInterceptor, // for normal advice
- targetInterceptor, // invoke target without considering advice, if optimized
- new SerializableNoOp(), // no override for methods mapped to this
- targetDispatcher, this.advisedDispatcher,
- new EqualsInterceptor(this.advised),
- new HashCodeInterceptor(this.advised)
- };
- Callback[] callbacks;
- // If the target is a static one and the advice chain is frozen,
- // then we can make some optimizations by sending the AOP calls
- // direct to the target using the fixed chain for that method.
- if (isStatic && isFrozen) {
- Method[] methods = rootClass.getMethods();
- Callback[] fixedCallbacks = new Callback[methods.length];
- this.fixedInterceptorMap = CollectionUtils.newHashMap(methods.length);
- // TODO: small memory optimization here (can skip creation for methods with no advice)
- for (int x = 0; x < methods.length; x++) {
- Method method = methods[x];
- List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, rootClass);
- fixedCallbacks[x] = new FixedChainStaticTargetInterceptor(
- chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass());
- this.fixedInterceptorMap.put(method, x);
- }
- // Now copy both the callbacks from mainCallbacks
- // and fixedCallbacks into the callbacks array.
- callbacks = new Callback[mainCallbacks.length + fixedCallbacks.length];
- System.arraycopy(mainCallbacks, 0, callbacks, 0, mainCallbacks.length);
- System.arraycopy(fixedCallbacks, 0, callbacks, mainCallbacks.length, fixedCallbacks.length);
- this.fixedInterceptorOffset = mainCallbacks.length;
- }
- else {
- callbacks = mainCallbacks;
- }
- return callbacks;
- }
复制代码- // 可以看到MethodInterceptor实现了Callback接口
- public interface MethodInterceptor extends Callback {
- Object intercept(Object var1, Method var2, Object[] var3, MethodProxy var4) throws Throwable;
- }
复制代码- /**
- * General purpose AOP callback. Used when the target is dynamic or when the
- * proxy is not frozen.
- */
- private static class DynamicAdvisedInterceptor implements MethodInterceptor, Serializable {
- private final AdvisedSupport advised;
- public DynamicAdvisedInterceptor(AdvisedSupport advised) {
- this.advised = advised;
- }
- // CglibAopProxy.DynamicAdvisedInterceptor.intercept
- // 最终创建动态代理之后,会调用到这个intercept方法
- @Override
- @Nullable
- public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
- Object oldProxy = null;
- boolean setProxyContext = false;
- Object target = null;
- TargetSource targetSource = this.advised.getTargetSource();
- try {
- if (this.advised.exposeProxy) {
- // Make invocation available if necessary.
- oldProxy = AopContext.setCurrentProxy(proxy);
- setProxyContext = true;
- }
- // Get as late as possible to minimize the time we "own" the target, in case it comes from a pool...
- target = targetSource.getTarget();
- Class<?> targetClass = (target != null ? target.getClass() : null);
- // 0 = {ExposeInvocationInterceptor}
- // 1 = {AspectJAroundAdvice}
- // 2 = {MethodBeforeAdviceInterceptor}
- // 3 = {AspectJAfterAdvice}
- // 4 = {AfterReturningAdviceInterceptor}
- // 5 = {AspectJAfterThrowingAdvice}
- // 获取拦截器链
- List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
- Object retVal;
- // Check whether we only have one InvokerInterceptor: that is,
- // no real advice, but just reflective invocation of the target.
- if (chain.isEmpty() && CglibMethodInvocation.isMethodProxyCompatible(method)) {
- // We can skip creating a MethodInvocation: just invoke the target directly.
- // Note that the final invoker must be an InvokerInterceptor, so we know
- // it does nothing but a reflective operation on the target, and no hot
- // swapping or fancy proxying.
- Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
- retVal = invokeMethod(target, method, argsToUse, methodProxy);
- }
- else {
- // We need to create a method invocation...
- // 创建 CglibMethodInvocation 进行proceed()方法调用
- retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
- }
- retVal = processReturnType(proxy, target, method, retVal);
- return retVal;
- }
- finally {
- if (target != null && !targetSource.isStatic()) {
- targetSource.releaseTarget(target);
- }
- if (setProxyContext) {
- // Restore old proxy.
- AopContext.setCurrentProxy(oldProxy);
- }
- }
- }
- @Override
- public boolean equals(@Nullable Object other) {
- return (this == other ||
- (other instanceof DynamicAdvisedInterceptor &&
- this.advised.equals(((DynamicAdvisedInterceptor) other).advised)));
- }
- /**
- * CGLIB uses this to drive proxy creation.
- */
- @Override
- public int hashCode() {
- return this.advised.hashCode();
- }
- }
复制代码
org.springframework.aop.framework.CglibAopProxy.CglibMethodInvocation#proceed
- @Override
- @Nullable
- public Object proceed() throws Throwable {
- try {
- // 调用 ReflectiveMethodInvocation.proceed
- return super.proceed();
- }
- catch (RuntimeException ex) {
- throw ex;
- }
- catch (Exception ex) {
- if (ReflectionUtils.declaresException(getMethod(), ex.getClass()) ||
- KotlinDetector.isKotlinType(getMethod().getDeclaringClass())) {
- // Propagate original exception if declared on the target method
- // (with callers expecting it). Always propagate it for Kotlin code
- // since checked exceptions do not have to be explicitly declared there.
- throw ex;
- }
- else {
- // Checked exception thrown in the interceptor but not declared on the
- // target method signature -> apply an UndeclaredThrowableException,
- // aligned with standard JDK dynamic proxy behavior.
- throw new UndeclaredThrowableException(ex);
- }
- }
- }
复制代码
org.springframework.aop.framework.ObjenesisCglibAopProxy#createProxyClassAndInstance
- @Override
- protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
- Class<?> proxyClass = enhancer.createClass();
- Object proxyInstance = null;
- if (objenesis.isWorthTrying()) {
- try {
- // 创建代理类实例
- proxyInstance = objenesis.newInstance(proxyClass, enhancer.getUseCache());
- }
- catch (Throwable ex) {
- logger.debug("Unable to instantiate proxy using Objenesis, " +
- "falling back to regular proxy construction", ex);
- }
- }
- if (proxyInstance == null) {
- // Regular instantiation via default constructor...
- try {
- Constructor<?> ctor = (this.constructorArgs != null ?
- proxyClass.getDeclaredConstructor(this.constructorArgTypes) :
- proxyClass.getDeclaredConstructor());
- ReflectionUtils.makeAccessible(ctor);
- proxyInstance = (this.constructorArgs != null ?
- ctor.newInstance(this.constructorArgs) : ctor.newInstance());
- }
- catch (Throwable ex) {
- throw new AopConfigException("Unable to instantiate proxy using Objenesis, " +
- "and regular proxy instantiation via default constructor fails as well", ex);
- }
- }
- // 设置 callBack 回调接口
- ((Factory) proxyInstance).setCallbacks(callbacks);
- return proxyInstance;
- }
复制代码
org.springframework.aop.framework.ReflectiveMethodInvocation#proceed
- @Override
- @Nullable
- public Object proceed() throws Throwable {
- // We start with an index of -1 and increment early.
- // 所有拦截器都执行完成后,执行目标方法
- if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
- return invokeJoinpoint();
- }
- Object interceptorOrInterceptionAdvice =
- this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
- if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
- // Evaluate dynamic method matcher here: static part will already have
- // been evaluated and found to match.
- 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 {
- // Dynamic matching failed.
- // Skip this interceptor and invoke the next in the chain.
- return proceed();
- }
- }
- else {
- // It's an interceptor, so we just invoke it: The pointcut will have
- // been evaluated statically before this object was constructed.
- // 通过不同的拦截器完成切面逻辑
- // 看到这里就和jdk动态代理的逻辑是一样的了
- return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
- }
- }
复制代码
后面的执行逻辑就和上面jdk动态代理的执行逻辑一样了。
到此这篇关于spring aop底层源码执行逻辑剖析的文章就介绍到这了,更多相关spring aop底层源码内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯! 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:  进行删除处理。
4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
|