從aop中獲取被攔截方法中的參數


@Aspect
public class CacheManager {

}


@Before("execution(* com.aliexpress.social.game.cointree.cointree.gateway.application.*.*(..))")
public void migrateCheck(JoinPoint joinPoint) {
String userId = getUserIdFromAop(jointPoint);
}

//從攔截的參數中獲取用戶id
private String getUserIdFromAop(JoinPoint joinPoint){
//https://www.cnblogs.com/yln20170705/p/10511743.html

//類名
String clazzName = joinPoint.getTarget().getClass().getName();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//方法名
String methodName = methodSignature.getName();
//參數名數組
String[] parameters = methodSignature.getParameterNames();
//參數值
Object[] args = joinPoint.getArgs();

//獲取參數名對應數組下標
RequestInfoDTO infoDTO = null;
String userId = null;

//infoDTO是被攔截的發方法的參數名,例如:plant(RequestInfoDTO infoDTO,Long cropId)
int paramIndex = ArrayUtils.indexOf(parameters,"infoDTO");
if (paramIndex != -1){
//參數info在方法列表中,位於第一個位置
infoDTO = (RequestInfoDTO)args[0];
userId = infoDTO.getUserId();

}
log.info("getUserIdFromAop clazzName = {} methodName = {} parameters = {} args = {} userId = {} ",
new Object[] {clazzName, methodName, JSON.toJSONString(parameters), JSON.toJSONString(args), userId});

return userId;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM