AOP实现几种方式

@Aspect
@Component
@Order(2)
public class AopUtil {

    /**
    *定义了切入方式,表达式与自定义注解
     * 对所有service和impl进行切入
     * 需要在yml添加:
     *   aop:
     *     proxy-target-class: true
     */
    @Pointcut("execution(* cc.mrbird.febs.server.grp.controller.*.*(..)) ||     @annotation(cc.mrbird.febs.common.core.utils.Inrercept)")
    public  void ponitCutExceution(){

    }

    @Pointcut("@annotation(cc.mrbird.febs.common.core.utils.Inrercept)")
    public  void pointCutThis(){

    }

    //    @Before("pointCutThis()")
    public void beforeAnnotation(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在方法
        Method method = signature.getMethod();
        //获取请求类名
        String className = joinPoint.getTarget().getClass().getName();
//        System.out.println("请求类名:"+className);
        //获取请求方法名
        String methodName = method.getName();
//        System.out.println("获取请求方法名:"+methodName);
        //获取请求参数
        Object[] args = joinPoint.getArgs();
        for (Object o : args) {
            //进行权限字段赋值
            AuthorizationUtil.setAuthObject(o);
        }
    }

    @Before("ponitCutExceution()")
    public void beforeAnnotationAll(JoinPoint joinPoint){

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在方法
        Method method = signature.getMethod();
        //获取请求类名
        String className = joinPoint.getTarget().getClass().getName();
//        System.out.println("请求类名:"+className);
        //获取请求方法名
        String methodName = method.getName().toLowerCase();
        System.out.println("获取请求方法名:"+methodName);


}

自定义注解类:

@Target({ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Inrercept {

    boolean value() default true;


}

使用示例:

  
    @RequestMapping(value = "/testaop", method = RequestMethod.POST)
    @Inrercept()
    public void testaop(CaseBasis caseBasis) {
        //此方法会自动被拦截
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值