Datasheet

<bean id=”screen” class=”com.wrox.begspring.ScreenWriter” />
<bean id=”multiply” class=”com.wrox.begspring.OpMultiply” />
<bean id=”opsbean” class=”com.wrox.begspring.CalculateSpringAOP”
autowire=”byType”/>
<aop:aspectj-autoproxy/>
<bean id=”logaspect” class=”com.wrox.begspring.aspects.LoggingAspect”/>
</beans>
The Spring container processes the <aop:aspectj-autoproxy> element and automatically creates a
dynamic proxy required for AOP when it wires the beans together. Proxies are automatically added to the
CaculateSpringAOP class by Spring, allowing for interception and the application of the LoggingAspect.
Note that you must add the highlighted AOP schema and namespace before the
<aop:aspectj-
autoproxy>
element can work.
In this case, Spring 2 uses the AspectJ pointcut language (described in the next section) to determine where
the proxies should be added for interception by the aspect.
Matching Join Points via AspectJ Pointcut Expressions
Java annotations are used to specify the pointcut to match when applying the aspect. The code responsible
for this is highlighted 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 @Aspect annotation marks this class as an Aspect. The @Before() annotation specifies the actual
pointcut. The AspectJ pointcut expression language is used here to match a join point — a candidate spot
in the target code where the aspect can be applied. This expression basically says, Apply this aspect before
you call any method on the type
com.wrox.begspring.Operation.
Since
com.wrox.begspring.Operation is actually an interface, this results in aspect application before
execution of any method on any implementation of this interface.
24
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 24