Download Wiley Beginning Spring Framework 2
Transcript
01612c01.qxd:WroxPro
10/31/07
10:42 AM
Page 21
Chapter 1: Jump Start Spring 2
SPRING AOP
Existing target code modules
Matches pointouts and apply aspects
Apply
aspect
here
Matched
JoinPoint
Logging aspect (code)
Apply
aspect
here
Apply
aspect
here
Matched
JoinPoint
Matched
JoinPoint
Figure 1-4
Adding a Logging Aspect
In this section, you can try out Spring AOP support by applying a logging aspect to the existing calculation code.
The code for the logging aspect is the com.wrox.begspring,aspects.LoggingAspect class, which
you can find under the src\chapter1\springaop\src\main\java\com\wrox\begspring\aspects
directory; it is shown here:
package com.wrox.begspring.aspects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before(“execution(* com.wrox.begspring.Operation.*(..))”)
public void logMethodExecution(JoinPoint jp) {
System.out.println(“AOP logging -> “
+ jp.toShortString() );
}
}
The logging aspect that can be applied is in boldface in the preceding code. Note that there is nothing
about this aspect code that refers to the CalculateSpring code — the target to which it is applied. In
fact, this aspect can be coded, modified, and maintained independently of the CalculateSpring code.
21