aop和注释' selenitance'

发布于 2025-01-28 05:17:45 字数 1407 浏览 2 评论 0原文

让我们考虑以下情况。

@interface LoggedMethodInvocation{}

@LoggedMethodInvocation
@interface MonitoredMethodInvocation{}

我想要@MonitoredMethodInvocation注释意味着@loggedMethodInvocation注释。

class LoggingAOPConfig {
   
    @Pointcut("@annotation(LoggedMethodInvocation)")
    public void servicePointcut() {
    }

    @Around("servicePointcut()")
    public Object logMethodInvocation(ProceedingJoinPoint pjp) throws Throwable {
        // log the method invocation...
    }
}
class MonitoringAOPConfig {

    @Pointcut("@annotation(MonitoredMethodInvocation)")
    public void servicePointcut() {
    }

    @Around("servicePointcut()")
    public Object monitorResponseTime(ProceedingJoinPoint pjp) throws Throwable {
        // add some meters to the method invocation
    }
}

现在,我想介绍一些方法,这些方法应受到监视和记录。我想仅用一个注释来注释该方法,即@monitoredMethodInvocation

class SomeService {
   
    @MonitoredMethodInvocation
    Object someMethod(Object requestPayload) {
        // ...
        return responsePayload;
    }
}

但是,它没有播放,记录方面未进入帐户。

有Spring的AnnotationUtils.findannotation提供所需的功能(识别,是否应考虑@LoggedMethodInvocation)。但是,我不知道如何将其放入点尺寸配置中。

我应该如何修改日志记录AOP配置,以便它可以识别日志记录注释,即使它隐藏在@MonitoredMethodInvocation后面?

let's consider the following situation.

@interface LoggedMethodInvocation{}

@LoggedMethodInvocation
@interface MonitoredMethodInvocation{}

I would like the @MonitoredMethodInvocation annotation implying the @LoggedMethodInvocation annotation.

class LoggingAOPConfig {
   
    @Pointcut("@annotation(LoggedMethodInvocation)")
    public void servicePointcut() {
    }

    @Around("servicePointcut()")
    public Object logMethodInvocation(ProceedingJoinPoint pjp) throws Throwable {
        // log the method invocation...
    }
}
class MonitoringAOPConfig {

    @Pointcut("@annotation(MonitoredMethodInvocation)")
    public void servicePointcut() {
    }

    @Around("servicePointcut()")
    public Object monitorResponseTime(ProceedingJoinPoint pjp) throws Throwable {
        // add some meters to the method invocation
    }
}

Now I would like to introduce some method, which shall be both monitored and logged. And I would like to annotate the method only with one annotation, namely @MonitoredMethodInvocation.

class SomeService {
   
    @MonitoredMethodInvocation
    Object someMethod(Object requestPayload) {
        // ...
        return responsePayload;
    }
}

However it doesn't play, the logging aspect is not taken into the account.

There is spring's AnnotationUtils.findAnnotation which offers the needed functionality (of recognizing, whether the @LoggedMethodInvocation shall be considered). However, I don't know how to put this into the pointcut configuration.

How shall I modify the logging AOP config so it will recognize the logging annotation even if it is hidden behind the @MonitoredMethodInvocation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2025-02-04 05:17:45

使用来自 https://stackoverflow.com/a/a/38209801/3394495 的语法。

class LoggingAOPConfig {
   
    @Pointcut("@annotation(LoggedMethodInvocation)")
    public void servicePointcut() {
    }

    @Pointcut("execution(@(@LoggedMethodInvocation *) * *(..))")
    public void metaServicePointcut() {
    }

    @Around("servicePointcut() || metaServicePointcut()")
    public Object logMethodInvocation(ProceedingJoinPoint pjp) throws Throwable {
        // log the method invocation...
    }
}

Using the syntax from https://stackoverflow.com/a/38209801/3394495, this should work for your case:

class LoggingAOPConfig {
   
    @Pointcut("@annotation(LoggedMethodInvocation)")
    public void servicePointcut() {
    }

    @Pointcut("execution(@(@LoggedMethodInvocation *) * *(..))")
    public void metaServicePointcut() {
    }

    @Around("servicePointcut() || metaServicePointcut()")
    public Object logMethodInvocation(ProceedingJoinPoint pjp) throws Throwable {
        // log the method invocation...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文