Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

About this book

About this book

AspectJ in Action, Second Edition is a comprehensive and practical guide to applying AOP and AspectJ in real-world enterprise applications. Although the book focuses on practical applications, it doesn't skimp on concepts in AOP and constructs in AspectJ. I cover a broad spectrum of solutions—from simple examples that address tracing, monitoring, and policy enforcement to complex ones dealing with caching, concurrency control, transactions, and security. The book also covers how AOP helps improve domain-logic implementation. To make this book immediately useful and provide a practical context of enterprise applications, I use:

  • Java (1.6)

  • Spring MVC (3.0)

  • Log4J (1.2)

  • OSCache (2.4)

  • JUnit (4.6)

  • Eclipse (3.5)

  • AspectJ (1.6)

  • Spring Security (3.0)

  • JPA (1.0)

  • EJB (3.0)

  • Ant (1.7)

  • AJDT (2.0)

  • Spring (3.0)

  • Spring Batch (2.0)

  • Hibernate (3.3)

  • Mockito (1.8)

  • Maven (2.2)

  • SpringSource Tools Suite (2.1)

Regardless of your area of expertise, you'll find examples that you can use in your work immediately.


This book is divided into two parts plus three appendices. Part 1 (chapter 1 through 9) focuses on the technology, although we introduce many practical aspects that you may use in your application. Part 2 (chapter 10 through 17) focuses on applications of AOP. If you're new to AOP and AspectJ, you should read part 1 before reading part 2. If you're familiar with an older version of AspectJ, you may want to read part 1, focusing only on new features such as support for annotations, the @AspectJ syntax, new weaving models, and Spring integration.

Chapter 1 makes a case for AOP. It introduces the problems that aspect-oriented programming aims to address, discusses how current techniques fall short, and explains how AOP handles them. It presents the core concepts of AOP such as the join point model, pointcuts, and advice. It also presents a generic model of AOP to help distinguish it from similar technologies.

Chapter 2 introduces AspectJ at a high level. We'll discuss various language concepts and constructs, weaving choices, and Spring integration. We'll finish the chapter by showing the IDE support for AspectJ.

Chapter 3 gets into the details of the AspectJ language by closely examining its join point model. It introduces the pointcut expression language along with many examples. This chapter should serve as a handy reference for you.

Chapter 4 focuses on dynamic crosscutting that lets you modify the behavior of the system. It introduces various kinds of advice supported by AspectJ. In this chapter, you'll begin to implement aspects to deal with fault tolerance and caching functionality. This chapter provides you with enough information to start writing simple AspectJ programs.

Chapter 5 focuses on static crosscutting that you can use to modify the structure of the system. It examines mechanisms such as inter-type declarations, weave-time errors and warning declarations, and exception softening. It also shows a few examples of AspectJ that you can begin using immediately.

Chapter 6 discusses the unit of modularization in AspectJ: the aspect. We'll put together all the concepts presented so far. Equipped with this new knowledge, we'll update the caching example introduced in chapter 4. Specifically, we'll add automated tests to verify its functionality and expose the aspect over JMX.

Chapter 7 presents the brand-new feature in AspectJ 5: the @AspectJ syntax. In this chapter, we'll map the traditional syntax to the @AspectJ syntax, which is useful with both byte-code-based weaving and Spring's proxy-based implementation. We'll complete this chapter by discussing how you choose between the traditional and the @Aspect syntax.

Chapter 8 discusses weaving models offered by AspectJ, some of which are new features of AspectJ 5. We'll go into detail about build-time and load-time weaving. We'll also show an example that adds monitoring to a web application using load-time weaving.

Chapter 9 focuses on Spring AspectJ integration. We'll discuss using the @AspectJ syntax while avoiding the use of the byte-code-based weaver. We'll dive into how Spring's dependency injection mechanism works in synergy with proxy-based AOP. We'll also discuss a Spring-based variation of load-time weaving along with a complete example.

Next, we'll dive into part 2 to apply the knowledge you gain in part 1 to practical applications of AOP.

Chapter 10 discusses many variations of tracing and monitoring. These aspects represent how most developers begin with AOP. Make no mistake: these aren't toy aspects, and by the end of this chapter you should be able use aspects in your own project and reap immediate benefits.

Chapter 11 continues the journey of exploring another set of aspects that developers use during their initial exploration of AOP: policy enforcement. In this chapter, we'll discuss how policy-enforcement aspects can help keep your design intact. We'll include a wide range of examples: layered architecture, mixing of JDBC with JPA, Swing, and EJB.

Chapter 12 presents a set of design patterns that have been found to be useful in practice and that we'll use in the chapters that follow. We'll present the worker object pattern, the wormhole pattern, the participant pattern, and its variation—the annotation-driven participant pattern.

Chapter 13 applies some of the patterns presented in the previous chapter to a complex crosscutting concern of concurrency control. We'll implement aspects that modularize concurrency control for Swing-based applications. We'll also implement a read-write lock aspect.

Chapter 14 discusses one of the most commonly required crosscutting concern in enterprise applications: transaction management. We'll implement several aspects based on the abstraction offered by Spring. We'll also develop a fault-tolerance scheme that works particularly well with transaction managed operations.

Chapter 15 explains the modularization of another crosscutting concern: security. We'll implement aspects based on abstraction offered by Spring Security.

Chapter 16 introduces how AOP can help you better implement domain logic. We'll discuss how AspectJ can extend dependency injection concepts to domain objects and how to use this possibility of implementing rich behavior in domain objects. We'll also examine improving business logic through aspects, refactoring using aspects, and implementing certain domain-driven design policies using aspects.

Chapter 17 rounds out the book by showing a pragmatic approach to adopting AOP.

The first of the three appendixes presents an e-commerce example that is used in the book. The remaining appendixes explain how to use AspectJ with Ant and Maven.

Who should read this book

AspectJ in Action, Second Edition is aimed at intermediate to advanced Java developers. Whether you develop enterprise applications or architect complex software systems, you'll find this book enjoyable and helpful. If you're developing applications using the Spring Framework, you'll find most of the code developed in this book of immediate value. But even if you aren't using Spring, you'll be able to modify the code to any other framework you may be using. The book focuses heavily on Spring-based applications, but it also includes examples of other technologies such as Swing and EJB.

Although knowledge of object-oriented programming and Java is required, I don't assume that you're familiar with aspect-oriented programming or AspectJ. Basic knowledge of the Spring Framework will help you get the most out of this book. If you aren't familiar with the Spring Framework, you may want to read a few of the many online resources to gain some familiarity. For some of the specific technologies, such as transaction management in Spring and Spring Security, the book presents a short introduction so that you can understand the aspects without needing another source. I also cite resources (both text and online) for those who want to gain in-depth understanding.

Code and typographical conventions

To keep the code short, we don't show most import statements in Java and AspectJ source code, except in chapter 2 and where import statements improve clarity of the code. Similarly, we don't show the namespace and schema declarations in most XML source code. The downloadable code, of course, has unabbreviated code.

The book follows the following typographical conventions:

  • Italic typeface is used to introduce new terms.

  • Courier typeface is used to denote code samples as well as program elements.

  • Courier bold typeface is used to denote code of special interest.

  • Code-line continuations are indicated by .

Getting the source code

The source code for the example applications in this book is freely available from Manning's web site, or Much of the source code is reusable either in its original state or after some customization. The downloadable package contains the source code, instructions on how to set up the environment, and build scripts that automate compiling and running the programs. The source code is organized to promote experimentation, where you can modify code for any of the projects without affecting others.

All of the technologies used in this book are being steadily updated. I expect the code to work without any changes with Java 5 and Java 6, Spring 3.0, and AspectJ 1.6.5. But I'll update source code with significant releases of the technologies used—especially if those break the code.

Author online

The purchase of AspectJ in Action, Second Edition includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to or This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning's commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher's web site as long as the book is in print.

About the author

Ramnivas Laddad is a well-known expert in enterprise Java, especially in the area of AOP and Spring. He is the author of the first edition of AspectJ in Action, the best-selling book on AOP and AspectJ that has been lauded by industry experts for its presentation of practical and innovative AOP applications to solve real-world problems. Ramnivas, a Spring Framework committer, is also a very active presenter at leading industry events and has been an active member of both the AspectJ and Spring communities from their beginnings. He has worked with a wide range of systems, especially dealing with complex and mission-critical applications, in various roles.

Ramnivas shares his thoughts on, and you can follow him on Twitter at

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint