AOP通过反射获取自定义注解
Posted hephae
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
以上是关于AOP通过反射获取自定义注解的主要内容,如果未能解决你的问题,请参考以下文章