包中所有方法的@AspectJ切入点 [英] @AspectJ pointcut for all methods inside package
问题描述
我具有用于特定软件包的此工作代码,但我想为所有控制器,服务和 dao 软件包进行配置 例如
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
,依此类推. . . 那是我的项目的基本软件包,有人可以帮忙我可以做些什么,以便它适用于我的Web项目的所有类,包括控制器,在此先感谢. .
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;
}
}
}
推荐答案
这些替代方法之一如何?
How about one of these alternatives?
A)具有程序包限制的常规执行切入点:
execution(* *(..)) &&
(
within(com.abc.xyz..controller..*) ||
within(com.abc.xyz..service..*) ||
within(com.abc.xyz..dao..*)
)
B)程序包限制的执行切入点:
execution(* com.abc.xyz..controller..*(..)) ||
execution(* com.abc.xyz..service..*(..)) ||
execution(* com.abc.xyz..dao..*(..))
顺便说一句,我更喜欢B,因为它更短并且更容易阅读.您可能已经猜到了,..
表示任何包或子包",而*
在表达式后的末尾表示在"..
"之后表示任何类中的任何方法".
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".
这篇关于包中所有方法的@AspectJ切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!