非常相似,my answer here這sheltem已經指出,該解決方案看起來像這樣(在註釋風格的語法這個時間,因爲在Spring AOP不能使用本地AspectJ的語法):
樓主的註釋:
package annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface MyAnnotation {}
驅動器應用:
我使用驅動程序應用程序來測試我的AspectJ解決方案。在Spring中,類和方面都需要成爲Spring bean /組件才能實現。
package de.scrum_master.app;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import annotations.MyAnnotation;
public class Application {
public void method1(@MyAnnotation int i) {}
public void method2(String id, @MyAnnotation float f) {}
public void method3(int i, @MyAnnotation List<String> strings, @MyAnnotation String s) {}
public void method4(int i, @MyAnnotation Set<Integer> numbers, float f, boolean b) {}
public void method5(boolean b, String s, @MyAnnotation String s2, float f, int i) {}
public void notIntercepted(boolean b, String s, String s2, float f, int i) {}
public static void main(String[] args) {
List<String> strings = new ArrayList<String>();
strings.add("foo");
strings.add("bar");
Set<Integer> numbers = new HashSet<Integer>();
numbers.add(11);
numbers.add(22);
numbers.add(33);
Application app = new Application();
app.method1(1);
app.method2("foo", 1f);
app.method3(1, strings, "foo");
app.method4(1, numbers, 1f, true);
app.method5(false, "foo", "bar", 1f, 1);
app.notIntercepted(false, "foo", "bar", 1f, 1);
}
}
看點:
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.SoftException;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import annotations.MyAnnotation;
@Aspect
public class ArgCatcherAspect {
@Before("execution(public * *(.., @MyAnnotation (*), ..))")
public void interceptMethodsWithAnnotatedParameters(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
MethodSignature signature = (MethodSignature) thisJoinPoint.getSignature();
String methodName = signature.getMethod().getName();
Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
Annotation[][] annotations;
try {
annotations = thisJoinPoint.getTarget().getClass().
getMethod(methodName, parameterTypes).getParameterAnnotations();
} catch (Exception e) {
throw new SoftException(e);
}
int i = 0;
for (Object arg : thisJoinPoint.getArgs()) {
for (Annotation annotation : annotations[i]) {
if (annotation.annotationType() == MyAnnotation.class)
System.out.println(" " + annotation + " -> " + arg);
// Verify 'arg' here or do whatever
}
i++;
}
}
}
控制檯日誌:
execution(void de.scrum_master.app.Application.method1(int))
@annotations.MyAnnotation() -> 1
execution(void de.scrum_master.app.Application.method2(String, float))
@annotations.MyAnnotation() -> 1.0
execution(void de.scrum_master.app.Application.method3(int, List, String))
@annotations.MyAnnotation() -> [foo, bar]
@annotations.MyAnnotation() -> foo
execution(void de.scrum_master.app.Application.method4(int, Set, float, boolean))
@annotations.MyAnnotation() -> [33, 22, 11]
execution(void de.scrum_master.app.Application.method5(boolean, String, String, float, int))
@annotations.MyAnnotation() -> bar
[帶有加註解的參數匹配的切入點方法](的
可能重複http://stackoverflow.com/questions/2766844/pointcut-matching-methods-with-annotated-parameters) – sheltem
@sheltem,感謝您指出這一點。不幸的是,這是我嘗試過的一件事,但失敗了。日誌包含以下錯誤:切入點格式不正確:期望字符位置處的'名稱模式'執行(公共* *(..,@ aspects.VerifyMe(*),..)) – Stormcloud
BTW:有兩個「公共」一詞後,由一個空格隔開的單詞 - stackoverflow將它們解釋爲斜體! – Stormcloud