Home > Enterprise >  is it possible to do 1 Advice to multiple PointCut?
is it possible to do 1 Advice to multiple PointCut?

Time:05-26

For ex: i want to log every behavior in a book class. Is it possilbe to do it with Spring-AOP?

sample

CodePudding user response:

you can try with bellow expression in @Around, for example :

    @Pointcut("execution(* AOPviaAnnotation.Book.*(..))")
    public void allMethodOfBook() {};

    @Around("allMethodOfBook()")
    public void logBookInfo(){
     ...
    }

or try with ||

    @Pointcut("execution(* AOPviaAnnotation.Book.add(..))")
    public void addMethod() {};
    @Pointcut("execution(* AOPviaAnnotation.Book.delete(..))")
    public void deleteMethod() {};

    @Around("addMethod() || deleteMethod()")
    public void logBookInfo(){
     ...
    }

CodePudding user response:

I think setting multiple values in pointcut is impossible. I suggest another approach. How about using custom annotations? you can reference this repository

you can do like below.

  1. write custom annotation
  2. write Aspect (logging)
  3. use annotation where you like to log.
  • Related