zoukankan      html  css  js  c++  java
  • springboot-aop

    https://www.cnblogs.com/aston/p/7257657.html

    1.工程结构图

    2.maven依赖

    3.AopTest controller 类

      

    1 @RestController
    2 public class AopTest {
    3 
    4 @RequestMapping (value = "/aoptest")
    5 public String aopTest(){
    6 return " AOP test success!";
    7 }
    8 
    9 }

    4.定义aop类 ,切点,前置通知before,后置通知after,返回通知AfterReturning

     1 @Aspect
     2 @Component
     3 public class HttpAspect {
     4 //使用org.slf4j.Logger,这是Spring实现日志的方法
     5 private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
     6 
     7 /**
     8  * 定义AOP扫描路径
     9  * 第一个注解只扫描aopTest方法
    10  */
    11 //@Pointcut("execution(public * com.aston.reader.controller.StudentController.aopTest())")
    12 ///aop-test/src/main/java/com/test/controller/AopTest.java
    13 @Pointcut("execution(public * com.test.controller.AopTest.aopTest())")
    14 public void log(){}
    15 
    16 /**
    17  * 记录HTTP请求开始时的日志
    18  */
    19 @Before("log()")
    20 public void doBefore(JoinPoint joinPoint){
    21     ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    22     HttpServletRequest request = attributes.getRequest();
    23     //URL
    24     logger.info("url={}", request.getRequestURI());
    25     //method
    26     logger.info("method={}", request.getMethod());
    27     //ip
    28     logger.info("ip={}",request.getRemoteAddr());
    29     //类方法
    30     logger.info("class={} and method name = {}",joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName());
    31     //参数
    32     logger.info("参数={}",joinPoint.getArgs());
    33 }
    34 
    35 /**
    36  * 记录HTTP请求结束时的日志
    37  */
    38 @After("log()")
    39 public void doAfter(){
    40     ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    41     HttpServletRequest request = attributes.getRequest();
    42     logger.info("url = {} end of execution", request.getRequestURL());
    43 }
    44 
    45 /**
    46  * 获取返回内容
    47  * @param object
    48  */
    49 @AfterReturning(returning = "object",pointcut = "log()")
    50 public void doAfterReturn(Object object){
    51     logger.info("response={}",object.toString());
    52 }
    53 }

    5.启动,输入请求http://localhost:8080/aoptest

  • 相关阅读:
    Oracle Relink RAC Binary Options 说明
    Oracle 10g 对象 默认 ITL 数量 测试
    Oracle 相关的专业术语 说明
    Oracle 11g 新特性 自适应游标共享(Adaptive Cursor Sharing: ACS) 说明
    symbian 学习笔记(1) 基础
    symbian 学习笔记(3) 手机独有
    计算机体系结构几个常用的知识点记录
    数据结构和算法笔记(3)
    windows mobile 通用曾抽象
    一些简单常用算法整理学习
  • 原文地址:https://www.cnblogs.com/Andrew520/p/8544564.html
Copyright © 2011-2022 走看看