spring aop拦截自定义注解的切入点表达式

@within(com.cxh.study.aop.controller.UserAccessAnnotation)
表示拦截含有com.cxh.study.aop.controller.UserAccessAnnotation这个注解的类中所有方法

@annotation(com.cxh.study.aop.controller.UserAccessAnnotation)
表示拦截含有这个注解的方法

注意:如果在方法参数上需要该注解,则在表达式中不写类名,而是写参数名; 如果拦截的方法上有注解,则可以直接在方法上加入参数就可以取得,如果注解在Class上则需要使用ProceedingJoinPoint通过反射取得注解

示例

package com.cxh.study.aop.controller;

import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Jackie on 2015/4/20.
 * Email : cxh5060@163.com
 */
@Aspect
@Component
public class LoginInterceptor {
    /**
     * A Join Point is defined in the action layer where the method needs
     * a permission check.
     */
    @Autowired
    private HttpServletRequest httpServletRequest;

    @Pointcut("@annotation(com.cxh.study.aop.controller.UserAccessAnnotation)")
    public void userAccess() {}


    /**
     * 这里需要取得userAccessAnnotation,
     * 如果注解是在方法上,可以得到,如果注解在类上则为null
     * @param proceedingJoinPoint
     * @param userAccessAnnotation
     * @throws Throwable
     */
    @Around(value = "@within(userAccessAnnotation) || " +
            "@annotation(userAccessAnnotation)")
    public void beforeMethod(ProceedingJoinPoint proceedingJoinPoint, UserAccessAnnotation userAccessAnnotation)  throws Throwable{
        System.out.println("method starts");
        proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
        proceedingJoinPoint.getTarget().getClass();

    }

    /**
     * 这里不需要取得userAccessAnnotation
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    @Around(value = "@within(com.cxh.study.aop.controller.UserAccessAnnotation) || " +
            "@annotation(com.cxh.study.aop.controller.UserAccessAnnotation)")
    public void beforeMethod1(ProceedingJoinPoint proceedingJoinPoint)  throws Throwable{
        System.out.println("method starts");
        proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
        proceedingJoinPoint.getTarget().getClass();

    }


    @AfterReturning("userAccess()")
    public void afterMethod(){
        System.out.println("method  ends");
    }



    @Before(value = "@within(com.cxh.study.aop.controller.UserAccessAnnotation) || @annotation(com.cxh.study.aop.controller.UserAccessAnnotation)")
    public void before(JoinPoint joinPoint) throws Throwable {
        LogFactory.getLog(this.getClass()).info("monitor.before, class: " + joinPoint.getSignature().getDeclaringType().getSimpleName() + ", method: " + joinPoint.getSignature().getName());
    }

    @After(value = "@within(com.cxh.study.aop.controller.UserAccessAnnotation) || @annotation(com.cxh.study.aop.controller.UserAccessAnnotation)")
    public void after(JoinPoint joinPoint) throws Throwable {
        LogFactory.getLog(this.getClass()).info("monitor.after, class: " + joinPoint.getSignature().getDeclaringType().getSimpleName() + ", method: " + joinPoint.getSignature().getName());
    }
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值