I am familiar with Spring AOP, although not having much hands on experience on that.
My question is, if I want to have some AOP functionality for some methods of a class, not all, then can it be possible with single pointcut. Say, I have four methods save1,save2,get1 and get2 in my class and I want to apply AOP on save1 and save2 only, then in that case how can I create a single pointcut for that? How will my pointcut expression looks like? OR Is it even possible?
There are many ways to do it(with wildcard expression, with aspectJ annotation, ..) i will give an example with aspectJ
class MyClass{
@MyPoint
public void save1(){
}
@MyPoint
public void save2(){
}
public void save3(){
}
public void save4(){
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyPoint {
}
@Aspect
@Component
public class MyAspect {
@Before("@annotation(com.xyz.MyPoint)")
public void before(JoinPoint joinPoint) throws Throwable {
//do here what u want
}
}
So you are all set, as long as you marked @Mypoint
annotation, spring will call before aspect for this method, make sure spring is managing this method and object, not you. include aspectJ in your classpath
You need to specify a pointcut expression to select the methods with to apply your advice.
See 7.2.3 Declaring a pointcut in the Spring Documentation and use the execution joinpoint designator to select the methods.
Having a pointcut expression like so should do the trick
**execution(* save*(..))**
See here for more information
您可以将or
和and
与切入点表达式一起使用:
execution(* my.Class.myMethod(..)) or execution(* my.Class.myOtherMethod(..))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.