【转】Spring AOP 拦截指定注解标识的类或方法
AOP中扫描指定注解相关说明
(1)@annotation:用来拦截所有被某个注解修饰的方法
(2)@within:用来拦截所有被某个注解修饰的类
(3)within:用来指定扫描的包的范围
代码Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| @Aspect @Component @Order(10) public class BidAuthorityProxy {
@Around("@within(com.core.annotation.EnableRoleAuthority) && within(com.bid..*)") public Object verifyRoleExecuteCommand(ProceedingJoinPoint pjp) throws Throwable { MethodSignature msig = (MethodSignature) pjp.getSignature(); Method targetMethod = pjp.getTarget().getClass().getDeclaredMethod(msig.getName(), msig.getMethod().getParameterTypes()); VerifyRoleAuthority annotation = targetMethod.getAnnotation(VerifyRoleAuthority.class); if (annotation == null) { Class<?>[] inters = pjp.getTarget().getClass().getInterfaces(); for (Class<?> inter : inters) { Method targetInterMethod = inter.getDeclaredMethod(msig.getName(), msig.getMethod().getParameterTypes()); annotation = targetInterMethod.getAnnotation(VerifyRoleAuthority.class); if (annotation != null) { break; } } } return pjp.proceed(); } }
|
来源
https://blog.csdn.net/java_faep/article/details/104005399