Spring AOP 拦截指定注解标识的类或方法
代码Demo
@Aspect@Component@Order(10)public class BidAuthorityProxy {/** * 扫描指定包下的类中使用@EnableRoleAuthority注解修饰的类 */@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;}}}// 获取到注解中的值后进行后续自定义逻辑操作retu pjp.proceed();// 执行方法}}
相关说明:
AOP中扫描指定注解相关说明
(1)@annotation:用来拦截所有被某个注解修饰的方法
(2)@within:用来拦截所有被某个注解修饰的类
(3)within:用来指定扫描的包的范围
作者:琅琊之榜PJ
来源链接:https://blog.csdn.net/java_faep/article/details/104005399
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。