zoukankan      html  css  js  c++  java
  • spring-aop(二)学习笔记

    常用增强处理类型

    增强处理类型                                                       特点
    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:aspect id="aroundExample" ref="aBean">
    
        <aop:around
            pointcut-ref="businessService"
            method="doBasicProfiling"/>
    
        ...
    
    </aop:aspect>
    
    • 控制目标方法是否执行
    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
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            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">
    
                <aop:pointcut id="theExecutionOfSomePersonServiceMethod"
                    expression="execution(* x.y.service.PersonService.getPerson(String,int))
                    and args(name, age)"/>
    
                <aop:around pointcut-ref="theExecutionOfSomePersonServiceMethod"
                    method="profile"/>
    
            </aop:aspect>
        </aop:config>
    
    </beans>
    
    

    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:aspect id="afterThrowingExample" ref="aBean">
    
        <aop:after-throwing
            pointcut-ref="dataAccessOperation"
            method="doRecoveryActions"/>
    
        ...
    
    </aop:aspect>
    
    
    • 还可通过throwing属性指定异常信息的接收对象
    <aop:aspect id="afterThrowingExample" ref="aBean">
    
        <aop:after-throwing
            pointcut-ref="dataAccessOperation"
            throwing="dataAccessEx"
            method="doRecoveryActions"/>
    
        ...
    
    </aop:aspect>
    

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





  • 相关阅读:
    30.过滤掉smb.conf配置文件中的空行和注释行和空白行(初级写法很不严谨)
    29. 分析文件内容,判断 fgets 共执行了多少次?
    28. 使用fgetc()/fputc()实现文件的加密与解密,存在溢出风险。
    27.读文件时通过两种方式判断文件结尾
    26. 使用fgetc()/fputc()实现文件的拷贝
    24. 练习定义几种指针及数组
    23. 实现 func()函数,在func()中,通过操作arr,实现修改str1到str2字符串,并打印所有字符串,考察对指针与数组与字符串的基本掌握
    Windows程序设计(Charles Petzold)HELLOWIN程序实现
    jvm 解释器和JIT编译器
    java 创建线程的方法
  • 原文地址:https://www.cnblogs.com/baiyifengyun/p/13779978.html
Copyright © 2011-2022 走看看