1. 开启切面编程
<aop:aspectj-autoproxy/>
  1. 编写日志注解类
package com.ff.annotation;import java.lang.annotation.*;/*** 日志注解类* by wyc* 2020.06.12*/@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyLog {public String description() default "";
}
  1. 切面类
package com.ff.annotation;import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;/*** @author wyc*/
@Aspect //AOP 切面
@Component
@Slf4j
public class MyLogAspect {//切入点@Pointcut(value = "@annotation(com.ff.annotation.MyLog)")private void pointcut() {}/*** 在方法执行前** @param point* @param myLog* @return*/@Around(value = "pointcut() && @annotation(myLog)")public Object around(ProceedingJoinPoint point, MyLog myLog) {JSONObject jo = new JSONObject();//说明jo.put("description",myLog.description());//拦截的类名jo.put("clazz",point.getTarget().getClass());//拦截的方法jo.put("method",((MethodSignature) point.getSignature()).getMethod().getName());//入参jo.put("param",point.getArgs());log.info("around-{}",jo);try {return point.proceed(); //执行程序} catch (Throwable throwable) {throwable.printStackTrace();return throwable.getMessage();}}/*** 方法执行后** @param joinPoint* @param myLog* @param result* @return*/@AfterReturning(value = "pointcut() && @annotation(myLog)", returning = "result")public Object afterReturning(JoinPoint joinPoint, MyLog myLog, Object result) {log.info("afterReturning-{}",result);return result;}/*** 方法执行后 并抛出异常** @param joinPoint* @param myLog* @param ex*/@AfterThrowing(value = "pointcut() && @annotation(myLog)", throwing = "ex")public void afterThrowing(JoinPoint joinPoint, MyLog myLog, Exception ex) {log.info("请求:" + myLog.description() + " 出现异常");}}
  1. 日志记录
package com.ff.core.user.controller;import com.ff.annotation.MyLog;
import com.ff.kafka.MessageProducer;
import com.ff.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;import java.io.IOException;@Scope("prototype")
@Controller
@RequestMapping(value = "/user")
public class UserController {@Autowiredprivate DemoService demoService;@Autowiredprivate MessageProducer messageProducer;@RequestMapping(value = "/test", method = RequestMethod.GET)@ResponseBody@MyLog(description = "index请求")public String testDubbo(@Param("j") String j){//  messageProducer.send("test_kafka","内容");System.out.println("城市");return "1str";}}

spring注解日志相关推荐

  1. 54. spring boot日志升级篇—logback【从零开始学Spring Boot】

    在<44. Spring Boot日志记录SLF4J>章节中有关相关的介绍,这里我们在深入的了解下logback框架. 为什么要使用logback ? --在开发中不建议使用System. ...

  2. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考:  Spring 注解学习手札( ...

  3. springboot日志写入mysql_44. Spring Boot日志记录SLF4J【从零开始学Spring Boot】

    学院中有Spring Boot相关的课程!点击「阅读原文」进行查看! SpringSecurity5.0视频:http://t.cn/A6ZadMBe Sharding-JDBC分库分表实战: 在开发 ...

  4. 浅谈Spring注解

    Spring目前的趋势是使用注解结合Java代码而不是配置来定义行为.属性.功能.规则和扩展点,因此梳理注解也是梳理Spring功能点的很好的方式,全面的梳理可以补足我们知识点的漏洞. 查找所有注解 ...

  5. java spring注解维护,从一次工程启动失败谈谈 spring 注解

    原标题:从一次工程启动失败谈谈 spring 注解 檀宝权 Java 后端开发工程师,负责度假 App 后端和广告后端开发维护工作,熟悉 Tomcat,Spring,Mybatis,会点 Python ...

  6. Spring 注解教程

    Spring 注解教程 Spring Annotations允许我们通过java程序配置依赖项并实现依赖项注入. 目录[ 隐藏 ] 1 Spring注释 1.1 Spring注释列表 1.2 Spri ...

  7. JavaEE进阶 - Spring Boot 日志文件 - 细节狂魔

    文章目录 1.?志有什么?? 2.?志怎么?? 3.自定义日志打印 准备工作:创建一个 Spring Boot 项目,并在其中创建一个UserController类,用来演示. 在程序中得到?志对象 ...

  8. Spring注解开发学习笔记

    1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...

  9. Spring注解大全(史上最全,字母编号,有实例)

    目录 A @Accessors 链式编程使用.需要搭配@Getter和@Setter使用.主要有三个参数: 序号 参数名 介绍 1 chain 链式 2 fluent 流式(若无显示指定chain的值 ...

最新文章

  1. JS获取整个页面的文档
  2. 《深入理解计算机系统》读书笔记二:内存,高速缓存存储器
  3. Set常用用法元素检索
  4. 【PAT】A1106 Lowest Price in Supply Chain
  5. PHP读取163邮箱获取联系人
  6. jsp传递参数的四种方法
  7. 网页游戏怎么修改数据_一周网页游戏开服数据总结(9.1-9.7)
  8. cpu卡操作协议iso14443协议
  9. VI编辑器的基本使用
  10. nexus3下载地址
  11. 同一master,两个slave的server_id相同问题处理
  12. html5 心跳效果,css3实现心脏跳动
  13. 调试器原理_调试器的工作原理
  14. servlet exception
  15. mqtt发布json数据_mqtt应用于进程间通信
  16. task判断任务是否存在
  17. 别再用 Visio 了!试试这个比它快 10 倍的画图工具不香吗?
  18. [转载]Badboy使用教程
  19. vant toast 指定挂载到指定位置_文档如何只允许编辑指定位置?2步限制编辑,保护文档不被修改...
  20. python中pass作用_总结python中pass的作用

热门文章

  1. rx580默认频率1150_AMD RX590对比RX580值得买吗?AMD Radeon RX 590显卡深度图解评测+拆机...
  2. StarWind模拟iSCSI设备 [类似openfiler、FreeNAS]
  3. 如何制作含字母的流水条码
  4. linux tcp 包大小,Linux TCP数据包接收处理 --- 转
  5. 电脑硬件故障维修教程
  6. 华拓金服联合主办“第五届中国客户联络中心金融服务高峰论坛”
  7. 太吾绘卷加载卡54_太吾绘卷保存卡死应对方案_无法保存解决方法一览_可可网...
  8. SmartDraw2008破解
  9. win10没有7zip的右键菜单
  10. Sybase SQL查询字段类型