zoukankan      html  css  js  c++  java
  • Spring源码窥探之:AOP注解

    AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么。在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@AfterReturning,@AfterThrowing,@Around,@PointCut以及@EnableAspectJAutoProxy

    1. 模拟业务类CalculateService

    /**
     * description:模拟业务类
     *
     * @author 70KG
     * @date 2018/12/17
     */
    public class CalculateService {
    
        public int calculate(int i, int j) {
            int result = i / j;
            System.out.println("---->CalculateService-业务方法执行。。。。。。");
            return result;
        }
    
    }

    2. 编写切面类,切面类必须标注它为切面类(@Aspect),关于切入点表达式,可以参考官方文档,写法很多种,还有关于JoinPoint这个参数必须放在第一位,否则报错。

    /**
     * description:日志切面类,JoinPoint必须在参数的第一位
     *
     * @author 70KG
     * @date 2018/12/17
     */
    @Aspect // ---> 声明此类是一个切面类
    public class LogAdvice {
    
        @Pointcut("execution(* com.nmys.story.springCore.springaop.sample01.CalculateService.*(..))")
        public void pointCut() {
        }
    
        /**
         * 目标方法执行前执行
         */
        @Before("pointCut()")
        public void logBefore(JoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            Object[] args = joinPoint.getArgs();
            System.out.println("---->" + signature + "方法执行前,传入的参数是:" + Arrays.asList(args));
        }
    
        /**
         * 目标方法执行后执行
         */
        @After("pointCut()")
        public void logAfter(JoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            Object[] args = joinPoint.getArgs();
            System.out.println("---->" + signature + "方法执行后,传入的参数是:" + Arrays.asList(args));
    
        }
    
        /**
         * 方法发生异常时执行
         */
        @AfterThrowing(value = "pointCut()", throwing = "ex")
        public void logException(JoinPoint joinPoint, Exception ex) {
            Signature signature = joinPoint.getSignature();
            System.out.println("---->" + signature + "方法执行后,抛出了异常信息:" + ex.getMessage());
        }
    
    
        /**
         * 正常返回时执行
         */
        @AfterReturning(value = "pointCut()", returning = "result")
        public void logReturn(JoinPoint joinPoint, Object result) {
            Signature signature = joinPoint.getSignature();
            System.out.println("---->" + signature + "方法执行后,返回的结果是:" + result);
        }
    
    }

    3. 配置类

    /**
     * description
     *
     * @author 70KG
     * @date 2018/12/17
     */
    @Configuration
    @EnableAspectJAutoProxy // ---> 开启切面的自动代理
    public class MyConfig {
    
        @Bean
        public CalculateService calculateService() {
            return new CalculateService();
        }
    
        // 将切面类也交给容器管理
        @Bean
        public LogAdvice logAdvice() {
            return new LogAdvice();
        }
    
    }

    4. 测试类

    /**
     * description
     *
     * @author 70KG
     * @date 2018/12/17
     */
    public class Test01 {
    
        public static void main(String[] args) {
    
            // 加载配置文件
            AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MyConfig.class);
            CalculateService calc = (CalculateService) ac.getBean("calculateService");
            calc.calculate(4,2);
        }
    
    }

    5. 结果

    ---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行前,传入的参数是:[4, 2]
    ---->CalculateService-业务方法执行。。。。。。
    ---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,传入的参数是:[4, 2]
    ---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,返回的结果是:2
  • 相关阅读:
    centos7.4 系统安装指导
    win10下硬盘安装CentOS7
    CentOs7.X下配置FTP
    pyspider 安装使用过程的一些坑
    .Net Core 商城微服务项目系列(十三):搭建Log4net+ELK+Kafka日志框架
    .Net Core自动化部署系列(二):使用Jenkins打造镜像发布流水线
    Kubernetes 系列(六):Kubernetes部署Prometheus监控
    Kubernetes 系列(五):Prometheus监控框架简介
    .Net Core 商城微服务项目系列(十二):使用k8s部署商城服务
    Kubernetes 系列(四):使用Traefik访问.net core api
  • 原文地址:https://www.cnblogs.com/zhangjianbing/p/10131355.html
Copyright © 2011-2022 走看看