zoukankan      html  css  js  c++  java
  • 2507-AOP- springboot中使用-使用注解方式

    Springboot中使用aop,与SSM中使用AOP,整体配置与编写方式都是类似的。但是Springboot简化了很多xml配置,切点的表达式可以直接进行javaconfig。

    记录一些示例
    springboot示例:
    版本1.5.9.RELEASE
    pom文件中添加aop的依赖

    <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指定注解的目标为方法级
     * Retention指定注解可以在运行时被获取(利用反射)
     *
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface CountAopAnnotation {
    }
    

    aop类:

    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * aop
     * Created by hxy 2018/5/5.
     */
    
    @Component
    @Aspect
    @Order(10) //构建执行顺序
    public class CountAopHelper {
    
        /*
        * 定义一个切入点
        */
        @Pointcut("@annotation(com.company.project.core.annotation.CountAopAnnotation)")
        public void myInfoAnnotation() {
        }
    
        // 用@Pointcut来注解一个切入方法
        @Pointcut("execution(* com.company.project.web.*.*(..))")
        public void excudeController() {
        }
    
        @Before("myInfoAnnotation()")
        public void deBefore(JoinPoint joinPoint) throws Throwable {
            System.out.println("deBefore");
    
            // 通过RequestContextHolder获取HttpServletRequest 可以获取请求头相关的内容
            // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            // StringBuffer requestURL = request.getRequestURL();
        }
    
        @After("myInfoAnnotation()")
        public void doAfter(JoinPoint joinPoint) throws Throwable {
            System.out.println("doAfter");
    
            // 通过RequestContextHolder获取HttpServletRequest 可以获取请求头相关的内容
            // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            // StringBuffer requestURL = request.getRequestURL();
        }
    
        /**
         * Around(环绕通知)是在Before(前置通知)前面执行
         * &&@annotation(annotation) 这个是对方法参数的形参进行注入
         * <p>
         * value可以是多种 1纯注解形式 myInfoAnnotation()  2 混合 myInfoAnnotation()&&@annotation(annotation)&& excudeController()
         * 使用场景  1大面积使用aop  使用Pointcut来写匹配表达式  2精准定位 使用注解形式
         */
        @Around(value = "myInfoAnnotation()")
        public Object doAround(ProceedingJoinPoint thisJoinPoint) throws Throwable {
            System.out.println("doAround");
    
            // 获取切点的参数
            Object[] args = thisJoinPoint.getArgs();
    
            //环绕通知必须执行,否则不进入注解的方法
            return thisJoinPoint.proceed();
        }
    
    
    }
    
    

    使用:
    在接口上加上注解

       @CountAopAnnotation
        @GetMapping("/testAnno")
        public Result testAnno() {
    
            System.out.println("here is api testAnno");
    
            return ResultGenerator.genSuccessResult("dsds");
        }
    
    

    执行结果是:
    先执行了环绕通知,再执行前置通知,再执行被aop代理的方法,再执行后置通知;

    doAround
    deBefore
    here is api testAnno
    doAfter
    
    
  • 相关阅读:
    函数模板、函数模板特化、重载函数模板、非模板函数重载
    输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
    文件的读写、二进制文件的读写、文件随机读写
    文件流(fstream, ifstream, ofstream)的打开关闭、流状态
    流类库继承体系(IO流,文件流,串流)和 字符串流的基本操作
    对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
    operator new 和 operator delete 实现一个简单内存泄漏跟踪器
    异常与继承、异常与指针、异常规格说明
    程序错误、异常(语法、抛出、捕获、传播)、栈展开
    C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
  • 原文地址:https://www.cnblogs.com/starmoon1994/p/9103939.html
Copyright © 2011-2022 走看看