- Spring 5 Design Patterns
- Dinesh Rajput
- 403字
- 2021-07-08 09:59:26
Applying aspects for cross cutting concerns
In a Spring application, the DI pattern provides us with loose coupling between collaborating software components, but Aspect-Oriented Programming in Spring (Spring AOP) enables you to capture common functionalities that are repetitive throughout your application. So we can say that Spring AOP promotes loose coupling and allows cross-cutting concerns, listed as follows, to be separated in a most elegant fashion. It allows these services to be applied transparently through declaration. With Spring AOP, it is possible to write custom aspects and configure them declaratively.
The generic functionalities that are needed in many places in your application are:
- Logging and tracing
- Transaction management
- Security
- Caching
- Error handling
- Performance monitoring
- Custom business rules
The components listed here are not part of your core application, but these components have some additional responsibilities, commonly referred to as cross-cutting concerns because they tend to cut across multiple components in a system beyond their core responsibilities. If you put these components with your core functionalities, thereby implementing cross-cutting concerns without modularization, it will have two major problems:
- Code tangling: A coupling of concerns means that a cross-cutting concern code, such as a security concern, a transaction concern, and a logging concern, is coupled with the code for business objects in your application.
- Code scattering: Code scattering refers to the same concern being spread across modules. This means that your concern code of security, transaction, and logging is spread across all modules of the system. In other words, you can say there is a duplicity of the same concern code across the system.
The following diagram illustrates this complexity. The business objects are too intimately involved with the cross-cutting concerns. Not only does each object know that it's being logged, secured, and involved in a transactional context, but each object is also responsible for performing those services assigned only to it:

Spring AOP enables the modularization of cross-cutting concerns to avoid tangling and scattering. You can apply these modularized concerns to the core business components of the application declaratively without affecting the aforementioned the above components. The aspects ensure that the POJOs remain plain. Spring AOP makes this magic possible by using the Proxy Design Pattern. We will discuss the Proxy Design pattern more in the coming chapters of this book.
- LabVIEW2018中文版 虛擬儀器程序設計自學手冊
- LabVIEW Graphical Programming Cookbook
- Python數據分析基礎
- Mastering Python High Performance
- Python機器學習:手把手教你掌握150個精彩案例(微課視頻版)
- Visual C#通用范例開發金典
- 計算機應用基礎案例教程
- Python 3.7從入門到精通(視頻教學版)
- QGIS Python Programming Cookbook(Second Edition)
- 征服C指針(第2版)
- Python數據可視化之matplotlib實踐
- 軟件開發中的決策:權衡與取舍
- Python編程:從入門到實踐(第2版)
- Tkinter GUI Application Development Blueprints
- 軟件測試實驗實訓指南