spring-aop(二)学习笔记
阅读原文时间:2023年07月10日阅读:1

常用增强处理类型

增强处理类型

                                                       特点

before

前置增强处理,在目标方法前织入增强处理

AfterReturning

后置增强处理,在目标方法正常执行(不出现异常)后织入增强处理

AfterThrowing

异常增强处理,在目标方法抛出异常后织入增强处理

After

最终增强处理,不论方法是否抛出异常,都是会在目标方法最后织入增强处理

Around 

环绕增强处理,在目标方法的前后都可以织入增强处理,同时可以控制目标方法的执行及返回值的再加工处理

环绕增强: 相当于拦截器,可以控制目标方法的执行,可以对目标方法的返回值再加工

  • Around Advice 的第一个参数必须是ProceedingJoinPoint类型,

  • 调用ProceedingJoinPoint类的proceed()方法来控制目标方法是否执行,

    • proceed()方法可以传入一个Object的数组,当方法执行时,数组内的值会当作参数传入

    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
    // start stopwatch
    Object retVal = pjp.proceed();
    // stop stopwatch
    return retVal;
    }

  • xml

    <aop:around
        pointcut-ref="businessService"
        method="doBasicProfiling"/>
    
    ...

  • 控制目标方法是否执行

    package x.y;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.springframework.util.StopWatch;

    public class SimpleProfiler {

    public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
        StopWatch clock = new StopWatch("Profiling for '" + name + "' and '" + age + "'");
        try {
            clock.start(call.toShortString());
            return call.proceed();
        } finally {
            clock.stop();
            System.out.println(clock.prettyPrint());
        }
    }

    }

  • xml

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- this is the object that will be proxied by Spring's AOP infrastructure -->
    <bean id="personService" class="x.y.service.DefaultPersonService"/>
    
    <!-- this is the actual advice itself -->
    <bean id="profiler" class="x.y.SimpleProfiler"/>
    
    <aop:config>
        <aop:aspect ref="profiler">
        &lt;aop:pointcut id="theExecutionOfSomePersonServiceMethod"
            expression="execution(* x.y.service.PersonService.getPerson(String,int))
            and args(name, age)"/&gt;
    
        &lt;aop:around pointcut-ref="theExecutionOfSomePersonServiceMethod"
            method="profile"/&gt;
    
    &lt;/aop:aspect&gt;
    </aop:config>

After(Finally) Advice 不管方法是否执行完成,After Advice都会执行,和Finally一样

<aop:aspect id="afterFinallyExample" ref="aBean">

    <aop:after
        pointcut-ref="dataAccessOperation"
        method="doReleaseLock"/>

    ...

</aop:aspect>

After Throwing Advice 当执行目标方法抛出异常时,使用After Throwing Advice

  • 注意:是throwing Exception,try…catch是不起作用的

    <aop:after-throwing
        pointcut-ref="dataAccessOperation"
        method="doRecoveryActions"/>
    
    ...

  • 还可通过throwing属性指定异常信息的接收对象

    <aop:after-throwing
        pointcut-ref="dataAccessOperation"
        throwing="dataAccessEx"
        method="doRecoveryActions"/>
    
    ...

参考资料:

https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#aop-schema-advice-after-throwing

来自为知笔记(Wiz)