zoukankan      html  css  js  c++  java
  • AOP

    AOP

    maven依赖

    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-aop</artifactId>  
    </dependency> 

     自定义注解

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Time {
    }

    Aspect
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    /**
     * @author zfang
     * @date 2020/7/10
     */
    
    @Aspect
    @Component
    public class WebAspect {
        //private static final String E = "execution(* com....ProjectTypeController.*(..))";
        private static final String E = "@annotation(com....Time)";
    
        /**
         * 切入点
         */
        @Pointcut(E)
        public void executePackage() {
        }
    
        /**
         * 前置通知
         *
         * @param joinPoint
         */
        @Before("executePackage()")
        public void before(JoinPoint joinPoint) {
            System.out.println("前置通知");
        }
    
        /**
         * 后置最终通知
         */
        @After("executePackage()")
        public void after() {
            System.out.println("后置最终通知");
        }
    
        /**
         * 后置返回通知
         *
         * @param joinPoint
         * @param keys
         */
        @AfterReturning(value = E, returning = "result")
        public void afterReturning(JoinPoint joinPoint, String result) {
            System.out.println("后置返回通知");
            System.out.println("后置返回通知 返回值:" + result);
        }
    
        @Around(E)
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            long start = System.currentTimeMillis();
            Object result = joinPoint.proceed();
            long executionTime = System.currentTimeMillis() - start;
            System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
            return result;
        }
    
        /**
         * 后置异常通知
         *
         * @param joinPoint
         * @param exception
         */
        @AfterThrowing(value = "executePackage()", throwing = "tw")
        public void exp(Throwable tw) {
            System.out.println("后置异常通知");
            System.out.println(tw.getMessage());
        }
    }
     
  • 相关阅读:
    LeetCode15.3 Sum
    LeetCode215. Kth Largest Element in an Array
    python基础结构的时间复杂度
    顺时针打印矩阵
    合并k个有序链表
    LeetCode3. Longest Substring Without Repeating Characters
    决策树剪枝问题
    LeetCode98. Validate Binary Search Tree
    LeetCode96. Unique Binary Search Trees
    Visio软件不能使用方向键移动图形的解决办法
  • 原文地址:https://www.cnblogs.com/xiaomaoyvtou/p/13277782.html
Copyright © 2011-2022 走看看