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)