Java 如何获取带注释的方法参数和his注释
在我的应用程序中,我有一些带有参数的方法,这些参数由一些注释注释。现在,我想编写一个方面,使用注释属性中的信息对注释参数进行一些预处理。例如,方法:Java 如何获取带注释的方法参数和his注释,java,spring,aop,aspectj,Java,Spring,Aop,Aspectj,在我的应用程序中,我有一些带有参数的方法,这些参数由一些注释注释。现在,我想编写一个方面,使用注释属性中的信息对注释参数进行一些预处理。例如,方法: public void doStuff(Object arg1, @SomeAnnotation CustomObject arg1, Object arg2){...} 方面: @Before(...) public void doPreprocessing(SomeAnnotation annotation, CustomObject cus
public void doStuff(Object arg1, @SomeAnnotation CustomObject arg1, Object arg2){...}
方面:
@Before(...)
public void doPreprocessing(SomeAnnotation annotation, CustomObject customObject){...}
在@Before中应该写什么
编辑:
谢谢大家。这是我的解决方案:
@Before("execution(public * *(.., @SomeAnnotation (*), ..))")
public void checkRequiredRequestBody(JoinPoint joinPoint) {
MethodSignature methodSig = (MethodSignature) joinPoint.getSignature();
Annotation[][] annotations = methodSig.getMethod().getParameterAnnotations();
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
for (Annotation annotation : annotations[i]) {
if (SomeAnnotation.class.isInstance(annotation)) {
//... preprocessing
}
}
}
}
@Before(“执行(public**(..,@SomeAnnotation(*),..)”)
public void checkRequiredRequestBody(连接点连接点){
MethodSignature methodSig=(MethodSignature)joinPoint.getSignature();
注释[][]注释=methodSig.getMethod().getParameterAnnotations();
对象[]args=joinPoint.getArgs();
对于(int i=0;i
您可以这样做:
@Before("execution(* com.foo.bar.*.doStuff(..)) && args(arg1, arg2)")
public void logSomething(JoinPoint jp, CustomObject arg1, Object arg2) throws Throwable {
MethodSignature methodSignature = (MethodSignature) jp.getSignature();
Class<?> clazz = methodSignature.getDeclaringType();
Method method = clazz.getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes());
SomeAnnotation argumentAnnotation;
for (Annotation ann : method.getParameterAnnotations()[0]) {
if(SomeAnnotation.class.isInstance(ann)) {
argumentAnnotation = (SomeAnnotation) ann;
System.out.println(argumentAnnotation.value());
}
}
}
@SomeAnnotation
public class CustomObject {
}
以及被截取的方法:
public void doStuff(@SomeAnnotation("xyz") CustomObject arg1, Object arg2) {
System.out.println("do Stuff!");
}
你不能这样做
@Before(...)
public void doPreprocessing(SomeAnnotation annotation, CustomObject customObject){...}
因为注释不是参数,在其中只能引用参数。
您可以使用@args(annot)
,但这只匹配放置在参数类型本身上的注释,而不匹配实际参数前面的注释@args(annot)
适用于以下情况:
@Before("execution(* com.foo.bar.*.doStuff(..)) && args(arg1, arg2)")
public void logSomething(JoinPoint jp, CustomObject arg1, Object arg2) throws Throwable {
MethodSignature methodSignature = (MethodSignature) jp.getSignature();
Class<?> clazz = methodSignature.getDeclaringType();
Method method = clazz.getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes());
SomeAnnotation argumentAnnotation;
for (Annotation ann : method.getParameterAnnotations()[0]) {
if(SomeAnnotation.class.isInstance(ann)) {
argumentAnnotation = (SomeAnnotation) ann;
System.out.println(argumentAnnotation.value());
}
}
}
@SomeAnnotation
public class CustomObject {
}
下面是一个应该有效的例子。(我没有测试它,但它应该可以工作)
在这里,我添加了javax.annotation.Resource的测试,只是为了演示如何使用答案,但是当然,您应该用您需要处理的注释替换它。
proceedjoinpoint
只是针对@周围的建议,JoinPoint
是针对@之前的。那么,响应应该是正确的。你说你没有测试它,但它的方式会抛出一个错误。