Learn Java for Web Development

(Tina Meador) #1

220 CHAPTER 5: Building Java Web Applications with Spring Web MVC























  1. <aop:pointcut id = "log"

  2. expression="execution( .getAllBooks())" />

  3. <aop:before pointcut-ref = "log"

  4. method="logBefore" />

  5. <aop:after pointcut-ref = "log"

  6. 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



  1. package com.apress.aop;

  2. import org.springframework.context.ApplicationContext;

  3. import org.springframework.context.support.ClassPathXmlApplicationContext;



  4. public class Driver {



  5. public static void main(String...args){

  6. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");



Free download pdf