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.
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.
Cross Cutting Concern :
- Dependency Injection
- Architectural rule enforcement
- Exception Handling
- 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.
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.
- Cross-cutting: This feature introduces the parts of the code that are available in the program
- Advice: Means codes that need to be injected.
- Joinpoint: Is a place where one or more Aspect can be applied.
- Pointcut: Includes a set of Joinpoints that are selected based on a set of indicators.
- Aspect: Is a place where advices and point-cuts are combined.
- 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.
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.
- Before ()
- after() returning
- after() throwing
Methods of using Aspects:
- Source Code Weaving
- Binary Weaving
- Load Time Weaving
- JBoss AOP
- AspectWerkz (merged with AspectJ now)
- Java Aspect Components (JAC)