esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I get started with AspectJ?

  1. Download AspectJ
  2. Run java -jar aspectj-[version].jar and perform the installation.
  3. Add the bin directory of to your path to make it easier to run the AsjpectJ compiler ajc.
  4. Add aspectjrt.jar to your classpath.
  5. 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:

    This code sample is only viewable to esus.com members
    Login or become a member!


  6. Running this code yields:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


  7. Now write the Logging Aspect.

    TraceAspect.java:

    This code sample is only viewable to esus.com members
    Login or become a member!


  8. Compile both the .java files with the ajc:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


  9. Running Test again results in the logging statements.

     
    This code sample is only viewable to esus.com members
    Login or become a member!


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

     
    This code sample is only viewable to esus.com members
    Login or become a member!





Further Information
Author of answer: Joris Van den Bogaert

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.