AspectJ in Action
Practical Aspect-Oriented Programming
Ramnivas Laddad
  • July 2003
  • ISBN 9781930110939
  • 512 pages
This title is out of print and no longer for sale.

Speaks directly to me as a developer.

Alan Cameron Wills, fastnloose


AspectJ in Action, Second Edition is now available. An eBook of this older edition is included at no additional cost when you buy the revised edition!

A software system is the realization of a set of concerns. One of the central premises of object-oriented programming is that each concern should be implemented as a separate module. However, there are certain system-wide concerns, such as logging, security, performance, and so forth, that often need to be addressed in many, if not all of the modules. Consequently, the code to handle these system-wide concerns may be mixed in with the core logic of a huge number of modules, resulting in lower productivity, poor quality, and systems that are hard to evolve.

Aspect-oriented programming overcomes these problems by modularizing the system-wide concerns.

AspectJ enables AOP programming in Java by adding a few new language constructs. By using Java as the base language and creating a final system that is compatible with Java byte code specification, AspectJ passes on all the benefits of Java. The use of Java as the base language also makes AspectJ a relatively easy language to learn.

AspectJ in Action is a practical guide to AOP and AspectJ. The reusable code examples that are provided will enable quick implementation of functionality in your system.

Table of Contents detailed table of contents


how real is AspectJ?

into the future!


about this book

Part 1 Understanding AOP and AspectJ

1. Introduction to AOP

1.1. The architect’s dilemma

1.2. Evolution of programming methodologies

1.3. Managing system concerns

1.3.1. Identifying system concerns

1.3.2. A one-dimensional solution

1.3.3. It’s all about modularizing

1.4. Implementing crosscutting concerns in nonmodularized systems

1.4.1. Symptoms of nonmodularization

1.4.2. Implications of nonmodularization

1.4.3. Introducing AOP

1.4.4. A bit of history

1.4.5. The AOP methodology

1.5. Anatomy of an AOP language

1.5.1. The AOP language specification

1.5.2. The AOP language implementation

1.5.3. A weaving example

1.6. Benefits of AOP

1.7. Myths and realities of AOP

1.8. Summary

2. Introducing AspectJ

2.1. AspectJ: a bird’s eye view

2.1.1. Crosscutting in AspectJ

2.1.2. Crosscutting elements

2.2. AspectJ Hello World

2.3. AspectJ: under the hood

2.4. The join point model

2.4.1. Exposed join point categories

2.4.2. Join point demonstration example

2.5. Aspects

2.6. AspectJ logistics overview

2.6.1. The AspectJ compiler

2.6.2. AspectJ browser

2.6.3. IDE integration

2.7. Summary

3. AspectJ: syntax basics

3.1. Pointcuts

3.1.1. Wildcards and pointcut operators

3.1.2. Signature syntax

3.1.3. Implementing pointcuts

3.2. Advice

3.2.1. Anatomy of advice

3.2.2. The before advice

3.2.3. The after advice

3.2.4. The around advice

3.2.5. Comparing advice with methods

3.2.6. Passing context from a join point to advice

3.2.7. Returning a value from around advice

3.2.8. An example using around advice: failure handling

3.2.9. Context collection example: caching

3.3. Static crosscutting

3.3.1. Member introduction

3.3.2. Modifying the class hierarchy

3.3.3. Introducing compile-time errors and warning

3.4. Tips and tricks

3.5. Summary

4. Advanced AspectJ

4.1. Accessing join point information via reflection

4.1.1. The reflective API

4.1.2. Using reflective APIs

4.2. Aspect precedence

4.2.1. Ordering of advice

4.2.2. Explicit aspect precedence

4.2.3. Aspect inheritance and precedence

4.2.4. Ordering of advice in a single aspect

4.2.5. Aspect precedence and member introduction

4.3. Aspect association

4.3.1. Default association

4.3.2. Per-object association

4.3.3. Per-control-flow association

4.3.4. Implicit limiting of join points

4.3.5. Comparing object association with member introduction

4.3.6. Accessing aspect instances

4.4. Exception softening

4.5. Privileged aspects

4.6. Summary

Part 2 Basic applications of AspectJ

5. Monitoring techniques: logging, tracing, and profiling

5.1. Why use AspectJ for logging?

5.1.1. A simple case in point

5.1.2. Logging the conventional way

5.1.3. Logging the aspect-oriented way

5.2. What’s wrong with conventional logging

5.3. The beauty of AspectJ-based logging

5.4. Developing logging and tracing aspects

5.4.1. Method call tracing

5.4.2. Exceptions logging

5.5. Common logging idioms

5.5.1. Logging the method parameters

5.5.2. Indenting the log statements

5.5.3. Aspect precedence

5.5.4. Changing the underlying logging mechanism

5.5.5. Using logging in a multithreaded environment

5.6. Extending logging for other usage

5.6.1. Testing

5.6.2. Profiling

5.7. Summary

6. Policy enforcement: system wide contracts

6.1. AspectJ-based policy enforcement overview

6.2. The current solution and its challenges

6.3. Enforcement using AspectJ

6.3.1. Policy enforcement implementation choices

6.3.2. The role of policy enforcement during the product lifecycle

6.4. Policy enforcement patterns

6.4.1. Detecting the violation of a specific call pattern

6.4.2. Implementing flexible access control

6.4.3. Enforcing the best-practices principles

6.5. Example: implementing EJB programming restrictions

6.5.1. Implementing "no AWT"

6.5.2. Implementing "no nonfinal static field access"

6.6. Example: implementing Swing policies

6.6.1. Understanding the problem

6.6.2. Detecting the violation

6.7. Summary

7. Optimization: pooling and caching

7.1. The typical case

7.1.1. Return, reuse, recycle: The role of resource pooling

7.1.2. Resource pooling issues

7.2. Diving into the pool using AspectJ

7.2.1. Designing a template aspect

7.2.2. Implementing the template aspect

7.3. Example 1: database connection pooling

7.3.1. Understanding the database connection pool interface

7.3.2. AspectJ-based database connection pooling

7.3.3. Implementing the connection pool

7.3.4. Testing our solution

7.3.5. Tweaking the solution

7.4. Example 2: thread pooling

7.4.1. The echo server

7.4.2. Understanding the thread pool interface

7.4.3. AspectJ-based thread pooling

7.4.4. Implementing the thread pool

7.4.5. Testing our solution

7.4.6. Tweaking the solution

7.5. Extending pooling concepts to caching

7.5.1. AspectJ-based caching: the first version

7.5.2. AspectJ-based caching: the second version

7.5.3. Ideas for further improvements

7.6. Summary

Part 3 Advanced applications of AspectJ

8. Design patterns and idioms

8.1. The worker object creation pattern

8.1.1. The current solution

8.1.2. An overview of the worker object creation pattern

8.1.3. The pattern template

8.1.4. A summary of the worker object creation pattern

8.2. The wormhole pattern

8.2.1. The current solution

8.2.2. An overview of the wormhole pattern

8.2.3. The pattern template

8.2.4. A summary of the wormhole pattern

8.3. The exception introduction pattern

8.3.1. The current solution

8.3.2. An overview of the exception introduction pattern

8.3.3. The pattern template

8.3.4. A summary of the exception introduction pattern

8.4. The participant pattern

8.4.1. Current solutions

8.4.2. An overview of the participant pattern

8.4.3. The pattern template

8.4.4. A summary of the participant pattern

8.5. Idioms

8.5.1. Avoiding infinite recursion

8.5.2. Nullifying advice

8.5.3. Providing empty pointcut definitions

8.5.4. Providing a default interface implementation

8.6. Summary

9. Implementing thread safety

9.1. Swing’s single-thread rule

9.1.1. The rule

9.1.2. The problem

9.1.3. The solution

9.2. A test problem

9.3. Solution: the conventional way

9.4. Solution: the AspectJ way

9.4.1. The first version

9.4.2. The second version

9.4.3. The third version

9.5. Improving the solution

9.5.1. Dealing with exceptions

9.5.2. Avoiding the overhead

9.6. Improving the responsiveness of UI applications

9.7. Modularizing the read-write lock pattern

9.7.1. Implementation: the conventional way

9.7.2. Implementation: the AspectJ way

9.8. Summary

10. Authentication and authorization

10.1. Problem overview

10.2. A simple banking example

10.3. Authentication: the conventional way

10.3.1. Implementing the solution

10.3.2. Testing the solution

10.4. Authentication: the AspectJ way

10.4.1. Developing the solution

10.4.2. Testing the solution

10.5. Authorization: the conventional way

10.5.1. Understanding JAAS-based authorization

10.5.2. Developing the solution

10.5.3. Testing the solution

10.5.4. Issues with the conventional solution

10.6. Authorization: the AspectJ way

10.6.1. Developing the solution

10.6.2. Testing the solution

10.7. Fine-tuning the solution

10.7.1. Using multiple subaspects

10.7.2. Separating authentication and authorization

10.8. Summary

11. Transaction management

11.1. Example: a banking system with persistence

11.1.1. Implementing the core concern

11.1.2. Setting up the test scenario

11.2. The conventional solution

11.2.1. Using the same connection object

11.2.2. Committing at the top level only

11.3. Developing a simple AspectJ-based solution

11.3.1. Implementing the JDBC transaction aspect

11.3.2. Handling legacy system issues

11.3.3. Enabling transaction management for the banking system

11.3.4. Testing the solution

11.4. Improving the solution

11.4.1. Using the participant pattern

11.4.2. Implementing the JDBC transaction aspect: the second version

11.4.3. Testing the solution

11.5. Using AspectJ with advanced transaction-management systems

11.6. Summary

12. Implementing business rules

12.1. Using business rules in enterprise applications

12.2. An overview of business rule implementation

12.3. Current mechanisms

12.4. Introducing a solution using AspectJ

12.4.1. The template

12.5. Example: the banking system

12.5.1. Implementing the core business logic

12.5.2. Implementing the first business rule

12.5.3. Implementing the second business rule

12.5.4. Writing a test program

12.6. Implementing business rules with a rule engine

12.6.1. An overview of the rule engine

12.6.2. Using a rule engine

12.6.3. Modularizing with AspectJ

12.7. Example: a banking system with a rule engine

12.7.1. A brief overview of Jess (Java Expert System Shell)

12.7.2. Specifying rules

12.7.3. Understanding the rule invocation aspect

12.8. Summary

13. The next step

13.1. Applying AspectJ to new problems

13.1.1. Talking the talk

13.1.2. Walking the walk

13.2. Employing AspectJ in development phases

13.2.1. AspectJ in the design phase

13.2.2. AspectJ in the implementation phase

13.2.3. AspectJ in the testing phase

13.2.4. AspectJ in the maintenance phase

13.2.5. AspectJ in legacy projects

13.3. A word of warning

13.4. Evangelizing AspectJ

13.5. Parting thoughts

Appendix A: The AspectJ compiler

A.1. Downloading and setting up

A.2. A.2 An overview of the compiler

A.3. Compiling source files

A.4. Compiling source directories

A.5. Weaving into JAR files

A.6. Creating aspect libraries

A.7. Using aspect libraries

A.8. Utilizing incremental compilation mode

A.9. Producing useful warnings

Appendix B: Understanding Ant integration

B.1. Compiling source files using an Ant task

B.2. Weaving into JAR files using an Ant task

B.3. Creating aspect libraries using an Ant task

B.4. Utilizing aspect libraries using an Ant task

B.5. Utilizing incremental compilation using an Ant task



About the book

The book is divided into three parts. The first part introduces AOP and AspectJ and will be helpful to developers wanting to learn or advance their knowledge of AspectJ. The second and third parts present examples of everyday situations in which you can use simple and easy AspectJ solutions to implement common system requirements such as logging, policy enforcement, resource pooling, business rules, thread-safety, authentication and authorization, as well as transaction management.

What's inside

  • In-depth coverage of AOP and AspectJ
  • Design patterns and idioms
  • Solutions to real-world problems
  • Much reusable code content
  • AspectJ version 1.1

About the author

Ramnivas Laddad is a Sun Certified Architect of Java Technology. He has worked with object-oriented systems for many years and with aspect-oriented programming for the past three years. He is the author of several articles and papers and co-author of Professional Java XML. His article series on AOP and AspectJ was published in "JavaWorld." He lives in Sunnyvale, California.

... real solutions to tough problems ...

Chris Bartling, Identix, Inc.

I started reading in 11 pm and couldn't stop ... It's a must read for anyone interested in the future of programming.

Arno Schmidmeier, AspectSoft

... The only resource that presents AOP concepts and real-world examples in an approachable, readable way.

Jean Baltus, Metafro-Infosys