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

前端 未结 2 909
遇见更好的自我
遇见更好的自我 2021-02-06 06:00

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

1) I added aop.xml to META-INF folder. When deployed,

相关标签:
2条回答
  • 2021-02-06 06:51

    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. You can do this with an ant task like below

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

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

    sources.lst

    com/xx/yy/zz/LoggingAspect.java
    

    I set some of iajc task's attributes as follows

    outxml="true"
    outjar="jar_file_name"
    

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

    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.

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

    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.

    Hope this helps.

    0 讨论(0)
  • 2021-02-06 06:58

    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.

    0 讨论(0)
提交回复
热议问题