AspectJ 是如何工作的? [英] How does AspectJ work?

查看:22
本文介绍了AspectJ 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 Aspect 的工作原理.我来自 C/C++ 背景,魔法永远不会发生.

I am trying to understand how Aspect works. I come from a C/C++ background where magic never happens.

我知道你可以用 @Aspect 注释一些函数,然后写下 Aspect 实现等等.但是,新代码是如何(以及在​​什么时间)生成的?

I understand that you can annotate some functions with @Aspect then write down the Aspect implementation and so on. But, how (and at what time) does the new code get generated?

假设我没有编辑器.我使用 javacc 命令编译 java 类这将生成 .class 文件.现在,假设 Java 文件是使用 Aspect 注释的.然后我不应该用 Aspect 再次编译 .class 文件以生成另一组 .class 文件吗?

Assuming I have no editor. I compile java classes using javacc command that will generate .class files. Now, assume that the java files are annotated using Aspect. Shouldn't I then compile the .class files again with Aspect somehow to generate another set of .class files?

如果我的理解是正确的,这个双重编译步骤在maven中是如何完成的?还是春天?我找到了许多教程,它们会告诉您在此处和那里添加哪些内容才能使事情正常进行,但没有任何教程解释这些事情实际上是如何工作的.

If my understanding is correct, how is this dual compilation step done in maven? or spring? I found many tutorial that will tell you what to add here and there to get things working but no tutorial explains how these things are actually working.

推荐答案

很容易说你是一个 C++ 人.没有 javacc 这样的东西(如果你不是在谈论同名的 Java Parser Generator),但 Java 编译器被称为 javac.;-)

It is easy to tell that you are a C++ guy. There is no such thing as javacc (if you are not talking about the Java Parser Generator of the same name) but the Java Compiler is called javac. ;-)

正如 Philipp Wendler 已经指出的那样,Eclipse 不仅仅是一个 IDE,它是一个类似于 Apache 的组织,AspectJ 多年前就被采用为它的项目之一.这还有一个优点,即 Eclipse IDE 的 AJDT(AspectJ 开发工具)是您可以为任何 IDE 获得的最佳 AspectJ 支持,遗憾的是包括我最喜欢的 IntelliJ IDEA,它几乎在所有方面都优于 Eclipse IDE,除了它的 AspectJ 支持是比较一般.

As Philipp Wendler already pointed out, Eclipse is not just an IDE, it is an organisation similar to Apache, and AspectJ has been adopted many years ago as one of its projects. This has also the advantage that AJDT (AspectJ Development Tools) for Eclipse IDE are sort of the best AspectJ support you can get for any IDE, sadly including my favourite IntelliJ IDEA which is superior to Eclipse IDE in almost all respects except its AspectJ support being rather mediocre.

关于次要的主题就这么多.现在至于您的主要问题,它并不真正适合 Stack Overflow,因为它是一个论坛问题,而不是需要解决方案的具体编程问题.无论如何,我喜欢 AspectJ 并会简要回答:

So much for the minor topics. Now as for your main question, it is not really suited for Stack Overflow because it is rather a forum question, not about a concrete programming problem requiring a solution. Anyway, I love AspectJ and will answer briefly:

AspectJ 有自己的编译器 ajc,它基本上是 Eclipse Java 编译器 ejc 的增强版本,即它可以用来编译普通的 Java 文件以及方面以原生 AspectJ 语法或@AspectJ 风格的基于注解的语法.无论您以哪种方式编织您的方面,例如

AspectJ has its own compiler ajc which is basically an enhanced version of the Eclipse Java Compiler ejc, i.e. it can be used to compile normal Java files as well as aspects in native AspectJ syntax or in @AspectJ style annotation-based syntax. No matter which way you weave your aspects, e.g.

  1. 使用 ajc 从头开始​​构建方面 + Java 代码(编译时编织),

  1. build aspect + Java code with ajc from scratch (compile-time weaving),

仅使用 ajc 构建方面,使用 javacejc 或任何其他 Java 编译器构建 Java 代码,然后

build only aspects with ajc and Java code with javac, ejc or any other Java compiler, then

通过ajc(编译后或二进制编织)或

weave aspects into Java class files via ajc (post-compile or binary weaving) or

在类加载期间在运行时使用称为 AspectJ Weaver(加载时编织,LTW)的 Java 代理将方面编织到 Java 类文件中,

weave aspects into Java class files at runtime during class-loading with a Java agent called the AspectJ Weaver (load-time weaving, LTW),

AspectJ 所做的几乎总是相同的:它通过将方面代码编织到其中来修改 Java 字节码.在第 1 种情况下,您只需直接从 ajc 获得一组类文件.案例 2.1 创建了额外的新类文件.Case 2.2 只是在 JVM 中直接在内存中创建新的字节码.

What AspectJ does is always pretty much the same: It modifies Java byte code by weaving aspect code into it. In case 1 you just get one set of class files directly from ajc. Case 2.1 creates additional, new class files. Case 2.2 just creates new byte code in memory directly in the JVM.

这篇关于AspectJ 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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