|
Java™ by example!
|
|
|
How do I get started with AspectJ?
- Download AspectJ
- Run java -jar aspectj-[version].jar and perform the installation.
- Add the bin directory of to your path to make it easier to run the AsjpectJ compiler ajc.
- Add aspectjrt.jar to your classpath.
- The goal of this example is to extend this simple class with logging functionality. Every time a method is called or exited, a message must be logged to stdout.
Test.java:
- Running this code yields:
- Now write the Logging Aspect.
TraceAspect.java:
- Compile both the .java files with the ajc:
- Running Test again results in the logging statements.
- Notice that the original code in Test.java was not touched. But if you decompile Test.class with Jad, you'll see why Test behaves the way it does, with the logging:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|