案例08 AOP方式实现日志记录案例

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 采用AOP的面向切面编程方式,对学生信息管理系统中的新增学生信息、更新学生信息和删除学生信息3个方法实现日志记录业务。

 采用AOP的面向切面编程方式,对学生信息管理系统中的新增学生信息、更新学生信息和删除学生信息3个方法实现日志记录业务。

1. 创建项目

Idea创建Java项目,项目名称为:case08-spring-aop。

2. 导入spring相关jar包

case08-spring-aop项目下创建lib目录,在lib目录下导入Jar包:

    • 核心包

    spring-core-5.3.25.jar

    spring-beans-5.3.25.jar

    spring-context-5.3.25.jar

    spring-expression-5.3.25.jar

      • AOP包

      spring-aop-5.3.25.jar

      aspectjweaver-1.9.7.jar

        • 测试包

        junit-4.6.jar

          • 依赖包

          commons-logging-1.2.jar

          3. 创建Spring配置文件

          src目录下创建applicationContext.xml配置文件。

          <?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!--开启组件扫描--><context:component-scanbase-package="com.wfit"/><!--开启aspectj--><aop:aspectj-autoproxy/></beans>


          4. 创建StudentDao类

          src目录下创建com.wfit.dao包,此包目录下创建StudentDao.java类。

          @RepositorypublicclassStudentDao {
          publicvoidaddStudent(){
          System.out.println("新增成功!");
              }
          publicvoiddelStudent(){
          System.out.println("删除成功!");
              }
          publicvoidgetStudent(){
          System.out.println("查询成功!");
              }
          }


          5. 创建StudentService接口

          src目录下创建com.wfit.service包,此包目录下创建StudentService.java接口。

          publicinterfaceStudentService {
          /*** 新增*/publicvoidaddStudent();
          /*** 删除*/publicvoiddelStudent();
          /*** 查询*/publicvoidgetStudent();
          }


          6. 创建StudentServiceImpl类

          src目录下创建com.wfit.service.impl包,此包目录下创建StudentServiceImpl.java类实现StudentService接口。

          @ServicepublicclassStudentServiceImplimplementsStudentService{
          @AutowiredStudentDaostudentDao;
          @OverridepublicvoidaddStudent() {
          studentDao.addStudent();
              }
          @OverridepublicvoiddelStudent() {
          inti=1/0;
          studentDao.delStudent();
              }
          @OverridepublicvoidgetStudent() {
          studentDao.getStudent();
              }
          }


          7. 创建MyAspect类

          src目录下创建com.wfit.config包,此包目录下创建MyAspect.java类。

          @Component//注解一个组件对象@Aspect//声明一个切面publicclassMyAspect {
          /*** 切入点*/@Pointcut("execution(* com.wfit.service.StudentService.*(..))")
          privatevoidmyPointCut(){
              }
          /*** 前置通知*/@Before("myPointCut()")
          publicvoidbefore(JoinPointjp){
          System.out.println("前置通知,日志:"+jp.getSignature().getName() +"开始执行!");
              }
          /*** 后置返回通知*/@AfterReturning("myPointCut()")
          publicvoidafterReturning(JoinPointjp){
          System.out.println("后置返回通知,日志:"+jp.getSignature().getName() +"执行成功!");
              }
          /*** 环绕通知*/@Around("myPointCut()")
          publicvoidaround(ProceedingJoinPointpjp) throwsThrowable {
          //环绕开始System.out.println("around开始...");
          //执行当前目标方法pjp.proceed();
          //环绕结束System.out.println("around结束...");
              }
          /*** 异常通知*/@AfterThrowing(value="myPointCut()",throwing="e")
          publicvoidafterThrowing(JoinPointjp,Throwablee){
          Stringmsg="异常通知,日志:"+jp.getSignature().getName() +"执行异常,原因是:"+e;
          System.out.println(msg);
              }
          /*** 后置(最终)通知*/@After("myPointCut()")
          publicvoidafter(JoinPointjp){
          System.out.println("后置(最终)通知,日志:"+jp.getSignature().getName() +"执行结束!");
              }
          }


          8. 编写测试类

          src目录下创建com.wfit.test包,此包目录下创建TestStudent.java类。

          publicclassTestStudent {
          privateStudentServicestudentService;
          @Beforepublicvoidinit(){
          ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");
          studentService=applicationContext.getBean("studentServiceImpl", StudentService.class);
              }
          @TestpublicvoidtestAdd(){
          studentService.addStudent();
              }
          @TestpublicvoidtestDel(){
          studentService.delStudent();
              }
          @TestpublicvoidtestGet(){
          studentService.getStudent();
              }
          }


          9. 执行结果

            • 执行testAddStudent结果


              • 执行testDelStudent结果



              相关实践学习
              日志服务之使用Nginx模式采集日志
              本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
              目录
              相关文章
              |
              1月前
              |
              SQL 监控 Java
              在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
              这篇文章介绍了如何在IDEA和Spring Boot中使用AOP技术实现日志信息的记录到数据库的详细步骤和代码示例。
              在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
              |
              2月前
              |
              Java Spring
              在Spring Boot中使用AOP实现日志切面
              在Spring Boot中使用AOP实现日志切面
              |
              16天前
              |
              Prometheus Cloud Native Go
              Golang语言之Prometheus的日志模块使用案例
              这篇文章是关于如何在Golang语言项目中使用Prometheus的日志模块的案例,包括源代码编写、编译和测试步骤。
              20 3
              Golang语言之Prometheus的日志模块使用案例
              |
              2月前
              |
              Java Spring
              在Spring Boot中使用AOP实现日志切面
              在Spring Boot中使用AOP实现日志切面
              |
              3月前
              |
              缓存 监控 安全
              在 Spring Boot 中使用 AOP(Aspect-Oriented Programming)实现日志记录功能
              在 Spring Boot 中使用 AOP(Aspect-Oriented Programming)实现日志记录功能
              106 1
              |
              2月前
              |
              监控 Java Spring
              在Spring Boot中使用AOP实现日志记录
              在Spring Boot中使用AOP实现日志记录
              |
              3月前
              |
              监控 Java Spring
              自定义注解+AOP切面日志+源码
              自定义注解+AOP切面日志+源码
              35 1
              |
              2月前
              |
              XML Java 数据格式
              支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
              支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
              |
              3月前
              |
              XML Java 数据格式
              Spring5系列学习文章分享---第三篇(AOP概念+原理+动态代理+术语+Aspect+操作案例(注解与配置方式))
              Spring5系列学习文章分享---第三篇(AOP概念+原理+动态代理+术语+Aspect+操作案例(注解与配置方式))
              34 0
              |
              3月前
              |
              监控 Java 数据安全/隐私保护
              使用 AOP 记录操作日志
              使用 AOP 记录操作日志
              32 0