AspectJ in Action: Practical Aspect-Oriented Programming
暫譯: 《AspectJ 實戰:實用的面向切面程式設計》

Ramnivas Laddad

  • 出版商: Manning
  • 出版日期: 2003-07-01
  • 定價: $1,480
  • 售價: 6.0$888
  • 語言: 英文
  • 頁數: 512
  • 裝訂: Paperback
  • ISBN: 1930110936
  • ISBN-13: 9781930110939
  • 相關分類: Java 程式語言
  • 立即出貨(限量) (庫存=1)

買這商品的人也買了...

相關主題

商品描述

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.

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

Table of Contents

preface xvii
how real is AspectJ? xix
into the future! xxi
acknowledgments xxiii
about this book xxv



Part 1  Understanding AOP and AspectJ   1

1  Introduction to AOP   3
1.1 The architect’s dilemma 5
1.2 Evolution of programming methodologies 6
1.3 Managing system concerns 7
Identifying system concerns 8
A one-dimensional solution 10
It’s all about modularizing 11
1.4 Implementing crosscutting concerns in nonmodularized systems 14
Symptoms of nonmodularization 15
Implications of nonmodularization 18
Introducing AOP 19
A bit of history 20
The AOP methodology 21
1.5 Anatomy of an AOP language 22
The AOP language specification 23
The AOP language implementation 24
A weaving example 26
1.6 Benefits of AOP 27
1.7 Myths and realities of AOP 29
1.8 Summary 30
2  Introducing AspectJ   32
2.1 AspectJ: a bird’s eye view 33
Crosscutting in AspectJ 33
Crosscutting elements 34
2.2 AspectJ Hello World 37
2.3 AspectJ: under the hood 40
2.4 The join point model 43
Exposed join point categories 44
Join point demonstration example 50
2.5 Aspects 55
2.6 AspectJ logistics overview 59
The AspectJ compiler 59
AspectJ browser 60
IDE integration 61
2.7 Summary 62
3  AspectJ: syntax basics   64
3.1 Pointcuts 65
Wildcards and pointcut operators 67
Signature syntax 68
Implementing pointcuts 73
3.2 Advice 81
Anatomy of advice 82
The before advice 83
The after advice 83
The around advice 85
Comparing advice with methods 86
Passing context from a join point to advice 87
Returning a value from around advice 89
An example using around advice: failure handling 90
Context collection example: caching 92
3.3 Static crosscutting 95
Member introduction 95
Modifying the class hierarchy 96
Introducing compile-time errors and warning 97
3.4 Tips and tricks 98
3.5 Summary 99
4  Advanced AspectJ   100
4.1 Accessing join point information via reflection 101
The reflective API 103
Using reflective APIs 106
4.2 Aspect precedence 111
Ordering of advice 114
Explicit aspect precedence 115
Aspect inheritance and precedence 117
Ordering of advice in a single aspect 119
Aspect precedence and member introduction 120
4.3 Aspect association 122
Default association 123
Per-object association 125
Per-control-flow association 128
Implicit limiting of join points 132
Comparing object association with member introduction 134
Accessing aspect instances 135
4.4 Exception softening 136
4.5 Privileged aspects 139
4.6 Summary 141

Part 2  Basic applications of AspectJ   143

5  Monitoring techniques: logging, tracing, and profiling   145
5.1 Why use AspectJ for logging? 146
A simple case in point 147
Logging the conventional way 149
Logging the aspect-oriented way 153
5.2 What’s wrong with conventional logging 154
5.3 The beauty of AspectJ-based logging 156
5.4 Developing logging and tracing aspects 156
Method call tracing 157
Exceptions logging 163
5.5 Common logging idioms 167
Logging the method parameters 168
Indenting the log statements 170
Aspect precedence 172
Changing the underlying logging mechanism 173
Using logging in a multithreaded environment 173
5.6 Extending logging for other usage 174
Testing 174
Profiling 175
5.7 Summary 176
6  Policy enforcement: system wide contracts   178
6.1 AspectJ-based policy enforcement overview 179
6.2 The current solution and its challenges 181
6.3 Enforcement using AspectJ 182
Policy enforcement implementation choices 183
The role of policy enforcement during the product lifecycle 184
6.4 Policy enforcement patterns 185
Detecting the violation of a specific call pattern 185
Implementing flexible access control 187
Enforcing the best-practices principles 189
6.5 Example: implementing EJB programming restrictions 191
Implementing "no AWT" 193
Implementing "no nonfinal static field access" 194
6.6 Example: implementing Swing policies 195
Understanding the problem 196
Detecting the violation 198
6.7 Summary 200
7  Optimization: pooling and caching   202
7.1 The typical case 203
Return, reuse, recycle: The role of resource pooling 205
Resource pooling issues 206
7.2 Diving into the pool using AspectJ 208
Designing a template aspect 208
Implementing the template aspect 209
7.3 Example 1: database connection pooling 211
Understanding the database connection pool interface 212
AspectJ-based database connection pooling 213
Implementing the connection pool 216
Testing our solution 218
Tweaking the solution 222
7.4 Example 2: thread pooling 223
The echo server 224
Understanding the thread pool interface 226
AspectJ-based thread pooling 226
Implementing the thread pool 230
Testing our solution 231
Tweaking the solution 234
7.5 Extending pooling concepts to caching 235
AspectJ-based caching: the first version 237
AspectJ-based caching: the second version 239
Ideas for further improvements 240
7.6 Summary 241

Part 3  Advanced applications of AspectJ   243

8  Design patterns and idioms   245
8.1 The worker object creation pattern 247
The current solution 248
An overview of the worker object creation pattern 249
The pattern template 249
A summary of the worker object creation pattern 256
8.2 The wormhole pattern 256
The current solution 257
An overview of the wormhole pattern 257
The pattern template 258
A summary of the wormhole pattern 260
8.3 The exception introduction pattern 260
The current solution 261
An overview of the exception introduction pattern 265
The pattern template 265
A summary of the exception introduction pattern 269
8.4 The participant pattern 270
Current solutions 271
An overview of the participant pattern 273
The pattern template 274
A summary of the participant pattern 276
8.5 Idioms 277
Avoiding infinite recursion 277
Nullifying advice 279
Providing empty pointcut definitions 280
Providing a default interface implementation 281
8.6 Summary 285
9  Implementing thread safety   286
9.1 Swing’s single-thread rule 287
The rule 288
The problem 288
The solution 289
9.2 A test problem 290
9.3 Solution: the conventional way 293
9.4 Solution: the AspectJ way 297
The first version 298
The second version 303
The third version 307
9.5 Improving the solution 311
Dealing with exceptions 311
Avoiding the overhead 312
9.6 Improving the responsiveness of UI applications 313
9.7 Modularizing the read-write lock pattern 316
Implementation: the conventional way 316
Implementation: the AspectJ way 318
9.8 Summary 321
10  Authentication and authorization   323
10.1 Problem overview 324
10.2 A simple banking example 325
10.3 Authentication: the conventional way 329
Implementing the solution 329
Testing the solution 331
10.4 Authentication: the AspectJ way 333
Developing the solution 333
Testing the solution 336
10.5 Authorization: the conventional way 336
Understanding JAAS-based authorization 337
Developing the solution 338
Testing the solution 342
Issues with the conventional solution 345
10.6 Authorization: the AspectJ way 346
Developing the solution 346
Testing the solution 350
10.7 Fine-tuning the solution 353
Using multiple subaspects 353
Separating authentication and authorization 354
10.8 Summary 354
11  Transaction management   356
11.1 Example: a banking system with persistence 358
Implementing the core concern 358
Setting up the test scenario 362
11.2 The conventional solution 364
Using the same connection object 365
Committing at the top level only 367
11.3 Developing a simple AspectJ-based solution 368
Implementing the JDBC transaction aspect 368
Handling legacy system issues 373
Enabling transaction management for the banking system 374
Testing the solution 375
11.4 Improving the solution 378
Using the participant pattern 379
Implementing the JDBC transaction aspect: the second version 382
Testing the solution 385
11.5 Using AspectJ with advanced transaction-management systems 387
11.6 Summary 390
12  Implementing business rules   391
12.1 Using business rules in enterprise applications 392
12.2 An overview of business rule implementation 393
12.3 Current mechanisms 393
12.4 Introducing a solution using AspectJ 394
The template 394
12.5 Example: the banking system 396
Implementing the core business logic 396
Implementing the first business rule 401
Implementing the second business rule 403
Writing a test program 406
12.6 Implementing business rules with a rule engine 411
An overview of the rule engine 412
Using a rule engine 412
Modularizing with AspectJ 415
12.7 Example: a banking system with a rule engine 417
A brief overview of Jess (Java Expert System Shell) 417
Specifying rules 418
Understanding the rule invocation aspect 420
12.8 Summary 423
13  The next step   425
13.1 Applying AspectJ to new problems 426
Talking the talk 426
Walking the walk 427
13.2 Employing AspectJ in development phases 427
AspectJ in the design phase 428
AspectJ in the implementation phase 428
AspectJ in the testing phase 431
AspectJ in the maintenance phase 432
AspectJ in legacy projects 432
13.3 A word of warning 433
13.4 Evangelizing AspectJ 434
13.5 Parting thoughts 436
A  The AspectJ compiler   438
A.1 Downloading and setting up 439
A.2 An overview of the compiler 440
A.3 Compiling source files 441
A.4 Compiling source directories 441
A.5 Weaving into JAR files 442
A.6 Creating aspect libraries 443
A.7 Using aspect libraries 444
A.8 Utilizing incremental compilation mode 444
A.9 Producing useful warnings 446
B  Understanding Ant integration   447
B.1 Compiling source files using an Ant task 448
B.2 Weaving into JAR files using an Ant task 451
B.3 Creating aspect libraries using an Ant task 452
B.4 Utilizing aspect libraries using an Ant task 453
B.4 Utilizing incremental compilation using an Ant task 453

resources 455
index 461

商品描述(中文翻譯)

一個軟體系統是對一組關注點的實現。物件導向程式設計的一個核心前提是,每個關注點應該作為一個獨立的模組來實現。然而,某些系統範圍的關注點,例如日誌記錄、安全性、性能等,通常需要在許多模組中處理,甚至是所有模組。因此,處理這些系統範圍關注點的程式碼可能會與大量模組的核心邏輯混合在一起,導致生產力降低、品質不佳,以及系統難以演進。

面向切面程式設計(Aspect-oriented programming, AOP)通過模組化系統範圍的關注點來克服這些問題。

AspectJ 透過添加一些新的語言結構來實現 Java 中的 AOP 程式設計。使用 Java 作為基礎語言並創建一個與 Java byte code 規範相容的最終系統,AspectJ 繼承了 Java 的所有優點。使用 Java 作為基礎語言也使得 AspectJ 成為一種相對容易學習的語言。

《AspectJ in Action》是一本關於 AOP 和 AspectJ 的實用指南。提供的可重用程式碼範例將使您能夠快速實現系統中的功能。

本書分為三個部分。第一部分介紹 AOP 和 AspectJ,對於希望學習或提升 AspectJ 知識的開發人員將非常有幫助。第二和第三部分展示了日常情境的範例,您可以使用簡單易用的 AspectJ 解決方案來實現常見的系統需求,例如日誌記錄、政策執行、資源池化、業務規則、執行緒安全、身份驗證和授權,以及交易管理。

書中內容包括:
- 深入探討 AOP 和 AspectJ
- 設計模式和慣用法
- 解決現實世界問題的方案
- 大量可重用的程式碼內容
- AspectJ 版本 1.1

目錄
前言 xvii
AspectJ 的真實性 xix
展望未來! xxi
致謝 xxiii
關於本書 xxv

第一部分 理解 AOP 和 AspectJ 1
1 介紹 AOP 3
1.1 建築師的困境 5
1.2 程式設計方法論的演變 6
1.3 管理系統關注點 7
- 確定系統關注點 8
- 一維解決方案 10
- 一切都是關於模組化 11
1.4 在非模組化系統中實現橫切關注點 14
- 非模組化的症狀 15
- 非模組化的影響 18
- 介紹 AOP 19
- 一點歷史 20
- AOP 方法論 21
1.5 AOP 語言的解剖 22
- AOP 語言規範 23
- AOP 語言實現 24
- 一個編織範例 26
1.6 AOP 的好處 27
1.7 AOP 的神話與現實 29
1.8 總結 30
2 介紹 AspectJ 32
2.1 AspectJ:鳥瞰 33
- AspectJ 中的橫切 33
- 橫切元素 34
2.2 AspectJ Hello World 37
2.3 AspectJ:內部運作 40
2.4 連接點模型 43
- 曝露的連接點類別 44
- 連接點示範範例 50
2.5 切面 55
2.6 AspectJ 後勤概述 59
- AspectJ 編譯器 59
- AspectJ 瀏覽器 60
- IDE 整合 61
2.7 總結 62
3 AspectJ:語法基礎 64
3.1 切入點 65
- 通配符和切入點運算子 67
- 簽名語法 68
- 實現切入點 73
3.2 建議 81
- 建議的解剖 82
- 前置建議 83
- 後置建議 83
- 環繞建議 85
- 將建議與方法進行比較 86
- 從連接點傳遞上下文到建議 87
- 從環繞建議返回值 89
- 使用環繞建議的範例:失敗處理 90
- 上下文收集範例:快取 92
3.3 靜態橫切 95
- 成員引入 95
- 修改類別層次結構 96
- 引入編譯時錯誤和警告 97
3.4 提示與技巧 98
3.5 總結 99
4 進階 AspectJ 100
4.1 通過反射訪問連接點資訊 101
- 反射 API 103
- 使用反射 API 106
4.2 切面的優先順序 111
- 建議的排序 114
- 明確的切面優先順序 115
- 切面繼承與優先順序 117
- 單一切面中的建議排序 119
- 切面優先順序與成員引入 120
4.3 切面關聯 122
- 預設關聯 123
- 每個物件的關聯 125
- 每個控制流的關聯 128
- 隱式限制連接點 132
- 將物件關聯與成員引入進行比較 134
- 訪問切面實例 135
4.4 異常軟化 136
4.5 特權切面 139
4.6 總結 141

第二部分 AspectJ 的基本應用 143
5 監控技術:日誌記錄、追蹤和性能分析 145
5.1 為什麼使用 AspectJ 進行日誌記錄? 146
- 一個簡單的例子 147
- 傳統的日誌記錄方式 149
- 面向切面的日誌記錄方式 153
5.2 傳統日誌記錄的問題 154
5.3 基於 AspectJ 的日誌記錄之美 156
5.4 開發日誌記錄和追蹤切面 156
- 方法調用追蹤 157
- 異常日誌記錄 163
5.5 常見的日誌慣用法 167
- 記錄方法參數 168
- 縮排日誌語句 170
- 切面優先順序 172
- 更改底層日誌記錄機制 173
- 在多執行緒環境中使用日誌記錄 173
5.6 擴展日誌記錄以供其他用途 174
- 測試 174
- 性能分析 175
5.7 總結 176
6 政策執行:系統範圍的合約 178
6.1 基於 AspectJ 的政策執行概述 179
6.2 當前解決方案及其挑戰 181
6.3 使用 AspectJ 進行執行 182
- 政策執行實現選擇 183
- 在產品生命週期中政策執行的角色 184
6.4 政策執行模式 185
- 偵測特定調用模式的違規 185
- 實現靈活的訪問控制 187
- 執行最佳實踐原則 189
6.5 範例:實現 EJB 程式設計限制 191
- 實現「不使用 AWT」 193
- 實現「不訪問非最終靜態欄位」 194
6.6 範例:實現 Swing 政策 195
- 理解問題 196
- 偵測違規 198
6.7 總結 200
7 優化:資源池化和快取 202
7.1 典型案例 203
- 返回、重用、回收:資源池化的角色 205
- 資源池化問題 206
7.2 使用 AspectJ 進行資源池化 208
- 設計模板切面 208
- 實現模板切面 209
7.3 範例 1:資料庫連接池 211
- 理解資料庫連接池介面 212
- 基於 AspectJ 的資料庫連接池 213
- 實現連接池 216
- 測試我們的解決方案 218
- 調整解決方案 222
7.4 範例 2:執行緒池 223
- 回音伺服器 224
- 理解執行緒池介面 226
- 基於 AspectJ 的執行緒池 226
- 實現執行緒池 230
- 測試我們的解決方案 231
- 調整解決方案 234
7.5 將池化概念擴展到快取 235
- 基於 AspectJ 的快取:第一版 237
- 基於 AspectJ 的快取:第二版 239
- 進一步改進的想法 240
7.6 總結 241

第三部分 AspectJ 的進階應用 243
8 設計模式和慣用法 245
8.1 工作物件創建模式 247
- 當前解決方案 248
- 工作物件創建模式概述 249
- 模式模板 249
- 工作物件創建模式總結 256
8.2 蟲洞模式 256
- 當前解決方案 257
- 蟲洞模式概述 257
- 模式模板 258
- 蟲洞模式總結 260
8.3 異常引入模式 260
- 當前解決方案 261
- 異常引入模式概述 265
- 模式模板 265
- 異常引入模式總結 269
8.4 參與者模式 270
- 當前解決方案 271
- 參與者模式概述 273
- 模式模板 274
- 參與者模式總結 276
8.5 慣用法 277
- 避免無限遞迴 277
- 無效建議 279
- 提供空的切入點定義 280
- 提供預設介面實現 281
8.6 總結 285
9 實現執行緒安全 286
9.1 Swing 的單執行緒規則 287
- 規則 288
- 問題 288