Spring 2.5 Aspect Oriented Programming


Author(s): Massimiliano Dessi
ISBN: 1847194028
Published: February 2009
Relevance: 4/5
Readability: 5/5
Overall: 4/5

More Packt Publishing Books Reviews …

This book, teaches us Spring 2.5.x with Aspect Oriented Programming (Annotations and XML well cover) with a scalable learning approach, over 8 well-organized chapters, realistic and necessary Crosscutting Concerns are covered.

This Book is available here

Chapter 1: Understanding AOP Concepts

As an introduction, this chapter the author teaches us, in a concrete and well explained way, the limits of OOP. Some graphics are used to get a better understanding of this situation, furthermore two important problems about coding with OOP are explained in a concrete way with a sample code for Code Scattering and Code Tangling.

With this premise AOP is introduced as a solution. Here the author use 2 graphics to show us a representation of sequence of calling methods among objects with pure OOP and AOP. Therefore the author shows us a better understanding of the parts and its functions as components of AOP. There is a good the explanation again of the AOP components (theory way). After that, the types of Advice are introduced (even After(finally) ) and Spring is introduced in terms of AOP Code sample for "Programmatic Way" for each of the next advices

  • Before
  • After Returning
  • Around
  • After Throwing

The old Spring XML way is introduced (with ProxyFactoryBean), "Static AspectJ" and "Dynamic Spring" implementations are introduced, along with a comparison between them. Furthermore there is a comparison of "Spring AOP" with "AspectJ".

The book is oriented for Spring 2.5, therefore AspectJ annotations (<aop:aspectj-autoproxy/> introduced) with code sample are introduced for the next advices:

  • Before
  • After Returning
  • Around
  • After (finally)
  • After Throwing

And furthermore "Schema-based configuration" is introduced too for the following advices:

  • Before
  • After
  • After Returning
  • After Throwing
  • Around

You can see all advices types are explained with these two approach forms.

Chapter 2: Spring AOP Components

This chapter offers an explanation of terms for "Aspect", "Pointcut" , detailed explanations and excellent code (Java and XML) for pointcuts already provided by Spring, that means for:

  • NameMatchMethodPointcut
  • RegexpMethodPointcut
  • StaticMethodMatcherPointcut
  • DynamicMethodMatcherPointcut

"Operations on Pointcut" are explained like "ComposablePointcut" therefore "union" and "intersection" methods are introduced and well explained in code sample,
"ControlFlowPointcut" are covered too.

More terms like "Joinpoint", "Advice" are explained, with an interesting graphic for method "proceed()" about chain of interceptors are available for better understanding; Before, After Returning, After Throwing are explained with code. Another term is "Advisor" which is explained with extracts of code.

An important component is "Introductions"; almost six pages are dedicated for this topic. Something that I don’t like about this part is that it does not teach some "reason of the things" in some parts of the code.

Chapter 3: Spring AOP Proxies

This is an important chapter – "Proxy" is introduced and explained with an important theory. After that JDK and GLIB proxy are presented and well explained. A section called "Creating proxies programmatically" starts with

  • ClassicProxy
  • AspectJProxy

Both are well covered with good theory and code samples, the following topic covered is:

  • ProxyFactoryBean

This covered really well, with many properties explained in an excellent concrete way. Something that I like very much, is that it offers scenarios about strategies to select proxies – the code sample uses crosscutting concerns like Logging , Time Execution.
Furthermore for "XML configuration" is well cover using:

  • DefaultPointCutAdvisor
  • NameMatchMethodPointcut
  • ProxyFactoryBean (showing us three different approaches to work with interfaces and with interceptor Names)

A topic called "Advised objects" is introduced too, it has its own explanation and code sample. Now "Autoproxy" is introduced, covering "BeanNameAutoProxyCreator", "DefaultAdvisorAutoProxyCreator", both are well covered with a sample code again using "NameMatchMethodPointcut","DefaultPointCutAdvisor" how part of its XML configuration, "AutoProxyCreator with metadata" is cover too such as the previous already mentioned

After that, a brief but concrete explanation and configuration is offered for "AutoProxy with AspectJ, Annotation, XML Schema". Now "Target Sources" are covered with four interesting variations:

  • Hot Swappable
  • Pooling
  • Prototype
  • Thread local

Each topic is covered with a simple but concrete sample code and of course each corresponding configuration.

Chapter 4: AspectJ Support

This chapter start teaching us about some concepts and configuration for "Aspect" and "Pointcuts" and the presentation for PCD (Pointcut Designators) like:

  • execution, within, this, target, args
  • @target, @args, @within, @annotation

Most of these have sample code and do a comparison among themselves (i.e.: "execution" and "within" ). Here you learn how to declare an Aspect class dedicated only to declare Pointcuts (i.e ApplicationPointCutAspect) and other Aspect classes using only advice methods to call or use the pointcuts from the aspect class already mentioned. This chapter offers a list of the PCD’s not supported by Spring AOP, and a good amount of PCD samples
(more of ten that consist in a pointcut definition and its explanation) are offered.

Each group of the following selections are explained with a concrete sample

  • Selection method’s name
  • Selection on types of argument
  • Selection on type of return
  • Selection on declared exceptions
  • Selection on hierarchy
  • Selection on annotations

And for "Binding"

  • Binding arguments
  • Binding of return values
  • Exception Binding
  • Annotation Binding

All are well covered in a concrete way with annotations. Now for more detail about Advice:

  • @Before
  • @AfterReturning
  • @AfterThrowing
  • @After (After Finally)
  • @Around

The introduction is covered again and teaches us how use the JVM "javaagent" argument with Eclipse. Now is the turn of the "XML Schema-based configuration", again with the same order already mentioned in the beginning for this chapter "Aspect" and "Pointcuts" are covered in a simple and concrete way, therefore now is the turn for the "Advice"

  • @Before
  • @AfterReturning
  • @AfterThrowing
  • @After (After Finally)
  • @Around

For almost each advice shown above the chapter offers the xml configuration to work with/without binding for each situation. "Introduction" and "Advisor" is covered with its theory and code sample. The follows topics appears under the section "recipes"

  • Dependency Injection in domain objects (therefore @Configurable and its respective xml complement configuration are explained)
  • Advice ordering (Interface Ordered is introduced – annotation and its equivalent xml are mentioned)
  • Aspect instantiation model ("perthis" and "pertarget")

Something very important is LTW (Load Time Weaving)

  • AspectJ weaving in Spring (aop.xml mentioned)
  • Load-time weaving with Spring (<context:load-time-weaver/> and special command is shown )
  • Load-time weaving with AspectJ

"AOP strategy considerations" has a concrete list of conditions for:

  • Spring AOP proxy
  • Spring with AspectJ Weaver
  • AspectJ

Chapter 5: Design with AOP

Concurrency, is shown to us with two code samples. The first case, a class with some logic and thread safe control (read/write lock/unlock) together. After that, a custom Aspect class is created for thread safe control and the other class is modify,
now of course thread control are not include inside

In Caching, for learning purposes, EHCache is used, an Aspect class and a target class is used together with a xml configuration.

In Security, Spring Security is used now, in this situation "AccessDecisionManager" and "AccessDecisionVoter" interfaces are mentioned and its implementations are cover in the sample code ("RoleVoter" and "AuthenticatedVoter"),
now AOP is applied with three approaches:

  • Securing Methods with security interceptors
  • Securing Methods with pointcuts
  • Securing Methods with annotations

Concrete and simple code samples are used for the above list.

Now to follow, the most parts interesting in the book (for the reality), a case study, is for the two next chapters:

  • Chapter 6: Three-tier Spring Application, Domain-Driven Design
  • Chapter 7: Three-tier Spring Application, Tests and AOP

Chapter 6
is really a long chapter because the Java code has has already been shown to us AOP not included here for the moment. There theory of DDD is introduced:

  • Roles and responsibilities
    • Entities
    • Aggregates
    • Modules
    • Value Objects
    • Factories
    • Repositories
    • Services
  • Architecture
    • User Interface
    • Application Layer
    • Domain Layer
    • Infrastructure Layer

With all this theory explained, a sample application is used, starting with a simple "Package Explorer" of the project in Eclipse. The following sections are available

  • Design where interfaces and other more (hierarchy applied) are introduced, therefore many classes that implements them they use @Configurable and each of one of them has its inner public Builder class
  • Services where an interface with its implementations are used(Builder classes are called from there)
  • Factories again an interface with its implementations are used (Repositories classes are called from there)
  • Repositories many interfaces with its implementations are used (here db access is performed), Ibatis is used here therefore xml configuration maps is shown

The code is not complex, but something that lacks this chapter is at least an UML class diagram, It could be better include it to get a better understanding of all structure of the project

Now is the turn for Chapter 7, here the chapter starts with Spring MVC working with annotations (Even a HandlerInterceptorAdapter is included ), Testing (JUnit 4.5 with annotations and DbUnit). Load Time Weaver is applied here, and the book teaches us again about the jvm parameter quot;javaagent" working inside Eclipse

Now AOP is used. Both XML and Annotation approaches are applied. The aspects managed here are the most principal, Caching, Concurrency, Time Executing, Transactions (<tx:advice is used here), Security.

The last chapter 8: Develop with AOP Tool

This simple chapter teaches us how download and configure in a fast way many for the tools used throughout book for Linux MacOSX and Windows.

  • Java Development Kit
  • Spring
  • Eclipse
  • Spring IDE
  • AJDT
  • Apache Tomcat
  • PostgreSQL


AOP itself is complex for the first time that a beginner starts to learn it. This book avoids this problem by keeping the things simple and not overloading with heavy theory. That means the theory and practice are not poor (superficial) or too detailed.

Developers with some experience with AOP, would find this book very good for reference, as it has a long good amount the samples about configuration for XML and annotations. For its 300 pages I consider this book a good material for learning concepts and good practices.

I hope you enjoy this book like I did.


More Packt Publishing Books Reviews …