Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify single pointcut for all classes that extend a specific class

Tags:

I have multiple classes from different packages that extends a class Super. And i want to create an AOP pointcut around that match all the methods in all classes that extends Super. I have tried this:

@Around("within(com.mypackage.that.contains.super..*)")
public void aroundAllEndPoints(ProceedingJoinPoint joinPoint) throws Throwable {
        LOGGER.info("before Proceed ");
        joinPoint.proceed();
        LOGGER.info("after Proceed");
}

But it doesn't work. Any Suggestions?

like image 359
junior developper Avatar asked Jul 21 '16 14:07

junior developper


People also ask

What is the difference between JoinPoint and pointcut?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.

Which of the following option will match the given pointcut?

Explanation: Union means the methods that either pointcut matches. Intersection means the methods that both pointcuts match. Union is usually more useful. Explanation: Using the static methods in the org.

What is pointcut expression reference?

Pointcut is an expression language of spring AOP which is basically used to match the target methods to apply the advice. It has two parts ,one is the method signature comprising of method name and parameters. Other one is the pointcut expression which determines exactly which method we are applying the advice to.

What is pointcut annotation?

Pointcut: Pointcut is expressions that are matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points and Spring framework uses the AspectJ pointcut expression language.


1 Answers

The pointcut should be:

within(com.mypackage.Super+)

where com.mypackage.Super is the fully qualified base class name and + means "all subclasses". This works for Spring AOP. In AspectJ this would match too many joinpoints, not just method executions. Here is another pointcut that works for both Spring AOP and AspectJ:

execution(* com.mypackage.Super+.*(..))
like image 164
kriegaex Avatar answered Oct 11 '22 13:10

kriegaex