首先是几个概念:连接点(Joinpoint)、切点(Pointcut)、增强(Advice)、切面(Aspect)

另外也要使用到注解。

需求:通过注解定义LogEnable。然后程序运行能够识别定义了LogEnable注解的方法记录日志。

1.定义注解

packagecn.duanjt.util;import java.lang.annotation.*;/*** 记录日志的注解类

*@author段江涛

* @date 2018-11-08*/@Target(ElementType.METHOD)//表示用于标识方法

@Retention(RetentionPolicy.RUNTIME)//表示运行时保留

public @interfaceLogEnable {/*** 主要是标志日志的描述信息

*@return

*/String note()default "";

}

2.定义需要监听的类和方法

packagecn.duanjt.service;importorg.springframework.stereotype.Service;importcn.duanjt.Pojo.Student;importcn.duanjt.util.LogEnable;

@Servicepublic classStudentService {//定义注解,然后描述当前方法的作用

@LogEnable(note="获取学生信息")public Student getStudent(intid) {if (id == 0) {throw new RuntimeException("编码不能为0");

}

Student stu= newStudent();

stu.setId(id);

stu.setName("zhangsan");

stu.setAddr("重庆");returnstu;

}//未定义注解,将不会被监听

public int getId(intid) {return id + 1;

}

}

3.定义切面,记录日志

packagecn.duanjt.util;importorg.apache.commons.lang3.StringUtils;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.stereotype.Component;

@Aspect

@Componentpublic classLogHelper {//没有单独定义切点,直接在环绕方法里面处理[包cn.duanjt.service下面的所有类下面的所有方法,同时包含LogEnable注解的将被监听]

@Around("execution(* cn.duanjt.service.*.*(..)) && @annotation(LogEnable)")publicObject around(ProceedingJoinPoint point) {

MethodSignature signature=(MethodSignature) point.getSignature();long time = System.currentTimeMillis(); //记录开始时间

String className = point.getTarget().getClass().getName(); //类名

String method = className + "." + signature.getName(); //方法名

Object[] args = point.getArgs(); //参数

LogEnable logEnable= signature.getMethod().getAnnotation(LogEnable.class);

String logNote=logEnable.note(); //日志信息

try{

Object result=point.proceed();

System.out.println("方法名:" +method);

System.out.println("参数:" +StringUtils.join(args));

System.out.println("返回值:" +result.toString());

System.out.println("日志功能:" +logNote);

System.out.println("耗时:" + (System.currentTimeMillis() - time) + "毫秒");

System.out.println("-----------------------");returnresult;

}catch(Exception e) {

System.out.println("方法名1:" +method);

System.out.println("参数:" +StringUtils.join(args));

System.out.println("日志功能:" +logNote);

System.out.println("异常信息:" +e.getMessage());

System.out.println("耗时:" + (System.currentTimeMillis() - time) + "毫秒");

System.out.println("-----------------------");return null;

}catch(Throwable e) {return null;

}

}

}

4.在主程序上要加上注解@EnableAspectJAutoProxy。我这里使用的是springboot,如下:

packagecn.duanjt;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication

@ComponentScan("cn.duanjt")

@EnableAspectJAutoProxy//表示启用AOP

public classServerDemoApplication {public static voidmain(String[] args) {

SpringApplication.run(ServerDemoApplication.class, args);

}

}

最后,运行结果如下:

方法名:cn.duanjt.service.StudentService.getStudent

参数:1返回值:Student [id=1, name=zhangsan, addr=重庆]

日志功能:获取学生信息

耗时:0毫秒-----------------------

注意:

1. @EnableAspectJAutoProxy用于开启全局的AOP

2. LogHelper类上面的@Aspect和@Component是必须的,前者用于标注是切面,后者用于将对象注入到spring容器

3. 切面表达式@Around("execution(* cn.duanjt.service.*.*(..)) && @annotation(LogEnable)").一定需要execution。详细的可以下去再了解

java aop注解日志记录_spring aop通过注解实现日志记录相关推荐

  1. aop阻止方法运行_Spring AOP无法拦截内部方法调用

    假设一个接口里面有两个方法: package demo.long;public interfaceCustomerService {public voiddoSomething1();public v ...

  2. aop注解配置切点 spring_springboot aop 自定义注解方式实现一套完善的日志记录

    一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型(增删改查),详细描述,返回值. 二:项目结构图 如果想学习Java ...

  3. java方法设置切点_如何通过自定义注解实现AOP切点定义

    面向切面编程(Aspect Oriented Programming, AOP)是面向对象编程(Object Oriented Programming,OOP)的强大补充,通过横切面注入的方式引入其他 ...

  4. SpringBoot通过AOP实现系统日志记录(一)-Controller层日志监控

    Service层日志监控:SpringBoot通过AOP实现系统日志记录(二)-Service层日志监控 Mapper层日志监控:SpringBoot通过AOP实现系统日志记录(三)-Mapper层日 ...

  5. java aop 实例_Spring aop 简单示例

    简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出了一下记录 首先看下整个例子的目 ...

  6. AOP基本概念、AOP底层实现原理、AOP经典应用【事务管理、异常日志处理、方法审计】...

    1 什么是AOP AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件 ...

  7. spring框架:简述AOP的使用(xml方式和注解方式)

    本人小白一枚,欢迎大家一起讨论学习,如有错误,还望大家指教. AOP概述 AOP的概念: AOP,全称Apect Oriented Programming,译为面向切面编程,简单的说它可以帮我们把程序 ...

  8. Spring AOP代码实现:实例演示与注解全解

    1 理解AOP 1.1 什么是AOP AOP(Aspect Oriented Programming),面向切面思想,是Spring的三大核心思想之一(两外两个:IOC-控制反转.DI-依赖注入). ...

  9. SpringBoot基于AOP实现自定义非空验证的注解

    为了避免对大量参数进行过多的非空校验,我们可以自定义一个非空验证的注解,因为spring自带的@RequestParam并不能对参数进行非空 准备工作 首先需要创建一个spring boot项目,并引 ...

最新文章

  1. .Net Framework中的委托与事件
  2. java网络编程(二)
  3. android的窗口机制分析------UI管理系统
  4. 【模板】KMP算法、fail树
  5. java中的module是什么意思_Angular - 组件中module.id的含义是什么?
  6. 中计算散度的函数_梯度、散度、旋度
  7. sqlserver有外键无法创建触发器_数据库不使用外键的 9 个理由
  8. C++之new和delete探究
  9. Git本地版本控制备忘
  10. 【缺陷识别】基于matlab GUI SVM金属表面缺陷分类与测量(带面板)【含Matlab源码 1652期】
  11. 【Mybatis源码解析】Mybatis源码体系结构
  12. 计算机专业会学计量经济学吗,计量经济学的论文
  13. orCAD16.6 PSPICE DC Sweep 增加y轴
  14. 如何在Debian系统下搭建SVN
  15. python如何登录一个需要第三方验证的网站_python+selenium 之如何跳过登录验证
  16. ARP 应用——检测IP地址冲突
  17. 第5-3课:Dijkstra 算法
  18. Learning to Estimate the Travel Time(翻译)
  19. 怎样安装注册金蝶软件
  20. 供应链金融+区块链双链合璧

热门文章

  1. 学习实践量子计算与量子
  2. C#LeetCode刷题之#520-检测大写字母(Detect Capital)
  3. 如何破解Mac并为其提供真正应得的精美壁纸
  4. 管理R包——Pacman包介绍
  5. 106_Power Pivot之HR入离调转、在职、离职率相关指标
  6. Java的synchronized的使用_Java中synchronized的用法
  7. 用python可以画的可爱的图形_利用Python绘制诱人的桑基图
  8. ssh协议是osi_计算机网络协议分析
  9. Python 3.8.3 发布
  10. Python常用第三方库大盘点