Spring AOP 使用方法和参数注释
Spring AOP using method & parameter annotations
有没有办法让 Spring AOP 识别已注释的参数的值? (不能保证传入切面的参数的顺序,所以我希望用注解来标记需要用来处理切面的参数)
任何替代方法也会非常有帮助。
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Wrappable {
}
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Key {
}
@Wrappable
public void doSomething(Object a, @Key Object b) {
// something
}
@Aspect
@Component
public class MyAspect {
@After("@annotation(trigger)" /* what can be done to get the value of the parameter that has been annotated with @Key */)
public void trigger(JoinPoint joinPoint, Trigger trigger) { }
我们不能像方法注释那样获取参数注释值作为 AOP 的参数,因为注释不是实际参数,在那里您只能引用实际参数。
args(@Key b)
此注释将为您提供 Object(b) 的值,而不是 @Key 注释的值。
我们可以通过这种方式获取参数注解的值
MethodSignature methodSig = (MethodSignature) joinpoint.getSignature();
Annotation[][] annotations = methodSig.getMethod().getParameterAnnotations();
if (annotations != null) {
for (Annotation[] annotArr : annotations) {
for (Annotation annot : annotArr) {
if (annot instanceof KeyAnnotation) {
System.out.println(((KeyAnnotation) annot).value());
}
}
}
}
.
这是一个方面 class 的示例,它应该处理用 @Wrappable 注释标记的方法。调用 wrapper 方法后,您可以遍历方法参数以查明是否有任何参数标记有 @Key 注释。 keyParams 列表包含任何用 @Key 注释标记的参数。
@Aspect
@Component
public class WrappableAspect {
@After("@annotation(annotation) || @within(annotation)")
public void wrapper(
final JoinPoint pointcut,
final Wrappable annotation) {
Wrappable anno = annotation;
List<Parameter> keyParams = new ArrayList<>();
if (annotation == null) {
if (pointcut.getSignature() instanceof MethodSignature) {
MethodSignature signature =
(MethodSignature) pointcut.getSignature();
Method method = signature.getMethod();
anno = method.getAnnotation(Wrappable.class);
Parameter[] params = method.getParameters();
for (Parameter param : params) {
try {
Annotation keyAnno = param.getAnnotation(Key.class);
keyParams.add(param);
} catch (Exception e) {
//do nothing
}
}
}
}
}
}
有没有办法让 Spring AOP 识别已注释的参数的值? (不能保证传入切面的参数的顺序,所以我希望用注解来标记需要用来处理切面的参数)
任何替代方法也会非常有帮助。
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Wrappable {
}
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Key {
}
@Wrappable
public void doSomething(Object a, @Key Object b) {
// something
}
@Aspect
@Component
public class MyAspect {
@After("@annotation(trigger)" /* what can be done to get the value of the parameter that has been annotated with @Key */)
public void trigger(JoinPoint joinPoint, Trigger trigger) { }
我们不能像方法注释那样获取参数注释值作为 AOP 的参数,因为注释不是实际参数,在那里您只能引用实际参数。
args(@Key b)
此注释将为您提供 Object(b) 的值,而不是 @Key 注释的值。
我们可以通过这种方式获取参数注解的值
MethodSignature methodSig = (MethodSignature) joinpoint.getSignature();
Annotation[][] annotations = methodSig.getMethod().getParameterAnnotations();
if (annotations != null) {
for (Annotation[] annotArr : annotations) {
for (Annotation annot : annotArr) {
if (annot instanceof KeyAnnotation) {
System.out.println(((KeyAnnotation) annot).value());
}
}
}
}
.
这是一个方面 class 的示例,它应该处理用 @Wrappable 注释标记的方法。调用 wrapper 方法后,您可以遍历方法参数以查明是否有任何参数标记有 @Key 注释。 keyParams 列表包含任何用 @Key 注释标记的参数。
@Aspect
@Component
public class WrappableAspect {
@After("@annotation(annotation) || @within(annotation)")
public void wrapper(
final JoinPoint pointcut,
final Wrappable annotation) {
Wrappable anno = annotation;
List<Parameter> keyParams = new ArrayList<>();
if (annotation == null) {
if (pointcut.getSignature() instanceof MethodSignature) {
MethodSignature signature =
(MethodSignature) pointcut.getSignature();
Method method = signature.getMethod();
anno = method.getAnnotation(Wrappable.class);
Parameter[] params = method.getParameters();
for (Parameter param : params) {
try {
Annotation keyAnno = param.getAnnotation(Key.class);
keyParams.add(param);
} catch (Exception e) {
//do nothing
}
}
}
}
}
}