获取切入点内带注释的参数
我有两个注释 @LookAtThisMethod
和 @LookAtThisParameter
,如果我在带有 @LookAtThisMethod
的方法周围有一个切入点,我如何提取所述参数用@LookAtThisParameter注释的方法?
例如:
@Aspect
public class LookAdvisor {
@Pointcut("@annotation(lookAtThisMethod)")
public void lookAtThisMethodPointcut(LookAtThisMethod lookAtThisMethod){}
@Around("lookAtThisMethodPointcut(lookAtThisMethod)")
public void lookAtThisMethod(ProceedingJoinPoint joinPoint, LookAtThisMethod lookAtThisMethod) throws Throwable {
for(Object argument : joinPoint.getArgs()) {
//I can get the parameter values here
}
//I can get the method signature with:
joinPoint.getSignature.toString();
//How do I get which parameters are annotated with @LookAtThisParameter?
}
}
I have two annotation @LookAtThisMethod
and @LookAtThisParameter
, if I have a pointcut around the methods with @LookAtThisMethod
how could I extract the parameters of said method which are annotated with @LookAtThisParameter
?
For example:
@Aspect
public class LookAdvisor {
@Pointcut("@annotation(lookAtThisMethod)")
public void lookAtThisMethodPointcut(LookAtThisMethod lookAtThisMethod){}
@Around("lookAtThisMethodPointcut(lookAtThisMethod)")
public void lookAtThisMethod(ProceedingJoinPoint joinPoint, LookAtThisMethod lookAtThisMethod) throws Throwable {
for(Object argument : joinPoint.getArgs()) {
//I can get the parameter values here
}
//I can get the method signature with:
joinPoint.getSignature.toString();
//How do I get which parameters are annotated with @LookAtThisParameter?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我围绕这个其他答案对不同但类似的问题。
我必须遍历目标类的原因是因为被注释的类是接口的实现,因此
signature.getMethod().getParameterAnnotations()
返回 null。I modeled my solution around this other answer to a different but similar question.
The reason that I had to go through the target class was because the class that was annotated was an implementation of an interface and thusly
signature.getMethod().getParameterAnnotations()
returned null.此代码涵盖了方法属于接口的情况
This code covers the case where the Method belongs to the interface