AOP (Aspect Oriented Programming)

AOP (Aspect Oriented Programming)

Carlos
Carlos
  1. Introducing AOP
    While building application programs, there are many software-related issues that we need to pay attention to. For example, we build an account creation registration system for a bank. In addition to the main task of allowing users to create accounts (core concern), the system must also ensure other issues (cross-cutting concerns) such as user authentication, constraint checking, transaction management, and processing. exception handling, logging, debugging, measuring application performance, ...
    As you can see, the logic of our program has to do a lot of things like logging, opening/closing transactions, handling exceptions, etc. When there are many similar methods in the application, our code is tightly coupled. together, duplicate code, fragmentation in many places, difficulty modifying and adding new logic, etc. To solve this problem, we can use AOP techniques.
  2. What is AOP?
    AOP is the acronym for Aspect Oriented Programming. AOP is a programming technique that allows decomposing a program into separate, independent modules. When operating, the program will combine modules to perform functions, but when modifying functions, only one specific module needs to be modified.
    AOP, also known as Aspect Oriented Software Development (AOSD), is a design principle that helps separate requirements or concerns in the program into independent components and thereby increase productivity. flexibility for the program. In Separation of concerns, it is said that similar problems should be solved in a unit of code. In functional programming, a unit of code is a function/method, and in object-oriented programming (OOP), a unit of code is a class.
    In AOP, our program is divided into 2 types of concerns:
    • Core concern/ Primary concern: is the requirement, the main processing logic of the program.
    • Cross-cutting concern: is the secondary processing logic that needs to be performed by the program when the core concern is called such as security, logging, tracing, monitoring, ...
  3. What is Weaving?

Basically Weaving is the process of linking aspect and non-aspect components of a program to create the desired output. There are a few differences between AOP systems in terms of How to create Weaving. Can be divided into types of Weaving: Compile-time weaving (static weaving), Load-Time Weaving and Run-time weaving (dynamic weaving).
*Compile-time weaving :
       - Pre-Compile Weaving : using the preprocessor ( pre-processor) to combine aspect code and non-aspect code together before the code is compiled into Java byte code (.class).
        - Post-Compile Weaving / Binary weaving: this method is used to inject aspect code into Java .class files have been compiled.
* Load-Time Weaving: this method is used to inject aspect code when the class that needs to use aspect is loaded into the JVM, meaning while the application is running.
* Run-time weaving : Performs weaving and unweaving of aspect and non-aspect code at run-time.

  1. Static weaving
    Static  weaving is the process of combining aspect code and non-aspect code together before the code is compiled into Java byte code (.class) using a pre-processor. Therefore, the original code is changed only once at compile time. The performance of this combined code is equivalent to traditionally written code.
    The limitation of the Static weaving method is the difficulty in identifying the aspect's code later or making changes to the aspect's code. Every time an aspect's code is changed, all code that uses aspect must be recompiled.
  2. Dynamic weaving
    Dynamic weaving overcomes some limitations encountered when weaving is performed at compile time (Compile-time weaving/ Static weaving). The need for recompilation, redeployment, and restart can be avoided by performing run-time weaving.
    There is a slight difference between load-time and run-time weaving.
        • Load-time weaving simply delays the weaving process until the classes are loaded by the class loader. This approach requires using a weaving class loader or replacing the class loader with another loader. The drawback is increased load-time and lack of access to aspects while running.
      • Run-time weaving is the process of weaving and unweaving at run-time. This approach requires new mechanisms to interfere with running computations.
    Different AOP Frameworks have different ways of implementing dynamic weaving. While AspectWerkz uses byte code modification through JVM-level functionality and “hotswap” architecture to perform weaving of classes at run-time, Spring AOP Framework relies on proxies instead of class loaders or on objects. JVM argument.
    Dynamic weaving allows to speed up the design and testing phases in software development, as new aspects can be added or existing aspects can be changed without the need to recompile and redeploy existing aspects. application. However, a major drawback is reduced performance, because weaving happens at run-time.
  3. Advantages and disadvantages of AOP
    Advantages:
    • Increase the efficiency of Object-orented programming (OOP).
    • AOP is not used to replace OOP but to complement OOP, where OOP is lacking in creating complex applications. • Maximize the reusability of source code.
    • Ensure Single responsibility principle: each module only does what it needs to do.
    • Follow the principle “You aren't gonna need it – YAGNI” – we only install what we really need, never before.
    • Modularization at process/function level.
    • Code is cleaner due to separation of main processing and related processing.
    • The main function of the program does not need to know other secondary functions.
    • Additional functions can be added and turned on or off at run-time depending on requirements.
    • Changes, if any, to secondary functions will not affect the main program.
    • The system will be flexible and minimize the interdependency of modules.
    Disadvantages:
    • The concept is quite abstract, the level of abstraction of the program is high
    • Complex program flow.
    Thanks for your reading.