zoukankan      html  css  js  c++  java
  • AOP通过反射获取自定义注解

     自定义注解:

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface DemoAnno {
        String value()  default "";
    }

    AOP: 

    @Pointcut("@annotation(com.hephae.aop.aop.DemoAnno)")
        public void demoAspect() {
        }
    
        @Around(value = "demoAspect()")
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            Signature signature = joinPoint.getSignature();
            MethodSignature methodSignature = (MethodSignature)signature;
            //method为接口的Method对象,获取不到实现类方法上的注解
            Method method = methodSignature.getMethod();
            //targetMethod为实现类方法对象
            Method targetMethod = joinPoint.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes());
            //获取注解
            DemoAnno demoAnno = targetMethod.getAnnotation(DemoAnno.class);
            if (demoAnno != null) {
                String value = demoAnno.value();
            }
            Object obj = null;
            obj = joinPoint.proceed();
            return obj;
        }    
  • 相关阅读:
    poj 2029 Get Many Persimmon Trees 夜
    poj 1191 棋盘分割 夜
    DOM (四)
    div拖拽, onmousedown ,onmousemove, onmouseup
    闭包理解
    DOM(一)
    内存溢出与内存泄漏
    div随鼠标移动而移动(滚动条)
    对象继承模式
    DOM(二)
  • 原文地址:https://www.cnblogs.com/hephae/p/7284293.html
Copyright © 2011-2022 走看看