Sto*_*oud 7 java spring annotations aspectj
我在解决如何创建一个可以在具有特定注释参数的bean上运行的切入点时遇到了一些麻烦.我最终的目标是在处理参数之前验证参数的值,但目前我只需要创建切入点.
请考虑以下注释
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface MyAnnotation {}
Run Code Online (Sandbox Code Playgroud)
然后,我想将其应用于以下方法:
public void method1(@MyAnnotation long i) {}
public void method2(String someThing, @MyAnnotation long i) {}
public void method3(String someThing, @MyAnnotation long i, byte value) {}
Run Code Online (Sandbox Code Playgroud)
所以
我的切入点实现需要有以下几点:
@Before(value = "* *(..) && args(verifyMe)")
public void verifyInvestigationId(long verifyMe) {}
Run Code Online (Sandbox Code Playgroud)
我对这个@Before
值究竟是什么以及如何将注释及其类型联系起来感到有些困惑.在这一点上,可能不值得列出我尝试过的东西!
更新:基于我在http://stackoverflow.com/questions/3565718/pointcut-matching-methods-with-annotated-parameters/3567170#3567170中看到的建议(纠正了一些误解并增加了我忽略的空间) )我已经到了以下工作的地步:
@Before("execution(public * *(.., @full.path.to.MyAnnotation (*), ..))")
public void beforeMethod(JoinPoint joinPoint) {
System.out.println("At least one of the parameters are annotated with @MyAnnotation");
}
Run Code Online (Sandbox Code Playgroud)
这几乎就是我所需要的 - 我需要做的就是将带注释的参数的值作为参数传递给方法.我无法弄清楚Spring的语法(链接的答案没有显示出来).
非常相似,在这里我的回答这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 {}
Run Code Online (Sandbox Code Playgroud)
司机申请:
我使用驱动程序应用程序来测试我的AspectJ解决方案.在Spring中,类和方面需要是Spring bean/components才能实现.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
方面:
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++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
控制台日志:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4132 次 |
最近记录: |