Datasheet
This code prints a logging message, similar to the following, whenever a method on the Operation
interface (from the CalculateSpring code) is called:
AOP logging -> execution(getOpsName)
Try It Out Experimenting with Spring AOP Support
Once you have created an aspect, in this case a logging aspect, you can apply it to a target (in this
case the
CalculateSpring components) through the context descriptor. Try applying the aspect to the
CalcuateSpringAOP class (just the good old CalculateSpring class renamed) by following these steps:
1. Change the directory to the springaop project in the source code distribution:
cd src/chapter1/springaop
2. Compile code using Maven 2, via the following command:
mvn compile
3. Run the code using Maven 2 by typing the following on one line:
mvn exec:java -Dexec.mainClass=com.wrox.begspring.CalculateSpringAOP
–Dexec.args=”3000 3”
4. Take a look at the output; it should look like this:
AOP logging -> execution(getOpsName)
AOP logging -> execution(operate)
The result of 3000 times 3 is 9000!
Note that the executions of both the getOpsName() and operate() methods of the Operation inter-
face are logged. Yet the implementations of this interface — such as
OpAdd or OpMultiply — are not
logged at all.
How It Works
The main code for CalculateSpring is renamed CalculateSpringAOP, and is shown here:
package com.wrox.begspring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CalculateSpringAOP {
private Operation ops;
private ResultWriter wtr;
22
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 22