Issue
I am trying to learn Spring AOP implementation using AspectJ. I have 5 classes in different packages.
package com.sample.a;
Class A{
public void save(){
}
}
}
package com.sample.b;
Class B {
public void save(){
}
}
package com.sample.c;
Class C {
public void save(){
}
}
package com.sample.d;
Class D{
public void save(){
}
}
package com.sample.e;
Class E{
public void save(){
}
}
Whenever these method gets called, I need to print "Hello World", How can I achieve the above scenario using Spring AOP (AspectJ). I have done the following so far -
1) Enabled Spring AspectJ support in applicationContext.xml
<aop:aspectj-autoproxy />
2) Defined reference of Aspect in applicationContext.xml
<bean id="sampleAspectJ" class="com.sample.xxxxxxxx" />
3) Aspect Class
/**
* aspect class that contains after advice
*/
package com.sample;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class SpringAopSample{
@After(value = "execution(* com.sample.*.Save(..))")
public void test(JoinPoint joinPoint) {
System.out.println("Hello World");
}
}
is there any better way to achieve the above scenario? what if these 5 classes are in different packages and have different method names? Do I need to write 5 different methods in aspect (annotated with @after advice)
Solution
You should get acquainted with AspectJ pointcut syntax. For instance
- your own pointcut
execution(* com.sample.*.Save(..))
from the example will not match any of the samplesave()
methods because- you wrote
Save()
with a capital "S", but the methods have lower-case "s" as their first character, - your classes would rather match
com.sample.*.*
(sub-package!) than justcom.sample.*
.
- you wrote
- So you can match all
save()
methods in sub-package classes like this:execution(* com.sample.*.*.save(..))
. The first*
is the subpackagea
tod
, the second*
is the class name. - If you want to match sub-packages and even classes hierarchically to any depth you can just use the
..
syntax like this:execution(* com.sample..save(..))
- If you want to match only
public void
methods of any name and without parameters in all sub-packages, you can useexecution(public void com.sample..*())
And so forth. I hope this answers your question even though it is kind of unclear what exactly you want to know.
Answered By - kriegaex