220 CHAPTER 5: Building Java Web Applications with Spring Web MVC
- <aop:pointcut id = "log"
- expression="execution( .getAllBooks())" />
- <aop:before pointcut-ref = "log"
- method="logBefore" />
- <aop:after pointcut-ref = "log"
- method="logAfter" />
Lines 9 to 10: You use Spring’s aop configuration namespace to declare that the
LoggingAspect bean is an aspect.
Line 15: You declare the LoggingAspect as a bean. Even if the Spring Framework
transforms a POJO to an aspect by declaring it as an aspect in the context, it
still has to be declared as a Spring <bean>.
Line 18: Then you refer to that bean in the <aop:aspect> element.
Lines 19 to 20: The pointcut is defined in the preceding <pointcut> element with
an expression attribute set to select where the advice should be applied. The
expression syntax is AspectJ’s pointcut expression language.
Lines 21 to 22: You declare (using <aop:before>) that before the getAllBooks()
method is executed, the LoggingAspect’s logBefore method should be called.
This is called before advice. The pointcut-ref attribute refers to a pointcut
named log.
Lines 23 to 24: You (using <aop:after>) declare that the logAfter method should
be called after getAllBooks() has executed. This is known as after advice. The
pointcut-ref attribute refers to a pointcut named log.
Listing 5-22 illustrates the stand-alone Java application.
Listing 5-22. Stand-Alone Java Application
- package com.apress.aop;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Driver {
- public static void main(String...args){
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");