significado programming programacion oriented orientada mkyong aspectos java spring aop aspectj spring-aop

java - programming - @AspectJ pointcut para todos los métodos dentro del paquete



programacion orientada a aspectos java (3)

¿Qué tal una de estas alternativas?

A) Corte de punto de ejecución general con restricciones de paquete:

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

B) Puntos de ejecución de paquetes restringidos:

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

Por cierto, prefiero B, solo porque es un poco más corto y más fácil de leer. Como probablemente haya adivinado, la notación .. significa "cualquier paquete o subpaquete", mientras que * al final de la expresión después de .. significa "cualquier método en cualquier clase".

Tengo este código de trabajo para un paquete específico, pero quiero configurarlo para todos los controladores , paquetes de servicio y dao , por ejemplo

  • 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

y así. . . ese es el paquete básico de mi proyecto, ¿alguien puede ayudarme? ¿Cómo puedo hacerlo para que funcione para todas las clases de mi proyecto web, incluidos los controladores? Gracias de antemano. . .

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; } } }


Otra alternativa es usar

@Pointcut("bean(*Controller)")

Pero la denominación de sus frijoles debe ser correspondiente