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

查看:79
本文介绍了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文件.现在,假设使用Aspect对Java文件进行了注释.然后,我不应该再用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. ;-)

Yeah, 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.

次要主题太多了.现在,对于您的主要问题,它实际上并不适合StackOverflow,因为它是一个论坛问题,而不是涉及需要解决方案的具体编程问题.无论如何,我爱AspectJ,并会简要回答:

So much for the minor topics. Now as for your main question, it is not really suited for StackOverflow 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代码(编译时编织),
  2. 仅使用ajc构建方面,并使用javacejc或任何其他Java编译器构建Java代码,然后

  1. build aspect + Java code with ajc from scratch (compile-time weaving),
  2. build only aspects with ajc and Java code with javac, ejc or any other Java compiler, then

  1. 通过ajc(后编译或二进制编织)或
  2. 将方面编织到Java类文件中
  3. 在类加载期间使用称为AspectJ Weaver的Java代理(加载时编织,LTW)在运行时将方面织入Java类文件中,
  1. weave aspects into Java class files via ajc (post-compile or binary weaving) or
  2. 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会创建其他新的类文件.情况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.

我希望这会有所帮助.让我们不要进行长时间的讨论,好吗?如果您有后续问题,让我们限制在最低限度,好吗?

I hope this helps. Let us not make this a long discussion, okay? If you have follow-up questions, let us limit to a minimum, okay?

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

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