springboot最新版本自定义日志注解和AOP
LogAspectAnnotation
@ControllerLogAspectAnnotation
/**
*
* Define a log facet annotation
* @author sunyang
* @date 2018/12/19 14:23
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.METHOD})
public @interface ControllerLogAspectAnnotation {
//Description
String description() default "";
//Print request paramters
boolean isPrintPostData() default true;
//Print response Results
boolean isPrintResultData() default true;
//Print exceptions
boolean isPrintThrowing() default true;
//Print consumption time
boolean isPrintSpendTime() default false;
//Default value
String value() default "";
}
AbstractControllerLogAspect
com.sunyk.annotation.AbstractControllerLogAspect
/**
* Define abstract class log slices
*
* @author sunyang
* @date 2018/12/19 14:30
*/
public abstract class AbstractControllerLogAspect {
private static final Logger log = LoggerFactory.getLogger(AbstractControllerLogAspect.class);
public AbstractControllerLogAspect() {
}
//Define an abstract method
public abstract void controllerLog();
//Logic processing before operation
@Before("controllerLog() && @annotation(controllerLogAspectAnnotation)")
public void doBefore(JoinPoint joinPoint, ControllerLogAspectAnnotation controllerLogAspectAnnotation) throws Throwable{
if (controllerLogAspectAnnotation.isPrintPostData()){
log.info(controllerLogAspectAnnotation.description() + "开始调用:" + "requestData = {}", joinPoint.getArgs());
}
}
}
ControllerLogAspect
com.sunyk.aop.ControllerLogAspect
/**
*
* Using AOP to define aspect components, and used for annotation such as
* @ControllerLogAspectAnnotation.
* From the AbstractColltrollerLogAspect class.
*
* @author sunyang
* @date 2018/12/19 15:07
*/
@Aspect
@Component
@Configuration
public class ControllerLogAspect extends AbstractControllerLogAspect{
@Override
@Pointcut("execution(* com.sunyk.*.controller.*Controller.*(..))")
public void controllerLog() {
}
}
ServiceMonitor
com.sunyk.aop.ServiceMonitor
/**
* Use AOP alone to define aspect scan components
* @author sunyang
* @date 2018/12/19 18:39
*/
@Aspect
@Component
public class ServiceMonitor {
@AfterReturning("execution(* com.sunyk.*.controller.UseWebFluxController.useLog())")
public void logServiceAccess(JoinPoint joinPoint){
System.out.println("Completed:" + joinPoint);
}
}
Test
com.sunyk.flux.controller.UseWebFluxController
/**
* @author sunyang
* @date 2018/12/19 14:40
*/
@RestController
public class UseWebFluxController {
@GetMapping("/")
@ControllerLogAspectAnnotation(description = "start...", isPrintPostData = true)
public String useLog(){
System.out.println("use log....");
return null;
}
}
Effect Diagram

Other
pom.xml dependencies
<version>2.1.1.RELEASE</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
springboot最新版本自定义日志注解和AOP的更多相关文章
- 自定义日志注解 + AOP实现记录操作日志
需求:系统中经常需要记录员工的操作日志和用户的活动日志,简单的做法在每个需要的方法中进行日志保存操作, 但这样对业务代码入侵性太大,下面就结合AOP和自定义日志注解实现更方便的日志记录 首先看 ...
- Springboot中使用自定义参数注解获取 token 中用户数据
使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...
- 使用springboot最新版本mysql-Connector连接数据库时报错解决
在连接数据库时,使用了最新版本的mysql-Connector,即6.0以上版本 1.报错如下: Loading class `com.mysql.jdbc.Driver'. This is depr ...
- SpringBoot 使用 JSR303 自定义校验注解
JSR303 是 Java EE 6 中的一项子规范,叫做 Bean Validation,官方参考实现是hibernate Validator,有了它,我们可以在实体类的字段上标注不同的注解实现对数 ...
- Spring自定义日志注解
JDK1.5中引入注解,spring框架把java注解发扬光大 一 创建自定义注解 import java.lang.annotation.Retention; import java.lang.a ...
- Springboot高版本中@ConfigurationProperties注解取消location属性
在spring boot 1.5 版本之前 在@ConfigurationProperties注释中有两个属性:locations:指定配置文件的所在位置prefix:指定配置文件中键名称的前缀 sp ...
- 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Spring Boot 自定义注解,AOP 切面统一打印出入参请求日志
其实,小哈在之前就出过一篇关于如何使用 AOP 切面统一打印请求日志的文章,那为什么还要再出一篇呢?没东西写了? 哈哈,当然不是!原因是当时的实现方案还是存在缺陷的,原因如下: 不够灵活,由于是以所有 ...
- Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId
一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...
随机推荐
- 小白看过来runtinme
RunTime 概述 RunTime消息机制 RunTime交换方法 RunTime消息转发 RunTime关联对象 RunTime实现字典与模型互转 1.RunTime 概述 我们在面试的时候,经常 ...
- 【腾讯Bugly干货分享】Android 插件技术实战总结
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/1p5Y0f5XdVXN2EZYT0AM_A 前言 安 ...
- [ARCH] 1、virtualbox中安装archlinux+i3桌面,并做简单美化
星期六, 28. 七月 2018 02:42上午 - beautifulzzzz 1.安装ArchLinux系统 安装Arch主要看其wiki,比较详细- 中文的我主要参考:一步步教你如何安装 Arc ...
- Python自动化编程-树莓派的介绍与使用(一)
Raspberry Pi(中文名为“树莓派”,简写为RPi,(或者RasPi / RPI) [1] 是为学习计算机编程教育而设计),只有信用卡大小的微型电脑,其系统基于Linux. 随着Windo ...
- 2.AsyncQueryHandler、内容提供者
会话页面 Test :测试 public class Test extends AndroidTestCase{ public void test(){ Uri uri = Uri.parse(&qu ...
- 从 0 开始手写一个 Mybatis 框架,三步搞定!
阅读本文大概需要 3 分钟. MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码. 本文完成的Mybatis功能比较简单,代码还有许 ...
- Git使用详细教程(6):git mv重命名文件
与使用git rm类似,Git还提供了重命名文件的命令git mv,我们可以通过该命令重命名文件. 如下图,git mv包含了以下动作
- Kali学习笔记14:SMB扫描、SMTP扫描
SMB(Server Message Block)协议,服务消息块协议. 最开始是用于微软的一种消息传输协议,因为颇受欢迎,现在已经成为跨平台的一种消息传输协议. 同时也是微软历史上出现安全问题最多的 ...
- Python 字符串增删改查的使用
#coding=utf-8a = 'haha'a = "hao"print(a)s = 'Hello World!'print(s.swapcase()) #大写变小写,小写变大写 ...
- Scala - Tips
1- 运行scala命令,提示报错 问题现象: 在Windows7系统中安装scala后(直接安装MSI包,或者解压zip包添加环境变量的方式),执行scala命令报错,但可以执行scala -ver ...