Why do we use AOP (Aspect Oriented Programming)?

My name is Arsalan MirBozorgi and in this article I want to talk to you about aspect-oriented programming or AOP and explain the benefits of this type of programming. We’ve talked about object-oriented programming in previous articles. Object-oriented programming, as its name implies, is object-based and is very efficient in modeling typical requests on the system.  But if we want to execute a request in a certain part of a program, we have to turn to another type of programming so that we can well cover the concerns of each program. Aspect-oriented programming is one of those languages ​​that can do this for us.

Object-oriented programming

Aspect-oriented programming is actually a development methodology. In aspect-oriented programming, you can perform a variety of activities using different methods. If you use this method of programming, your final coding will be much more orderly. In this method of coding, each department recognizes its own responsibility and does it, and there are no additional and useless codes, and Separation of Concern is well achieved.

To give you a better understanding of aspect-oriented programming performance, let us give you an example. Suppose you have a very large project underway and you want to run business activities in one method.  To achieve your goal you should check the input values ​​of this method for safety and make sure that they do not harm your system. You also need to make sure that the process is logged in correctly. In this case, you have to write other required code (Cross Cutting Concerns) in addition to the main code and add them to your main code. Note that these required codes will be lost among your original codes and as a result the readability of these codes will be greatly reduced. In this case, you only need to use object-oriented programming to be able to write the main code, and the aspect-oriented programming itself will fully perform the required logs and check the safety of your methods.

Object-oriented programming

Cross Cutting Concern :

  • Logging/Tracing
  • Transactions
  • Security
  • Dependency Injection
  • Architectural rule enforcement
  • Profiling
  • Exception Handling
  • Caching
  • Mocking testing scenarios

Lately, we have talked about the benefits of using AOP. But if we want to use this programming language, we will have several problems that we will mentioned below:

  • Less Code Reuse
  • Poor Traceability
  • More difficult evolution
  • Poor code quality
  • Lower Productivity

To use AOP and develop the program, we must first divide the program into two parts, Business and Cross Cutting Concern, and the duty of each part must be clear. In the next step, we have to run the Concerns and classes in separate aspects. In the last step, we will integrate Cross Cutting and Business using AOP Compiler. This process is called Weaving and the compiler that performs this process is called Weaver.

Object-oriented programming

AOP Programming

In AOP programming, all Crosscutting Concerns are executed separately as different Aspects. These aspects are the basis of AOP and act similarly to the executive classes in object-oriented programming. These aspects do their job in certain parts of the program. Their function will also be activated if certain methods are called or specified parts of the program are executed. In order to learn AOP, you also need to learn the specific definitions and concepts that we have brought them below.

  1. Cross-cutting: This feature introduces the parts of the code that are available in the program
  2. Advice: Means codes that need to be injected.
  3. Joinpoint: Is a place where one or more Aspect can be applied.
  4. Pointcut: Includes a set of Joinpoints that are selected based on a set of indicators.
  5. Aspect: Is a place where advices and point-cuts are combined.
  6. Weaving: Means merging classes and aspects.

Joinpoint in AOP programming:

1.Call: A time when a method is called.

2.Execution: A time when a method is running.

3.Handler: A time when a catch block is running.

4.get: A time when a field is read.

5.set: A time when a value is written to a field.

6.static initialization: A time when a static block is executed within a class.

7.initialization: A time when a constructor is running.

8.Preinitialization: A time when an inherited constructor is executed.

9.advice execution: A time when an advice block is executed.

Object-oriented programming

How to define an Advice in the app

First of all, you should have general information about Advice, which we have below.

  • Advice is similar to defining methods.
  • Pointcuts simply specify the place where the function of an aspect needs to change, in which case they need to be associated with an Advice for more freedom of action.
  • Advice can be run before and after the join point.
  • Advice can also run around a join point. These types of advice affect the execution of the join point and can execute it or change the parameters when calling methods.

Object-oriented programming

Advice in AOP programming has a variety of types, including:
  • Before ()
  • after() returning
  • after() throwing
  • after()
  • around()

Methods of using Aspects:

There are 3 ways to use Aspects. One is through Java code and the other is through annotations or through XML.
Weaving in AspectJ has different models (AspectJ Weaving Model) that we have mentioned below:
  • Source Code Weaving
  • Binary Weaving
  • Load Time Weaving
There are also frameworks and technologies that use AOP extensively. These include:
  • AspectJ
  • Spring
  • JBoss AOP
  • AspectWerkz (merged with AspectJ now)
  • Java Aspect Components (JAC)

And in the end,

. Mirbazorgi’s website aims to help you learn and fix your problems by providing articles and practical experiences. Email me if you have any questions.

One Comment

  1. www.xmc.pl 04/28/2022 at 3:02 am - Reply

    I have a limited account and an administrator account on Windows XP. I would like to import all of my Firefox settings from my Limited Account to my Admin Account so they are exactly the same..

Leave A Comment

88 − = 78

Please Send Email

Your message sent successfully
There has been an error