Why do we use AOP (Aspect Oriented Programming)?
In this article, I’m going to familiarize you with another concept called aspect programming or AOP (Aspect Oriented Programming) and talk about the advantages and requirements of this type of programming for you. In previous articles, we provided materials about object-oriented programming or OOP.
We said that object-oriented programming is very efficient in order to model ordinary requests on the system, but if we want to implement a specific request in a certain part of the program, we need a different type of programming that can cover the concerns of each program as well. AOP (Aspect Oriented Programming) is a special type of programming that does exactly the same for us.
Aspect (simplifying aspect Oriented Programming means aspect-oriented programming or AOP) is actually a development-related methodology. With AOP, activities can be performed on certain methods or manufacturers. If you use AOP, the final coding will be very clean. In this coding, each section performs its own responsibility in detail, and there are no more additional codes, and Separation of Concern is realized.
To better understand the performance of AOP (Aspect Oriented Programming), we will review an example together. Suppose you have a large program in hand and want to do a business activity in one way or another. In this case, you need to first check the input values of this method in terms of security and make sure that they are not damaged to your system. You also want this process to be logged accurately. In this case, it is necessary that you, apart from the original code, also write other required codes (Cross Cutting Concerns) that the original code will perform business activity and other codes (Cross Cutting Concerns) will be lost among the original codes and the readability of these codes will drop. In this situation, the only solution is to use AOP. With AOP, you will only need to write the original codes, and the AOP will perform the required logs and security checks of your methods.
Cross Cutting Concern :
- Dependency Injection
- Architectural rule enforcement
- Exception Handling
- Mocking testing scenarios
Above, we said about the benefits of using AOP. But if we want to use this method of programming, we will face several problems that we have mentioned below.
- Less Code Reuse
- Poor Traceability
- More difficult evolution
- Poor code quality
- Lower Productivity
In order to use AOP and app development, we first need to split the app into 2 parts: Business and Cross Cutting Concern, and the task of each section is quite clear. In the next step, we need to implement concerns in separate classes and separate aspects. Finally, we will integrate Cross Cutting and Business using AOP Compiler. This process is referred to in the term Weaving and to the compiler that performs this process, Weaver.
In AOP programming, all Crosscutting Concerns are implemented separately as aspects. In fact, these aspects are the main base of AOP and work similar to the same executable classes in object-oriented programming. These Aspects work in certain parts of the program. Their performance will also be activated if certain methods are called or the specified parts of the program are implemented. In order to learn AOP, you need to also learn specific definitions and concepts that we have outlined 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:
- Call: It is when a method is called.
- Execution: It is related to when a method is running.
- Handler: It’s about when a catch block is running.
- get: Corresponds to when a field is called.
- Set: Corresponds to when a value is written on a field.
- staticinitialization: Corresponds to when a static block runs within a class.
- initialization: Corresponds to when a constructor or constructor runs.
- Preinitialization: Corresponds to when an inherited constructor is implemented.
- adviceexecution: It is related to when a block of advice is run.
How to define an Advice in the app
First, you need to have general information about advice, which we have briefly described below.
- Advices resemble the definition of methods.
- Pointcuts merely specify the location where an aspect function needs to be changed, and in this case it is necessary for them to have a connection with an Advice for the freedom of action of most pointcuts.
- An Advice can run before and after join point.
- An Advice can also run around a join point. These types of advice affect join point execution and can execute it or change parameters when calling methods.
Advice in AOP programming has a variety of types, including:
- Before ()
- after() returning
- after() throwing
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:
- JBoss AOP
- AspectWerkz (merged with AspectJ now)
- Java Aspect Components (JAC)
And in the end,