Front cover
brief contents
contents
foreword
preface
preface to the first edition
acknowledgments
about this book
Roadmap
Who should read this book
Code and typographical conventions
Getting the source code
Author online
About the author
about the title
about the cover illustration
a real-world perspective of AOP
Mapping AOP onto the hype cycle
Technology trigger
Peak of inflated expectations
Trough of disillusionment
Slope of enlightenment
Plateau of productivity
Where is AOP being used?
Enterprise applications
Web and application servers
Application frameworks
Monitoring tools
Compiler and IDE integration
Understanding AOP and AspectJ
Discovering AOP
1.1 Life without AOP
1.1.1 Code tangling
1.1.2 Code scattering
1.2 Modularizing with AOP
1.3 Anatomy of an AOP language
1.3.1 The AOP language specification
1.3.2 The AOP language implementation
1.4 Fundamental concepts in AOP
1.5 AOP by analogy
1.5.1 Cascading Style Sheets (CSS)
1.5.2 Database systems
1.5.3 Event-oriented programming
1.6 Implementations of AOP
1.6.1 AspectJ
1.6.2 Spring AOP
1.6.3 Other implementations of AOP
1.7 Alternatives to AOP
1.7.1 Frameworks
1.7.2 Code generation
1.7.3 Design patterns
1.7.4 Dynamic languages
1.8 Costs and benefits of AOP
1.8.1 Costs of AOP
1.8.2 Benefits of AOP
1.9 Summary
Introducing AspectJ
2.1 Writing your first AspectJ program
2.1.1 Setting up the example
2.1.2 Adding an aspect
2.2 AspectJ crosscutting construct
2.2.1 Common crosscutting constructs
2.2.2 Dynamic crosscutting construct: advice
2.2.3 Static crosscutting constructs
2.3 AspectJ alternative syntax
2.4 Weaving mechanisms
2.4.1 Source weaving
2.4.2 Binary weaving
2.4.3 Load-time weaving
2.5 AspectJ weaving: under the hood
2.5.1 The compiled aspect
2.5.2 The woven class
2.6 Spring AspectJ integration
2.7 AspectJ logistics overview
2.7.1 IDE integration
2.7.2 AspectJ documentation tool
2.8 Summary
Understanding the join point model
3.1 Understanding the join point model
3.1.1 Join points
3.1.2 Pointcuts
3.2 Categorizing exposed join points
3.2.1 Method join points
3.2.2 Constructor join points
3.2.3 Field access join points
3.2.4 Exception-handler join points
3.2.5 Class-initialization join points
3.2.6 Object initialization join points
3.2.7 Object pre-initialization join points
3.2.8 Advice execution join points
3.3 Join point demonstration example
3.3.1 The aspect
3.3.2 The result
3.4 Understanding pointcut basics
3.4.1 Named and anonymous pointcuts
3.4.2 Pointcut operators
3.5 Signature syntax
3.5.1 Type signature patterns
3.5.2 Method and constructor signature patterns
3.5.3 Field signature patterns
3.6 Implementing pointcuts
3.6.1 Kinded pointcuts
3.6.2 Non-kinded pointcuts
3.7 Summary
Modifying behavior with dynamic crosscutting
4.1 Advice overview
4.1.1 Advice classification
4.1.2 Advice syntax
4.2 Comparing advice to methods
4.2.1 Similarities between advice and methods
4.2.2 Differences between advice and methods
4.3 Advice in depth
4.3.1 Before advice
4.3.2 After advice
4.3.3 Around advice
4.4 Collecting join point context
4.4.1 Collecting objects at the join point
4.4.2 Implementing simple caching
4.5 Accessing join point context via reflection
4.5.1 The reflection API
4.5.2 Improving caching using reflection APIs
4.5.3 Comparing the reflection API to pointcuts
4.6 Summary
Modifying structure with static crosscutting
5.1 Introducing members
5.1.1 Scattering and tangling
5.1.2 Untangling with aspects
5.1.3 Mixing with mixins
5.1.4 Member introduction rules
5.1.5 Idiom: Providing a default interface implementation
5.2 Modifying the type hierarchy
5.3 Introducing members to multiple types
5.4 Supplying annotations
5.5 Declaring weave-time errors and warnings
5.6 Softening checked exceptions
5.7 Summary
Aspects: putting it all together
6.1 Working with aspects
6.1.1 Similarities between aspects and classes
6.1.2 Differences between aspects and classes
6.2 Aspect association
6.2.1 Default singleton association
6.2.2 Per-object association
6.2.3 Per-control-flow association
6.2.4 Per-type association
6.2.5 Implicit limiting of join points with aspect associations
6.2.6 Accessing aspect instances
6.2.7 Comparing per-object association and member introduction
6.2.8 Improving the caching aspect
6.3 Aspect precedence
6.3.1 Ordering of advice
6.3.2 Explicit aspect precedence
6.3.3 Ordering advice in a single aspect
6.4 Privileged aspects
6.5 Summary
Diving into the @AspectJ syntax
7.1 Syntax overview
7.1.1 Natural mapping
7.1.2 Java compatibility
7.1.3 Early error detection
7.2 Mapping aspects
7.2.1 Specifying aspect association
7.2.2 Accessing the aspect instance
7.2.3 Declaring aspect precedence
7.3 Mapping pointcuts
7.3.1 Mapping abstract pointcuts
7.3.2 Mapping concrete pointcuts
7.4 Mapping dynamic crosscutting constructs
7.4.1 The before advice
7.4.2 The after advice
7.4.3 The around advice
7.5 Mapping static crosscutting
7.5.1 Mapping weave-time declarations
7.5.2 Mapping declare parents
7.6 Features not implemented in @AspectJ
7.6.1 Associating annotations
7.6.2 Introducing data and methods
7.6.3 Softening exceptions
7.6.4 Privileged aspects
7.7 Comparing syntax styles
7.8 Summary
AspectJ weaving models
8.1 Classifying weaving models
8.2 Build-time weaving
8.2.1 Build-time source code weaving
8.2.2 Build-time binary weaving
8.3 Load-time weaving
8.3.1 Load-time weaving overview
8.3.2 Configuring the load-time weaver
8.4 Load-time weaver in action
8.5 Choosing syntax and weaving
8.6 Summary
Integration with Spring
9.1 Spring AOP fundamentals
9.1.1 Setting up the application
9.1.2 Utilizing the @AspectJ syntax
9.1.3 Spring IDE
9.2 Spring AOP under the hood
9.2.1 A quick introduction to dynamic proxies
9.2.2 Proxy-based AOP with Spring
9.2.3 Spring AOP internals
9.2.4 Proxy-based AOP in DI framework
9.2.5 Limitations of Spring AOP
9.3 @AspectJ support in depth
9.3.1 Dynamic crosscutting
9.3.2 Static crosscutting
9.4 Schema-style AOP support
9.4.1 Mapping aspects
9.4.2 Mapping pointcuts
9.4.3 Mapping advice
9.4.4 Mapping static crosscutting
9.5 Tapping into the full power of AspectJ
9.5.1 Configuring aspects using Spring DI
9.5.2 Spring-driven LTW
9.6 Choosing an appropriate AOP system
9.6.1 Comparing AspectJ to Spring AOP
9.6.2 Comparing Spring AOP syntax
9.6.3 It’s decision time
9.7 Summary
Applications of AspectJ with Spring
Monitoring techniques
10.1 Tracing in action
10.1.1 Tracing the aspect-oriented way
10.1.2 Tracing the conventional way
10.2 Conventional vs. AOP tracing
10.3 Selecting join points of interest
10.3.1 Selection based on static structure
10.3.2 Selection based on dynamic context
10.4 Tracing
10.4.1 Indenting trace calls
10.4.2 Sharing tracing aspect functionality
10.4.3 Tracing intra-method activities
10.4.4 Logging the method parameters
10.4.5 Choosing a type-specific logger
10.5 A detour: deployment options for monitoring aspects
10.5.1 Utilizing load-time weaving
10.5.2 Utilizing Spring AOP for tracing
10.6 Exception monitoring
10.7 Improving conventional logging
10.7.1 Modularizing NDC with conventional logging
10.7.2 Modularizing MDC with conventional logging
10.8 Performance monitoring
10.9 Runtime control of monitoring aspects
10.10 Summary
Policy enforcement: keeping your design intact
11.1 AOP-based policy enforcement overview
11.2 Policy origins and destinations
11.3 Enforcement using AOP
11.3.1 Compile-time enforcement
11.3.2 Runtime enforcement
11.4 Comparison with code-analysis tools
11.5 Implementing flexible access control
11.5.1 Restricting exposure
11.5.2 Limiting collaboration
11.5.3 Enforcing the factory pattern
11.6 Enforcement idiom: return-value restriction
11.7 Enforcing JPA-JDBC integration policies
11.8 Implementing EJB policies
11.8.1 Developing a core EJB enforcement aspect
11.8.2 Defining EJB pointcuts
11.8.3 Dealing with XML-based EJBs
11.8.4 Implementing a “no EJBs” policy
11.9 Detecting Swing concurrency control policy violations
11.9.1 Understanding the problem
11.9.2 Detecting the violation
11.10 Summary
Learning design patterns
12.1 The worker object pattern
12.1.1 The current solution
12.1.2 An overview of the worker object pattern
12.1.3 The worker object pattern template
12.1.4 Getting the return value
12.1.5 A summary of the worker object pattern
12.2 The wormhole pattern
12.2.1 The current solution
12.2.2 An overview of the wormhole pattern
12.2.3 The wormhole pattern template
12.2.4 A summary of the wormhole pattern
12.3 The participant pattern
12.3.1 Current solutions
12.3.2 An overview of the participant pattern
12.3.3 The participant pattern template
12.3.4 Consequences of the participant pattern
12.3.5 A summary of the participant pattern
12.4 Annotation-driven participant pattern
12.4.1 Current solution
12.4.2 The annotation-driven participant pattern template
12.4.3 Annotation type abstraction
12.4.4 Variation: bridged participation pattern
12.4.5 Role of ADPP in library aspects
12.5 Summary
Implementing concurrency control
13.1 Modularizing Swing’s single-thread rule
13.1.1 A test problem
13.1.2 Solution: the conventional way
13.1.3 Solution: the AspectJ way
13.1.4 Improving the solution
13.2 Improving the responsiveness of UI applications
13.3 Modularizing the read-write lock pattern
13.3.1 Implementation: the conventional way
13.3.2 Implementation: the AspectJ way
13.4 Summary
Managing transactions
14.1 Transaction management implementations
14.1.1 Conventional implementation
14.1.2 AOP implementation
14.2 Transaction-management players
14.2.1 Transaction management in architecture
14.2.2 Transaction management: one concept, too many implementations
14.2.3 Transaction-management abstraction
14.3 Aspect implementation choices
14.3.1 Proxy-based AOP
14.3.2 Byte-code weaving
14.4 Transaction-management aspect (almost) from scratch
14.4.1 Implementing the aspect
14.4.2 Testing the aspect
14.4.3 Road test for the aspect
14.5 Spring’s transaction management
14.5.1 XML-driven transaction management
14.5.2 Annotation-driven transaction management
14.5.3 AspectJ weaver-based transaction management
14.6 Implementing fault tolerance for transactional operations
14.6.1 Base aspect
14.6.2 Using the annotation-driven participant pattern
14.6.3 Configuring the aspect
14.6.4 Testing the fault-tolerance aspect
14.6.5 Improving the solution
14.7 Summary
Securing applications
15.1 Securing applications with conventional techniques
15.1.1 The do-it-yourself approach
15.1.2 The framework-based approach
15.2 Modularizing security using AOP
15.3 A quick overview of Spring Security
15.3.1 Authentication
15.3.2 Authorization
15.4 Implementing a security solution from scratch
15.4.1 Implementing authentication aspects
15.4.2 Implementing authorization aspects
15.5 Implementing field-level authorization
15.6 Spring Security prebuilt solutions
15.6.1 Web security
15.6.2 Service level security
15.6.3 Domain-object security
15.7 Additional ideas in implementing security
15.7.1 Auditing access
15.7.2 Filtering field content
15.8 Summary
Improving domain logic
16.1 Empowering objects
16.1.1 Enabling rich behavior
16.1.2 Injecting dependencies with Spring and AspectJ
16.1.3 Possibilities enabled by domain-object DI
16.2 Implementing business logic
16.2.1 Improving inventory management
16.2.2 Checking for the order-level constraint
16.2.3 Refactoring using aspects
16.3 Managing access to objects
16.3.1 Applying specific policies
16.3.2 Applying general policies
16.4 Summary
Taking the next step
17.1 The adoption path: technologies
17.1.1 Spring AOP
17.1.2 AspectJ
17.2 The adoption path: applications
17.2.1 Development aspects
17.2.2 Production aspects
17.3 Applying AOP to new problems
17.3.1 Talking the talk
17.3.2 Walking the walk
17.4 Employing AOP in development phases
17.4.1 AOP in the design phase
17.4.2 AOP in the implementation phase
17.4.3 AOP in the maintenance phase
17.4.4 AOP in legacy projects
17.5 Parting thoughts
appendix A: Setting up the example
Domain classes
Repository layer
Service layer
Application configuration
The web layer
Logging configuration
Building and running the application
appendix B: Using Ant with AspectJ
Weaving sources
Creating an aspect library
Weaving into jar files
appendix C: Using Maven with AspectJ
Weaving sources
Creating an aspect library
Weaving into jar files
index
Symbols
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Back cover