Java Reference
In-Depth Information
12.
13. <bean id="bookService" class="com.apress.aop.BookServiceImpl"/>
14.
15. <bean id="logAspect" class="com.apress.aop.LoggingAspect"/>
16.
17. <aop:config>
18. <aop:aspect ref = "logAspect">
19. <aop:pointcut id = "log"
20. expression="execution(* *.getAllBooks())" />
21. <aop:before pointcut-ref = "log"
22. method="logBefore" />
23. <aop:after pointcut-ref = "log"
24. method="logAfter" />
25. </aop:aspect>
26. </aop:config>
27. </beans>
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.
5. public class Driver {
6.
7. public static void main(String...args){
8. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
9.