javascript
java aop注解拦截_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;
}
}
}
// 獲取到注解中的值后進行后續(xù)自定義邏輯操作
return pjp.proceed();// 執(zhí)行方法
}
}
相關說明:
AOP中掃描指定注解相關說明
(1)@annotation:用來攔截所有被某個注解修飾的方法
(2)@within:用來攔截所有被某個注解修飾的類
(3)within:用來指定掃描的包的范圍
曾夢想仗劍走天涯XX
發(fā)布了19 篇原創(chuàng)文章 · 獲贊 24 · 訪問量 7萬+
私信
關注
標簽:Spring,within,VerifyRoleAuthority,annotation,AOP,注解,msig,pjp
來源: https://blog.csdn.net/java_faep/article/details/104005399
總結
以上是生活随笔為你收集整理的java aop注解拦截_Spring AOP 拦截指定注解标识的类或方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 红米手机换屏幕多少钱啊?
- 下一篇: java解析excel的js页面,Jav