自定义注解记录log
// (元注解)表示我们的注解可以用在那些地方
@Target(ElementType.METHOD)
// (元注解)表示我们的注解在什么地方有效:source(源码)< class(编译后)< runtime(运行时)
@Retention(RetentionPolicy.RUNTIME)
// (元注解)表示是否将我们的注解生成在javadoc中
@Documented
// (元注解)子类可以继承父类的注解
@Inherited
public @interface SysLog{
// 如果不设置default则使用注解时必填
//操作描述
String desc() ;
// 日志级别 枚举类
LogLevel logLevel() default LogLevel.INFO;
}
声明切面,执行逻辑
// 使用@Aspect注解声明一个切面
@Aspect
@Component
public class SysLogAspect {
/**
* 这里我们使用注解的形式 当然,我们也可以通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method 切点表达式:
* execution(...)
* 注解参数需要是注解全路径
*/
@Pointcut("@annotation(xx.xx.xx.SysLog)")
public void logPointCut() {
}
/**
* 环绕通知 @Around , 当然也可以使用 @Before (前置通知) @After (后置通知)
*
* @param point
* @return
* @throws Throwable
*/
@Before("logPointCut()")
public void around(JoinPoint point) throws Throwable {
// 注解逻辑
}
}