java - 包内所有方法的@AspectJ 切入点

标签 java spring aop aspectj spring-aop

我有一个特定包的工作代码,但我想为所有 controllersservicedao 包配置它 例如

  • 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

等等。 . . 那是我项目的基本包,有人可以帮助我如何去做,以便它适用于我的网络项目的所有类,包括 Controller ,在此先感谢。 . .

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

}

最佳答案

其中一种选择怎么样?

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,只是因为它更短且更易于阅读。正如您可能已经猜到的那样,.. 表示法表示“任何包或子包”,而 * 在表达式末尾的 .. 之后表示“任何类中的任何方法”。

关于java - 包内所有方法的@AspectJ 切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25992277/

相关文章:

java - 如何查询MyBatis<sql>片段的静态参数值?

java - 使用 Spring Cache 抽象和 JDBC DAO 的事务

java - 字符串中单词的出现次数(字数统计)

java - Android Studio 断言不起作用

java - 带有 Akka actor 的 UI 中的 Vaadin 通知

java - 在 Spring Boot 中重新创建查询

Spring /AO​​P : Best way to implement an activities log in the database

如果继承 BaseController,带有 OnException 的 ASP.NET MVC4 Controller 不起作用

spring - spring security内部使用AOP吗?

java - 为什么下划线不起作用?