Aspectj Cookbook

Recipe 2.5. Weaving Aspects at Load Time

Problem

You want to postpone the decision as to whether an aspect should be applied to a particular application until the application is being loaded into the Java Virtual Machine.

Solution

Use the new load-time weaving features of AspectJ 1.2.

Discussion

Using the code shown in Recipe 2.2, the following instructions show you how to apply the HelloWorld aspect to the MyClass class at load time:

  1. Compile the MyClass class using the traditional javac command:

    > javac -classpath %MY_CLASSPATH% -d %MY_DESTINATION_DIRECTORY% com/oreilly/ aspectjcookbook/MyClass.java

    By using the javac command, you are completely avoiding weaving any aspects into your application at compile time. If you have some aspects that you want to include at compile time, you can use the ajc command and list the aspects to be included at that point in the AspectJ build configuration file, not specifying any aspects you intend to only weave at load time.

  2. You can now check to ensure that the MyClass class has been compiled without any aspects by running the application using the java command:

    > java com.oreilly.aspectjcookbook.MyClass Inside foo (int, String)

  3. Compile the HelloWorld aspect into an aspect library .jar file using the ajc command:

    > ajc -outjar helloworldlibrary.jar -d %MY_DESTINATION_DIRECTORY% com/oreilly/ aspectjcookbook/HelloWorld.java

    You may find that you get the following warning stating that there is no match to the pointcut logic declared in your HelloWorld aspect:

    warning no match for this type name: com$oreilly$aspectjcookbook$MyClass [Xlint: invalidAbsoluteTypeName] call(void com.oreilly.aspectjcookbook.MyClass.foo(int, String)); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    You are not weaving your aspect into the MyClass class at this point, so the warning is expected and can be ignored in this case.

  4. You are now all set to run your separately compiled class and aspect, weaving them together at load time. A script is provided in AspectJ 1.2 to help you run the load-time weaver. This script is currently located in the %MY_HOME_DIRECTORY%/aspectj1.2/doc/examples/ltw directory and is called aj or aj.bat. Just as the ajc command is the AspectJ equivalent of the java command, so the aj script can be seen as the foundation for an AspectJ equivalent of the java command.

    In AspectJ 1.2, the aj script is located in an example directory as part of the AspectJ documentation. In future versions of AspectJ, as the load-time weaving facilities mature, it is likely that the aj script will join the more mainstream scripts in the %ASPECTJ_INSTALL_DIRECTORY%/bin directory.

  5. The aj command relies on three environment variables being set so that it can pick up the relevant aspect libraries and Java classes for your application. You need to set the following variables to the appropriate project and AspectJ installation locations:

    CLASSPATH

    The location of your project's classes and the aspectjrt.jar is usually installed to %MY_HOME_DIRECTORY%/aspectj1.2/lib.

    ASPECTPATH

    Points to any .jar files that contain aspects to be woven at load time. In this example, this variable should point to the helloworldlibrary.jar file.

    ASPECTJ_HOME

    Contains the location of your AspectJ installation.

  6. You can now run your application using the aj command. The aspects contained in the helloworldlibrary.jar are woven at load time into the MyClass class:

    > %ASPECTJ_INSTALL_DIRECTORY%/doc/examples/ltw/aj com.oreilly.aspectjcookbook.MyClass Hello World In the advice attached to the call point cut Inside foo (int, String)

In addition to the default implementation of load-time weaving supplied with AspectJ 1.2, you can create your own implementation by extending and using the classes provided in the org.aspectj.weaver.tools package. This is an advanced topic for developers who are working with the AspectJ tools and is not covered here.

This recipe shows you how to work with load-time weaving with the current implementation in AspectJ 1.2. Although load-time weaving of aspects is an important addition to the toolset, it is a fairly new feature in AspectJ and the current implementation is likely to change as it matures.

See Also

Setting up your environment in order to compile your AspectJ projects from the command line is covered in Recipe 2.1; the AspectJ Build Configuration file is shown in more detail in Recipe 2.3; another example of load-time weaving is provided by Adrian Colyer at http://www.jroller.com/comments/colyer/Weblog/load_time_weaving_with_aspectj; a full explanation of the load-time weaving facilities in AspectJ 1.2 is available as part of the documentation for AspectJ and is installed to %ASPECTJ_INSTALL_DIRECTORY%/doc/README-12.html.

Категории