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
-Stephen
Advertisement
Advertisement
-
Re: help with AspectJ please
Mon, October 4, 2004 - 9:14 AMcan't help you directly, but there seems to be a book on it: www.amazon.com/exec/obido...638-1512167
sadly, given the bleeding-edge nature of the thing and the Java world's obsession with Ant, it's worth being open to the possibility that there's no way to use AspectJ without Ant, or that doing so will take a lot of work.
-
Re: help with AspectJ please
Mon, October 4, 2004 - 4:04 PMHey
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 PMI 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