AOP 拦截注解

package com.msxf.demo.aspect;

import com.msxf.demo.annotation.Inner;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

/**
 * @author: hzq
 * @date: 2022/4/24
 */
@Aspect
@Component
public class InnerAspect {

    // 类和方法上带有Inner.class注解 都会被拦截
    // @within(inner) 注解在类上
    // @annotation(inner) 注解在方法上
    // 参数inner 传入的后面表达式
    @Around("@within(inner) || @annotation(inner)")
    @SneakyThrows
    public Object around(ProceedingJoinPoint point, Inner inner) {
        
        // 方法上面没有注解则获取类上面的
        if (inner == null) {
            Class<?> aClass = point.getTarget().getClass();
            inner = AnnotationUtils.findAnnotation(aClass, Inner.class);
        }

        if (inner.value()) {
            throw new RuntimeException("无权访问");
        }

        return point.proceed();
    }


    @Pointcut("execution(public String com.msxf.demo.controller.TestInnerController.select())")
    public void point(){}

    @Around("point()")
    @SneakyThrows
    public Object p(ProceedingJoinPoint point){

        System.out.println("触发了");

        return point.proceed();
    }
}

AOP切入点表达式_息魄的博客-CSDN博客_aop 切入点表达式在面向切面编程中(AOP),往往最先要做的事是配置切入点,官方文档中的切入点表达式如下所示:语法:指示符(访问修饰符(可选) 返回值类型(必须) 包名类名(可选) 方法(必须) 参数)execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?) 例如execution (public * com.ljb.servic...https://blog.csdn.net/T13533331461/article/details/108404689

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值