javaaopaspectjload-time-weaving

Pure AspectJ load time weawing does not work


I am trying to run an example of AspectJ load time weawing. First I tried to do something myself, and nothing worked. Normal weawing works fine but when I try to pointcut some library using load time weawing - nothing happens.

Here is the example repository I am using - https://github.com/medvedev1088/aspectj-load-time-weaving-example

I made two cahnges:

1) I added class Main -

package com.example.aspectj;

import org.joda.time.Chronology;
import org.joda.time.base.AbstractDateTime;

public class Main {
    public static void main(String[] args) {
        AbstractDateTime time = new AbstractDateTime() {
            @Override
            public long getMillis() {
                return 0;
            }

            @Override
            public Chronology getChronology() {
                return null;
            }
        };
        System.out.println(time.toString());
    }
}

2) I added following plugin to pom.xml to create runnable jar -

...
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <groupId>org.apache.maven.plugins</groupId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <id>make-executable-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.example.aspectj.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>
...

I run this jar from a target directory using the following command:

java -jar aspectj-ltw-example-1.0-SNAPSHOT-jar-with-dependencies.jar

After this method that should be pointcutted is executed but ascpect is not.


Solution

  • You need to make sure that the weaving agent is started, it does not happen automatically for LTW scenarios.

    java -javaagent:/path/to/aspectjweaver.jar -jar my.jar
    

    This is also described in the AspectJ manual.

    Caveat: Argument order is important. Make sure to put -javaagent before -jar, because everything after -jar will be interpreted as application arguments, not JVM arguments.