Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply aop on a whole package except one subpackage

Assuming my current package structure in a spring projects as :

com.stackoverflow
|- service
|- entities
|- controllers
   |- package1
   |- package2
|-util

How can I apply an aspect to all the packages under com.stackoverflow except on the package util?

Applying it to everything, the execution expression would be "com.stackoverflow...(..)"

What should the execution expression be in this case I want to remove the util subpackage from the execution expression?

like image 251
Wael Awada Avatar asked Jun 18 '13 21:06

Wael Awada


People also ask

Which method first executed in AOP?

Executing method on the target class Thus, Spring AOP injects a proxy instead of an actual instance of the target class. When we start the Spring or Spring Boot application, we see Spring executes the advice before the actual method.

What is a pointcut AOP?

Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.

How AOP is implemented in Spring?

In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style). Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception.

Which Aspect oriented programming AOP advice gives you the opportunity to choose whether to proceed to the join point or to shortcut the advised method execution?

Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.


1 Answers

Use the AND && and NOT ! operators in your Pointcut expression as

@Pointcut ("execution (* com.stackoverflow..*.*(..)) && " +
           "!execution (* com.stackoverflow.util..*.*(..))")
like image 169
Ravi K Thapliyal Avatar answered Sep 23 '22 21:09

Ravi K Thapliyal