From 680bfbe718bc47897bb9b2aac068dd764a2a7bcb Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 19 May 2010 05:52:47 +0000 Subject: [PATCH] SPR-7186 Added section on generic advice parameters --- spring-framework-reference/src/aop.xml | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/spring-framework-reference/src/aop.xml b/spring-framework-reference/src/aop.xml index b748bcefe4..76ccf963e6 100644 --- a/spring-framework-reference/src/aop.xml +++ b/spring-framework-reference/src/aop.xml @@ -1275,6 +1275,44 @@ public void audit(Auditable auditable) { } +
+ Advice parameters and generics + + Spring AOP can handle generics used in class declarations and + method parameters. Suppose you have a generic type like this: + + public interface Sample<T> { + void sampleGenericMethod(T param); + void sampleGenericCollectionMethod(Collection>T> param); +} + + You can restrict interception of method types to certain + parameter types by simply typing the advice parameter to the + parameter type you want to intercept the method for: + + @Before("execution(* ..Sample+.sampleGenericMethod(*)) && args(param)") +public void beforeSampleMethod(MyType param) { + // Advice implementation +} + + That this works is pretty obvious as we already discussed + above. However, it's worth pointing out that this won't work for + generic collections. So you cannot define a pointcut like + this: + + @Before("execution(* ..Sample+.sampleGenericCollectionMethod(*)) && args(param)") +public void beforeSampleMethod(Collection<MyType> param) { + // Advice implementation +} + + To make this work we would have to inspect every element of + the collection, which is not reasonable as we also cannot decide how + to treat null values in general. To achieve + something similar to this you have to type the parameter to + Collection<?> and manually + check the type of the elements. +
+
Determining argument names -- GitLab