在AspectJ中添加有关最终方法的建议 [英] Adding advice around final methods in AspectJ

查看:140
本文介绍了在AspectJ中添加有关最终方法的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用AspectJ在最终方法周围添加建议?我完全意识到使用Spring AOP是不可能的.但是我找不到与AspectJ相关的任何东西.

Is it possible to add advice around a final method using AspectJ? I am totally aware that it is impossible using Spring AOP. But I couldn't find anything related to AspectJ.

推荐答案

个人简介很抱歉,但是我想这样说:您想使用AspectJ.您为什么不尝试?,您得到的结果要比在这里写下问题并等待别人回答的结果要快得多.您的问题格式也不适合SO,因为您不会发布任何遇到问题的代码,而只问一个一般性问题.

Sorry for the introductory personal opinion, but I want to say this: You want to use AspectJ. Why don't you just try? You get a result much faster than by writing a question here and waiting for someone to answer it. Your question format is also unsuited for SO because you don't post any code you have trouble with, you just ask a general question.

是的,使用AspectJ可以检测最终的类和/或方法:

Yes, instrumenting final classes and/or methods is possible with AspectJ:

驱动程序应用程序以及最终方法:

package de.scrum_master.app;

public class Application {
  public final void doSomething() {}

  public static void main(String[] args) {
    new Application().doSomething();
  }
}

方面的原生AspectJ语法:

package de.scrum_master.aspect;

public aspect MyAspect {
  Object around() : execution(* doSomething()) {
    System.out.println(thisJoinPoint);
    return proceed();
  }
}

使用@AspectJ语法的方面:

我真的更喜欢本机语法,但是无论如何,由于某些未知的原因,有些人似乎更喜欢这种丑陋且冗长的版本,带有导入,抛出,显式声明的连接点实例以及更复杂的处理方式:

I really prefer the native syntax, but anyway, for some unknown reason some people seem to prefer this ugly and wordy version with imports, throws, explicitly declared joinpoint instances and more complicated way to proceed:

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class MyAspect {
  @Around("execution(* doSomething())")
  public Object aroundAdvice(ProceedingJoinPoint thisJoinPoint) throws Throwable {
    System.out.println(thisJoinPoint);
    return thisJoinPoint.proceed();
  }
}

控制台日志:

两个方面的效果都相同:

The effect is the same for both aspect variants:

execution(void de.scrum_master.app.Application.doSomething())

这篇关于在AspectJ中添加有关最终方法的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆