注解是一种能被添加到java源代码中的元数据,方法、类、参数和包都可以用注解来修饰。注解可以看作是一种特殊的标记,可以用在方法、类、参数和包上,程序在编译或者运行时可以检测到这些标记而进行一些特殊的处理。
声明一个注解要用到的东西
修饰符:访问修饰符必须为public,不写默认为pubic
关键字:关键字为@interface
注解名称:注解名称为自定义注解的名称,使用时还会用到
注解类型元素:注解类型元素是注解中内容
其次,JDK中还有一些元注解,这些元注解可以用来修饰注解。主要有:@Target,@Retention,@Document,@Inherited。
@Target
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)。
取值有:
@Retention
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效),即:注解的生命周期。
@Document
作用:表明该注解标记的元素可以被Javadoc 或类似的工具文档化
@Inherited
作用: 表明使用了@Inherited注解的注解,所标记的类的子类也会拥有这个注解。
自定义注解中参数可支持的数据类型:
1.八大基本数据类型
2.String类型
3.Class类型
4.enum类型
5.Annotation类型
6.以上所有类型的数组
自定义一个注解,如下所示:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperationLog {
String username() default "";
OperationType type();
String content() default "";
}
通过AOP加自定义注解简单实现一个操作日志的记录
自定义注解类:
package com.redistext.log;
import java.lang.annotation.*;
/**
* @author: maorui
* @description: TODO
* @date: 2021/10/27 21:23
* @description: V1.0
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperationLog {
String username() default "admin";
String type(); //记录操作类型
String content() default ""; //记录操作内容
}
切面类:
package com.redistext.log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author: maorui
* @description: TODO
* @date: 2021/10/27 21:29
* @description: V1.0
*/
@Aspect
@Component("logAspect")
public class LogAspect {
// 配置织入点
@Pointcut("@annotation(OperationLog)")
public void logPointCut() {
}
/**
* 前置通知 用于拦截操作,在方法返回后执行
*
* @param joinPoint 切点
*/
@AfterReturning(pointcut = "logPointCut()")
public void doBefore(JoinPoint joinPoint) {
handleLog(joinPoint, null);
}
/**
* 拦截异常操作,有异常时执行
*
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfter(JoinPoint joinPoint, Exception e) {
handleLog(joinPoint, e);
}
private void handleLog(JoinPoint joinPoint, Exception e){
try {
// 得到注解
OperationLog operationLog = getAnnotationLog(joinPoint);
System.out.println("---------------自定义注解:" + operationLog);
if (operationLog == null) {
return;
}
// 得到方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
String type = operationLog.type();
String content = operationLog.content();
String username = operationLog.username();
// 打印日志
System.out.println("操作类型:" + type);
System.out.println("操作名称:" + content);
System.out.println("操作人员:" + username);
System.out.println("类名:" + className);
System.out.println("方法名:" + methodName);
} catch (Exception e1) {
System.out.println("======前置通知异常======");
e1.printStackTrace();
}
}
private static OperationLog getAnnotationLog(JoinPoint joinPoint) throws Exception{
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
// 拿到自定义注解中的信息
return method.getAnnotation(OperationLog.class);
}
return null;
}
}
测试类:
package com.redistext.log;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: maorui
* @description: TODO
* @date: 2021/10/27 21:40
* @description: V1.0
*/
@RestController
@RequestMapping("/operationLog")
public class OperationLogController {
@OperationLog(type = "add", content = "添加")
@GetMapping(value = "/add")
public String addOperation(){
return "add";
}
@OperationLog(type = "update", username = "adminFather")
@GetMapping(value = "/update")
public String updateOperation(){
return "update";
}
@OperationLog(type = "delete", content = "删除", username = "adminMother")
@GetMapping(value = "/delete")
public String deleteOperation(){
return "delete";
}
@OperationLog(type = "find")
@GetMapping(value = "/find")
public String findOperation(){
return "find";
}
}
依次调用测试类各个接口,记录的操作日志信息如下:
参考文献:
使用Spring Aop自定义注解实现自动记录日志 - JavaShuo