Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@AspectJ pointcut for all methods inside package

I have this working code for a specific package, but i want to configure it for all controllers, service and dao packages Eg

  • com.abc.xyz.content.controller
  • com.abc.xyz.content.service
  • com.abc.xyz.content.dao
  • com.abc.xyz.category.controller
  • com.abc.xyz.category.service
  • com.abc.xyz.category.dao

and so on. . . that is the base package of my project, can someone please help how I can go about doing it so that it works for all classes of my web project including controllers, thanks in advance. . .

package com.abc.xyz.utilities;  import java.util.Arrays;  import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;  @Aspect @Component public class LoggingAspect {     private Log log = LogFactory.getLog(this.getClass());      @Pointcut("execution(* com.abc.xyz.content.service..*(..))")     protected void loggingOperation()     {     }      @Before("loggingOperation()")     @Order(1)     public void logJoinPoint(JoinPoint joinPoint)     {     log.info("Signature declaring type : " + joinPoint.getSignature().getDeclaringTypeName());     log.info("Signature name : " + joinPoint.getSignature().getName());     log.info("Arguments : " + Arrays.toString(joinPoint.getArgs()));     log.info("Target class : " + joinPoint.getTarget().getClass().getName());     }      @AfterReturning(pointcut = "loggingOperation()", returning = "result")     @Order(2)     public void logAfter(JoinPoint joinPoint, Object result)     {     log.info("Exiting from Method :" + joinPoint.getSignature().getName());     log.info("Return value :" + result);     }      @AfterThrowing(pointcut = "execution(* com.abc.xyz.content.service..*(..))", throwing = "e")     @Order(3)     public void logAfterThrowing(JoinPoint joinPoint, Throwable e)     {     log.error("An exception has been thrown in " + joinPoint.getSignature().getName() + "()");     log.error("Cause :" + e.getCause());     }      @Around("execution(* com.abc.xyz.content.service..*(..))")     @Order(4)     public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable     {     log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));     try     {         Object result = joinPoint.proceed();         log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);         return result;     }     catch (IllegalArgumentException e)     {         log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");         throw e;     }     }  } 
like image 296
Mohan Seth Avatar asked Sep 23 '14 10:09

Mohan Seth


People also ask

How do you specify a single pointcut for multiple packages?

You can use boolean operators: expression="execution(* package1. *. *(..))

What methods does this pointcut expression reference within?

within – pointcut expression for matching to join points within certain types. this – pointcut expression for matching to join points where the bean reference is an instance of the given type. target – pointcut expression for matching to join points where the target object is an instance of the given type.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

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.


1 Answers

How about one of these alternatives?

A) General execution pointcut with package restrictions:

execution(* *(..)) && (     within(com.abc.xyz..controller..*) ||     within(com.abc.xyz..service..*) ||     within(com.abc.xyz..dao..*) ) 

B) Package-restricted execution pointcuts:

execution(* com.abc.xyz..controller..*(..)) || execution(* com.abc.xyz..service..*(..)) || execution(* com.abc.xyz..dao..*(..)) 

I prefer B, by the way, just because it is a bit shorter and easier to read. As you have probably guessed, the .. notation means "any package or subpackage", whereas * at the end of the expression after .. means "any method in any class".

like image 83
kriegaex Avatar answered Sep 28 '22 04:09

kriegaex