当前位置: 首页 > 面试题库 >

包中所有方法的@AspectJ切入点

邵赞
2023-03-14
问题内容

我有针对特定软件包的此工作代码,但我想针对所有 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

等等。。。那是我项目的基本软件包,有人可以帮忙我如何做,以便它可以用于我的Web项目的所有类,包括控制器,在此先感谢。。。

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,因为它更短并且更容易阅读。您可能已经猜到了,..表示法的意思是“任何包或子包”,而*表达式的末尾..表示“任何类中的任何方法”。



 类似资料:
  • 我想从类列表(可能属于不同的包)中记录所有方法的条目。注意,这些方法应该只属于指定的类。 我尝试了以下方法,但这些都不起作用 (1) 在这里使用if()切入点,我得到一个错误 (2) 使用切入点和aop的组合。xml在这里我得到一个错误 这里出了什么问题? 当然可以通过在切入点中单独指定每个类来实现,但这对于数百个类来说是不可扩展的。理想情况下,如果可以从外部文本文件中提取类列表(便于配置),那就

  • 我有一个特定包的工作代码,但我想为所有控制器、服务和dao包配置它,例如 com。abc。xyz。所容纳之物控制器 com。abc。xyz。所容纳之物服务 com。abc。xyz。所容纳之物道 com。abc。xyz。类别控制器 com。abc。xyz。类别服务 com。abc。xyz。类别dao公司 等等这是我的项目的基本包,有人可以帮助我如何去做它,使它适用于我的网络项目,包括控制器的所有类,

  • 我使用Spring的和注释来实现一个简单的CRUD-app,可以通过RESTful API使用。现在,我想在我的存储库上添加一个AspectJ切入点,这样每当调用接口中的CRUD-method时,就会执行一些功能。 首先,我扩展了Spring的,以便在自己的接口中添加一些自定义功能: 一切都很好,我可以通过REST客户端调用这个方法。我不必实现接口,因为Spring在后面的工作中是一个奇迹。这是扩

  • 问题内容: 我想用指定的注释(例如@Monitor)监视所有类的所有公共方法(注意:注释在类级别)。可能的切入点是什么?注意:我正在使用@AspectJ样式的Spring AOP。 问题答案: 你应该将类​​型切入点与方法切入点结合使用。 这些切入点将在标记为@Monitor的类中查找所有公共方法: 为结合了前两者的最后一个切入点提供建议,你就完成了!

  • add:如果我将方法存根添加到Fragment2中,就可以开始使用next annotation,但这是一个非常难看的解决方案 解决方案:多亏了@Kriegaex,我找到了解决方案:

  • 服务实现 但是,如果我将注释移动到公共接口方法实现,我的方面就会被触发。我应该如何定义我的切入点或配置我的方面来使我的原始用例工作?