简体   繁体   English

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

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

I tried to configure load-time weaving (for doing profiling with Perf4J) in the next way:我尝试通过以下方式配置加载时编织(用于使用 Perf4J 进行分析):

1) I added aop.xml to META-INF folder. 1) 我将aop.xml添加到META-INF文件夹中。 When deployed, META-INF is placed in the artifact root directory (ie MyAppDeployed/META-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.jar to the Tomcat/lib folder (at first I tried MyAppDeployed/WEB-INF/libs but it also didn't work). 2)我把 aspectjrt- aspectjrt-1.6.1.jaraspectjweaver-1.6.1.jarcommons-jexl-1.1.jarcommons-logging.jarTomcat/lib文件夹(起初我尝试MyAppDeployed/WEB-INF/libs没用)。

3) I added -javaagent:C:\apache-tomcat-6.0.33\lib\aspectjweaver-1.6.1.jar to VM options when starting Tomcat. 3) 我在启动 Tomcat 时将-javaagent:C:\apache-tomcat-6.0.33\lib\aspectjweaver-1.6.1.jar到 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.jar name when specify a javaagent parameter.如果我在指定 javaagent 参数时在aspectjweaver-1.6.1.jar名称中出错。 If it's written correctly - no error messages are printed.如果它写得正确 - 不会打印错误消息。

Any ideas, what am I doing wrong?任何想法,我做错了什么?

PS I use Java 5, and I tried the same things with 1.5.4 version of the aspectj with exactly the same results. PS 我使用 Java 5,我用1.5.4版本的 aspectj 尝试了相同的操作,结果完全相同。

If you want to use load time weaving, you can first compile your classes with javac as usual then compile your aspect(s) with (i)ajc .如果你想使用加载时间编织,你可以先像往常一样用javac编译你的类,然后用(i)ajc编译你的方面。 You can do this with an ant task like below您可以使用如下所示的 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.jar in 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.由于我的所有 Java 类都在${src.dir}中,所以我将源列表提供给 iajc。 In source list there is only one line.在源列表中只有一行。

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.jar file contains当我运行编译方面的任务时,我有一个 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/lib folder.最后将 *jar_file_name.jar* 添加到您的 web 应用程序的WEB-INF/lib文件夹中。

Then start Tomcat with -javaagents:/path_to_aspectjweaver.jar as 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.当我直接将 META-INF 中的 aop.xml(或 aop-ajc.xml)放在 war 文件下时,它不起作用。 This way (I mean seperating aspect classes into a jar) just works fine for me.这种方式(我的意思是将方面类分离到一个 jar 中)对我来说很好用。

Hope this helps.希望这可以帮助。

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.我试图在 Tomcat 中解决相同的问题。除了 -javaagent 选项之外,您还需要确保 aop.xml 必须位于 WEB-INF/classes/META-INF 目录下。 This made the difference for me.这对我来说很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM