help with AspectJ please

topic posted Mon, October 4, 2004 - 4:34 AM by  Stephen
Share/Save/Bookmark
Advertisement
I have to integrate AspectJ into a large software development system. Can someone give me a very minimal "hello world" example so I know how the AspectJ files work and how to compile from the command line? The docs I've found don't give any simple examples, and the friend I asked not only gave me some huge example, but seemed to give me the instructions for compiling with Ant, which we don't use. I just need a simple Hello World example - I guess just one java class and one aspect file, and how to compile via the command line. Thanks to anyone who can help.

-Stephen
posted by:
Stephen
Advertisement
Advertisement
  • Re: help with AspectJ please

    Mon, October 4, 2004 - 4:04 PM
    Hey

    I'm looking into AspectJ as well and have a question - What is the difference between a Concern and an Aspect? Is an Aspect just an implementation of a Concern?

    Thanks
  • Re: help with AspectJ please

    Wed, October 6, 2004 - 4:22 PM
    I just wrapped up a talk on AOP, so thought I'd post my example. From what I've seen, I don't think you *need* Ant for this, but it will definitely help.

    Anyway, let's take two classes, Logger, OrderProcessesor. Very simple:

    public class Logger
    {
    public static void log(String msg)
    {
    System.out.println(new java.util.Date()+" - "+msg);
    }
    }

    public class OrderProcessor
    {
    public static void processOrder(String productID, int quantity) throws Exception
    {
    System.out.println("Product ID "+productID+" ordered");
    }

    public static void main(String[] args) throws Exception
    {
    for (int i = 0; i < args.length; i++)
    {
    OrderProcessor.processOrder(args[i], 1);
    }
    }
    }

    Now let's take a simple crosscut: log before and after each method. Let's write the aspect for it.

    public aspect OrderProcessingAspect
    {
    pointcut callProcessing() : call(public static void OrderProcesser.process*(..));

    before() : callProcessing()
    {
    Logger.log("starting method");
    }

    after() : callProcessing()
    {
    Logger.log("Ending method");
    }
    }

    Then compile using "ajc" and run the OrderProcessor as normal. One snafu I ran into was during compilation. In order for the weaving to work properly, all files must be given to ajc at once. Maybe that's where you ran into problems.

    HTH, Smasharolla

Recent topics in "Java Monkeys"

Topic Author Replies Last Post
Javascripting costs Schirin 3 May 17, 2007
Design patterns and principles emblylan 6 February 14, 2007
open source hosting alternatives? Mark 2 February 3, 2007
Ruby > Java Unsubscribed 6 January 9, 2007