使用SpringBoot+AOP实现可插拔式日志的示例代码

 更新时间:2019年07月26日 16:19:49   作者:Justin_Jun  
这篇文章主要介绍了使用SpringBoot+AOP实现可插拔式日志的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、AOP

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。 AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。

二、实现

引入依赖

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

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

新建SysLog类

package com.example.aop.annotation;

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 SysLog {
 String value() default " ";
}

新建SysLogAspect类

package com.example.aop.annotation;

import com.example.aop.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class SysLogAspect {
 @Autowired
 private LogService logService;

 @Pointcut("@annotation(com.example.aop.annotation.SysLog)")
 public void logPointCut() {}

 @Before("logPointCut()")
 public void before(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "before " + method.getName());
  } catch (Exception e) {
  }
 }

 @Around("logPointCut()")
 public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  Object object = joinPoint.proceed(args);
  try {
   logService.log(beginTime, "around " + method.getName());
  } catch (Exception e) {
  }
  return object;
 }

 @After("logPointCut()")
 public void after(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "after " + method.getName());
  } catch (Exception e) {
  }
 }

 @AfterReturning("logPointCut()")
 public void afterReturning(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "after returning " + method.getName());
  } catch (Exception e) {
  }
 }

 @AfterThrowing("logPointCut()")
 public void afterThrowing(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "after throwing " + method.getName());
  } catch (Exception e) {
  }
 }
}

新建TestService类

package com.example.aop.service;

import com.example.aop.dto.TestDomain;
import com.example.aop.annotation.SysLog;
import org.springframework.stereotype.Service;

@Service
public class TestService {
 @SysLog("log-1")
 public void log1(String name, TestDomain testDomain) {
  System.out.println("print log 1" + name + " " + testDomain.toString());
 }

 @SysLog("log-2")
 public void log2(String name, TestDomain testDomain) {
  System.out.println("print log 2" + name + " " + testDomain.toString());
 }


 @SysLog("throw-exception")
 public void throwException() {
  System.out.println("throw exception");
  int i = 3/0;
 }
}

新建TestController

package com.example.aop.controller;

import com.example.aop.dto.TestDomain;
import com.example.aop.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
 @Autowired
 private TestService testService;

 @GetMapping("test")
 public String test(@RequestParam("p1") String p1) {
  TestDomain testDomain = new TestDomain();
  testDomain.setId("1");
  testDomain.setTitle("t1");
  testService.log1(p1, testDomain);
  testService.log2(p1, testDomain);
  testService.throwException();
  return "hello aop";
 }
}

三、测试

运行AopApplication, 然后访问http://localhost:8080/test?p1=123,可以看到控制台有以下输出:

before log1 at: 1554903984403
print log 1123 com.example.aop.dto.TestDomain@2a7a4cd5
around log1 at: 1554903984402
after log1 at: 1554903984409
after returning log1 at: 1554903984409
before log2 at: 1554903984409
print log 2123 com.example.aop.dto.TestDomain@2a7a4cd5
around log2 at: 1554903984409
after log2 at: 1554903984410
after returning log2 at: 1554903984410
before throwException at: 1554903984410
throw exception
after throwException at: 1554903984410
after throwing throwException at: 1554903984410

github地址:https://github.com/lijun003/SpringBoot-AOP-log 

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring Boot用户注册验证的实现全过程记录

    Spring Boot用户注册验证的实现全过程记录

    最近在设计自己的博客系统,涉及到用户注册与登录验证,所以下面这篇文章主要给大家介绍了关于Spring Boot用户注册验证的实现全过程,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-01-01
  • Java反射机制,如何将一个实体类所有字段赋值为null

    Java反射机制,如何将一个实体类所有字段赋值为null

    这篇文章主要介绍了Java反射机制,如何将一个实体类所有字段赋值为null,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 使用EasyExcel实现百万级别数据导出的代码示例

    使用EasyExcel实现百万级别数据导出的代码示例

    近期需要开发一个将百万数据量MySQL8的数据导出到excel的功能,所以本文讲给大家介绍了基于EasyExcel实现百万级别数据导出,文中通过代码示例讲解的非常详细,需要的朋友可以参考下
    2023-12-12
  • Java中接口和抽象类的区别与相同之处

    Java中接口和抽象类的区别与相同之处

    这篇文章主要介绍了Java中接口和抽象类的区别与相同之处,本文讲解了抽象类的概念、接口的概念、接口和抽象类的区别与联系等内容,需要的朋友可以参考下
    2015-06-06
  • 使用Apache POI和SpringBoot实现Excel文件上传和解析功能

    使用Apache POI和SpringBoot实现Excel文件上传和解析功能

    在现代企业应用开发中,数据的导入和导出是一项常见且重要的功能需求,Excel 作为一种广泛使用的电子表格工具,常常被用来存储和展示数据,下面我们来看看如何使用Apache POI和SpringBoot实现Excel文件上传和解析功能吧
    2025-01-01
  • SpringCloud Gateway使用redis实现动态路由的方法

    SpringCloud Gateway使用redis实现动态路由的方法

    这篇文章主要介绍了SpringCloud Gateway使用redis实现动态路由的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Spring Boot 快速搭建微服务框架详细教程

    Spring Boot 快速搭建微服务框架详细教程

    SpringBoot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。本文重点给大家介绍Spring Boot 快速搭建微服务框架详细教程,需要的的朋友参考下吧
    2017-09-09
  • Springmvc项目web.xml中servlet-mapping路径映射配置注意说明

    Springmvc项目web.xml中servlet-mapping路径映射配置注意说明

    这篇文章主要介绍了Springmvc项目web.xml中servlet-mapping路径映射配置注意说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java数据结构之线段树中的懒操作详解

    Java数据结构之线段树中的懒操作详解

    对于线段树,若要求对区间中的所有点都进行更新,可以引入懒操作。懒操作包括区间更新和区间查询操作。本文将通过一个示例和大家详细聊聊线段树中的懒操作,需要的可以参考一下
    2022-10-10
  • idea2023设置启动参数、单元测试启动参数

    idea2023设置启动参数、单元测试启动参数

    在使用IDEA进行开发时,我们可以通过设置一些启动参数来优化开发环境的性能和体验,具有一定的参考价值,感兴趣的可以了解一下
    2023-11-11

最新评论