天天看点

spring自定义标签aop实现

简单记忆一下先。。。

自定义注解:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface YourAnnotation {
}
           

AOP 实现类:

@Aspect
@Component
public class YourAnnotationAop {
	@Around("@annotation(yourAnnotation)")
    	public Object aopAction(final ProceedingJoinPoint point, YourAnnotation yourAnnotation) throws Throwable {
		Annotation[][] pas = ((MethodSignature) point.getSignature()).getMethod().getParameterAnnotations();//获取YourAnnotation标注方法参数
        	
        	for (int i = 0; i < pas.length; i++) {
            	for (Annotation annotation : pas[i]) {
                	if (annotation instanceof YourAnotherAnnotation) {
				//可以用标签标注原来方法参数,比如设定cache key等,从而可以在这里进行缓存织入等
                	}
            	}
        	}
		Object value = point.proceed();//YourAnnotation注解的方法返回
	}
}