码迷,mamicode.com
首页 > 其他好文 > 详细

AOP通过反射获取自定义注解

时间:2017-08-04 12:50:10      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:throwable   param   class   runtime   注解   not   signature   types   interface   

 自定义注解:

@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;
    }

 

AOP通过反射获取自定义注解

标签:throwable   param   class   runtime   注解   not   signature   types   interface   

原文地址:http://www.cnblogs.com/hephae/p/7284293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!