AOP: Aspect Oriented Programming
20장 스프링 AOP 기능
20.1 Emergence of Aspect Oriented Programming
20.2 Using AOP in Spring
Emergence of Aspect Oriented Programming
The emergence of (AOP) is occasionally accompanied by news of websites falling victim to hacking incidents.
Consequently, in recent times, implementing security features to safeguard against hacking has become an essential requirement when developing web applications.
Furthermore, all web applications incorporate logging functionalities to record user access history as logs. Additionally, common functionalities such as transactions, exception handling, and email notifications are universally used across all web applications.
Therefore, each time a core functionality is added to a web application, the aforementioned common functionalities also need to be implemented from scratch.
However, this eventually leads to a situation where the solution becomes more complex than the problem itself. In Spring, this challenge can be addressed through Aspect-Oriented Programming (AOP), which provides a way to modularize and manage cross-cutting concerns efficiently.
예) Service Class executing member related functions
Here, the upgradeLevel() method is responsible for managing the member's grade, while the actual management of the member's grade is performed using a for loop. However, even when implementing the member grade functionality, auxiliary features such as logging, security, and transactions need to be implemented alongside.
In such a scenario, if there is a need to add a feature that sends an email to the member when their information or grade is modified, you would have to manually add the corresponding code directly into the upgradeLevel() method.
However, in the case of a large-scale web application, implementing these tasks individually for each method in classes can be time-consuming and lead to complex source code.
In other words, potential maintenance issues might arise. This is where Aspect-Oriented Programming (AOP) comes into play.
AOP involves the separation of primary functionality and auxiliary features within a method, selectively applying them to methods.
By utilizing AOP, auxiliary features scattered throughout the entire codebase can be consolidated into a single location for management. Furthermore, auxiliary features can be selectively applied to desired primary functionalities, resulting in simpler code and improved readability.
As a result, functionalities like the member grade management mentioned earlier can have auxiliary features applied to methods without the need for individual implementation for each method.
Once auxiliary features are created, they can be easily incorporated using a single configuration whenever needed, much like using components as needed. This naturally leads to simplified code and improved readability.
Figure 20-1 illustrates a shopping mall structure where various auxiliary features are applied to the primary functionalities of the shopping mall using AOP.
Each auxiliary feature is prepared in advance and can be selectively applied to methods or classes responsible for carrying out the primary functionalities.
20.2 Using AOP in Spring
Ways to Implement AOP Functionality in the Spring Framework:
• Using APIs provided by the Spring framework
• Using the @Aspect annotation
Process of Implementing AOP Functionality Using Spring API:
1. Specify the target class.
2. Specify the advice class.
3. Configure the pointcut in the configuration file.
4. Configure an advisor in the configuration file that combines the advice and the pointcut.
5. Use Spring's ProxyFactoryBean class in the configuration file to apply the advice to the target.
6. Access and use the bean object through the getBean() method.
The abstract method functionality of the Advice interfaces in the Spring Framework, which perform AOP functionality when methods provided by the Spring Framework are invoked.