• spring 系列 7-基于注解的aop


    使用@Component("logger")替换xml中的<bean id="logger" class="com.mantishell.utils.Logger"></bean>
    使用@Aspect替换<aop:config>标签
    使用@Pointcut替换<aop:pointcut>标签
    使用@Before替换<aop:before>
    使用@AfterReturning替换aop:after-returning>
    使用@AfterThrowing替换<aop:after-throwing>
    使用@After替换<aop:after>
    使用@Around替换<aop:around>

    package com.mantishell.utils;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    @Component("logger")
    @Aspect //表示当前类是一个切面类
    public class Logger {
        @Pointcut("execution(* com.mantishell.service.impl.*.*(..))")
        private void pt1(){}
        /**
         * 前置通知
         */
        //@Before("pt1()")
        public  void beforePrintLog(){
            System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
        }
    
        /**
         * 后置通知
         */
        //@AfterReturning("pt1()")
        public  void afterReturningPrintLog(){
            System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
        }
        /**
         * 异常通知
         */
        //@AfterThrowing("pt1()")
        public  void afterThrowingPrintLog(){
            System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
        }
    
        /**
         * 最终通知
         */
        //@After("pt1()")
        public  void afterPrintLog(){
            System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
        }
    
        @Around("pt1()")
        public Object aroundPringLog(ProceedingJoinPoint pjp){
            Object rtValue = null;
            try{
                Object[] args = pjp.getArgs();//得到方法执行所需的参数
    
                System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");
    
                rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
    
                System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");
    
                return rtValue;
            }catch (Throwable t){
                System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
                throw new RuntimeException(t);
            }finally {
                System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
            }
        }
    }
    
    

    使用环绕通知的话,执行的顺序可控:
    Logger类中的aroundPringLog方法开始记录日志了。。。前置
    Logger类中的aroundPringLog方法开始记录日志了。。。后置
    Logger类中的aroundPringLog方法开始记录日志了。。。最终

    使用前置后置注解的话,执行顺序不被控制:
    前置通知Logger类中的beforePrintLog方法开始记录日志了。。。
    最终通知Logger类中的afterPrintLog方法开始记录日志了。。。
    后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。

    所以最好还是使用环绕通知。

  • 相关阅读:
    [转]Win7 64位操作系统下配置PHP+MySql+Apache环境
    [转]你必须懂的 T4 模板:深入浅出
    [转]C#反射机制介绍
    [转]RDLC报表,纯文字内容,动态数据源 解决方案
    [转]关于C# 中的Attribute 特性
    [转]MVC中如何使用RDLC报表
    [转]关于用netbeans和xdebug调试php的配置
    1308论文相关时间
    [转].net 使用NPOI或MyXls把DataTable导出到Excel
    [转]MySQL安装总结:Start service没有响应(Win7 64位) 服务启动失败 1067 错误
  • 原文地址:https://www.cnblogs.com/mantishell/p/12670214.html
Copyright © 2020-2023  润新知