本文介绍如何在aop编程中解析spel表达式,实现非常简单,Spring本身就提供了简便的api,我们只需要获取:
方法:Method method
方法参数:Object[] arguments
spel表达式:String spel
这些都能从aop入口方法的参数ProceedingJoinPoint中得到。
spel表达式显然就是从自定义注解中获取了,而获取方法和参数的方式如下:
获取方法:
private Method getMethod(ProceedingJoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
if (method.getDeclaringClass().isInterface()) {
try {
method = joinPoint
.getTarget()
.getClass()
.getDeclaredMethod(joinPoint.getSignature().getName(),
method.getParameterTypes());
} catch (SecurityException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
return method;
}
获取方法参数值:
Object[] argumen

本文介绍了如何在Spring AOP编程中解析SpEL表达式,通过ProceedingJoinPoint获取方法和参数,利用Spring提供的API解析注解中的SpEL表达式。同时展示了如何在@AfterReturning后置增强中处理注解表达式的出参,包括对JSONPath的使用来处理复杂结构的数据。示例代码详细展示了方法和参数获取、SpEL解析以及注解处理的实现过程。
最低0.47元/天 解锁文章
1252

被折叠的 条评论
为什么被折叠?



