Spring AOP实现原理

Spring AOP(Aspect-Oriented Programming)实现原理主要基于代理模式。在Spring AOP中,会根据配置动态地在运行时创建代理对象,用来包裹目标对象。根据代理的类型,Spring AOP使用JDK动态代理或CGLIB来创建这些代理。

AOP主要概念

  1. Aspect: 定义跨多个类的行为,通常包含多个通知(Advices)。
  2. Join Point: 方法执行过程中的某个特定点,比如方法调用前、调用后、抛出异常后等。
  3. Advice: 定义在特定的join point上需要执行的动作,比如before, after, around等类型的advice。
  4. Pointcut: 定义哪些join points匹配通知(advice),它是一组表达式。
  5. Target Object: 被一个或多个aspects通知的对象。
  6. AOP Proxy: AOP框架为目标对象创建的代理,用于拦截某些方法调用,并提供通知的执行。

Spring AOP实现原理

在Spring中,AOP代理的创建过程主要由ProxyFactory类处理:

  1. 确定代理模式: 如果目标类实现了至少一个接口,则默认使用JDK动态代理,否则使用CGLIB。
  2. 收集advice: Spring会查找与目标方法匹配的advice(通过Pointcut定义)。
  3. 创建代理对象: 根据advice和目标对象创建代理实例。
  4. 代理使用: 当调用代理上的方法时,代理会根据advice执行相关的通知逻辑。

源码解析

让我们通过源码解析,看一下Spring是如何创建AOP代理的。

创建AOP代理

AbstractAutoProxyCreator类中有创建AOP代理的逻辑:

protected Object createProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {
    if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
        AutoProxyUtils.exposeTargetClass(this.beanFactory, beanName, beanClass);
    }

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.copyFrom(this);

    if (!proxyFactory.isProxyTargetClass()) {
        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);
    }

    return proxyFactory.getProxy(getProxyClassLoader());
}
进行方法拦截

当代理对象的方法被调用时,方法拦截的逻辑是通过ReflectiveMethodInvocation来处理的:

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;
        if (dm.methodMatcher.matches(this.method, this.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);
    }
}
Advice的执行

以上proceed()方法确保了各种advice按照设定的顺序执行。对于@Around@Before@After@AfterReturning@AfterThrowing等,都是通过相应的Advice实现类来实现具体的拦截逻辑。

代码演示

下面是一个使用Spring AOP的例子。我们定义一个简单的服务类和一个切面:

@Service
public class SimpleService {

    public void performOperation() {
        // 模拟方法执行
        System.out.println("Performing operation...");
    }
}

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* SimpleService.performOperation(..))")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Logging before operation: " + joinPoint.getSignature().getName());
    }
}

在这个例子中,LoggingAspect定义了一个@Before通知,它会在SimpleService.performOperation()方法执行前打印一条日志。

总结

Spring AOP是基于代理模式的,它在运行时动态地创建代理对象,并根据定义的切面和通知来增加额外的行为。通过使用JDK动态代理或CGLIB,Spring能够将通知应用到目标对象的方法上。深入理解和使用Spring AOP能够让您在不改变现有业务逻辑代码的情况下,增加如日志、事务管理、安全控制等横切关注点。

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辞暮尔尔-烟火年年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值