java 如何使用 AspectJ 和 Tomcat 配置加载时编织?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10032092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:20:23  来源:igfitidea点击:

How to configure load-time weaving with AspectJ and Tomcat?

javatomcataspectjload-time-weavingperf4j

提问by Roman

I tried to configure load-time weaving (for doing profiling with Perf4J) in the next way:

我尝试以下一种方式配置加载时编织(用于使用 Perf4J 进行分析):

1) I added aop.xmlto META-INFfolder. When deployed, META-INF is placed in the artifact root directory (i.e. MyAppDeployed/META-INF).

1)我添加aop.xmlMETA-INF文件夹。部署时,META-INF 被放置在工件根目录(即MyAppDeployed/META-INF)中。

2) I put aspectjrt-1.6.1.jar, aspectjweaver-1.6.1.jar, commons-jexl-1.1.jar, commons-logging.jarto the Tomcat/libfolder (at first I tried MyAppDeployed/WEB-INF/libsbut it also didn't work).

2) 我将aspectjrt-1.6.1.jar, aspectjweaver-1.6.1.jar, commons-jexl-1.1.jar,commons-logging.jar放到Tomcat/lib文件夹中(起初我尝试过,MyAppDeployed/WEB-INF/libs但也没有用)。

3) I added -javaagent:C:\apache-tomcat-6.0.33\lib\aspectjweaver-1.6.1.jarto VM options when starting Tomcat.

3)我-javaagent:C:\apache-tomcat-6.0.33\lib\aspectjweaver-1.6.1.jar在启动Tomcat时添加了VM选项。

4) My aop.xml:

4)我的aop.xml

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">

<aspectj>

    <aspects>
        <aspect name="org.perf4j.log4j.aop.TimingAspect"/>
    </aspects>

    <weaver options="-verbose -showWeaveInfo">           
        <include within="com.mypackages.MyClass"/>
    </weaver>
</aspectj>

I don't see any signs that load-time weaving happens. Neither error-reports nor necessary results. The only error message I have is:

我没有看到任何迹象表明加载时编织发生。既没有错误报告也没有必要的结果。我唯一的错误消息是:

Error occurred during initialization of VM
agent library failed to init: instrument
Error opening zip file: C:\apache-tomcat-6.0.33\lib\wrong-jar.jar

in a case when I do a mistake in a aspectjweaver-1.6.1.jarname when specify a javaagent parameter. If it's written correctly - no error messages are printed.

aspectjweaver-1.6.1.jar指定 javaagent 参数时我在名称中出错的情况。如果写入正确 - 不会打印错误消息。

Any ideas, what am I doing wrong?

任何想法,我做错了什么?

P.S. I use Java 5, and I tried the same things with 1.5.4version of the aspectj with exactly the same results.

PS我使用Java 5,我用1.5.4aspectj的版本尝试了同样的事情,结果完全相同。

回答by azizunsal

If you want to use load time weaving, you can first compile your classes with javacas usual then compile your aspect(s) with (i)ajc. You can do this with an ant task like below

如果您想使用加载时编织,您可以像往常一样首先使用javac编译您的类,然后使用(i)ajc编译您的方面。您可以使用如下所示的 ant 任务执行此操作

<target name="compile-aspect">
    <iajc source="1.6" target="1.6" showweaveinfo="true" verbose="true" outxml="true" debug="true" outjar="${dist.dir}/myaspect.jar">
            <argfiles>
                    <pathelement location="${src.dir}/sources.lst"/>
            </argfiles>
            <classpath>
                    <path refid="master-classpath" />
            </classpath>
    </iajc>
</target>

It is enough to have aspectjrt.jarin the classpath ("master-classpath") during compilation.

在编译期间在类路径(“master-classpath”)中有aspectjrt.jar就足够了。

Since all of my Java classes in ${src.dir}, I give a source list to iajc. In source list there is only one line.

因为我所有的 Java 类都在${src.dir} 中,所以我向 iajc 提供了一个源列表。源列表中只有一行。

sources.lst

来源.lst

com/xx/yy/zz/LoggingAspect.java

I set some of iajc task's attributes as follows

我设置了一些iajc任务的属性如下

outxml="true"
outjar="jar_file_name"

When I run compile-aspect task I have a jar jar_file_name.jarfile contains

当我运行 compile-aspect 任务时,我有一个 jar jar_file_name.jar文件包含

META-INF/MANIFEST.MF
com/xx/yy/zz/LoggingAspect.class
META-INF/aop-ajc.xml

And finally add the *jar_file_name.jar* to your web application's WEB-INF/libfolder.

最后将 *jar_file_name.jar* 添加到您的 Web 应用程序的WEB-INF/lib文件夹中。

Then start Tomcat with -javaagents:/path_to_aspectjweaver.jaras you did before.

然后像之前一样使用-javaagents:/path_to_aspectjweaver.jar启动 Tomcat 。

When I put the aop.xml (or aop-ajc.xml) in META-INF under the war file directly it doesn't work.This way (I mean seperating aspect classes into a jar) just works fine for me.

当我将 aop.xml(或 aop-ajc.xml)直接放在 war 文件下的 META-INF 中时,它不起作用。这种方式(我的意思是将方面类分成一个罐子)对我来说很好用。

Hope this helps.

希望这可以帮助。

回答by Naresh Dhiman

I was trying to solve the same issue in Tomcat. In addition to -javaagent option, you need to make sure the aop.xml must be under the WEB-INF/classes/META-INF dir. This made the difference for me.

我试图在 Tomcat 中解决同样的问题。除了 -javaagent 选项之外,您还需要确保 aop.xml 必须在 WEB-INF/classes/META-INF 目录下。这对我来说很重要。