利用aop自定义注解

自定义注解记录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 {
		 // 注解逻辑
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值