Updated March 27, 2023
What is AOP?
Aspect-Oriented Programming is the full form for AOP, as its name suggests we use aspects in programming language one of the major use of aspects is that it allows us to break our code into different modules and this process known as modularization. In AOP aspects enable us to implement crosscutting concerns like logging, transaction, etc.
Why do we Need AOP?
- AOP enables the aspect-oriented programming into the spring application, these aspects further enabled the modularization like security, transaction, logging that cut multiple type objects. It also allows us to dynamically add the cross-cutting concern around, before, after the code using some configuration. It makes our code easy to maintain for the future, removes repeated code, and maintains readability.
- We have one example for security in our web applications while writing the code we have so many areas where security is required i.e. everyone cannot access some part of application or some authentication is required so rather by repeating code on every method or class we will just write it into common class and use them for whole application.
- So for us, it will break our program code into various parts these small parts are called concern and we use this to increase the modularity.
Example to Use
Below is a simple example to show AOP (how to use it):
Suppose in a class we have n numbers of different methods so AOP provides us to add this cross-cutting concern after, around, before our logic.
class AOPDemo{
public void B1(){...}
public void B2(){...}
public void C3(){...}
public void C4(){...}
public void D5(){...}
public void D1(){...}
public void D2(){...}
public void Z1(){...}
public void Z2(){...}
public void Z3(){...}
//.. so many methods can be define here
}
In the above example, we have so many methods suppose I have some scenarios in which I need to maintain logs after the database connection is set up or after calling method till C.
- without AOP: I need to write the code in all methods that mean repeated code for doing the same thing but if in future requirement change then I need to make the change everywhere which leads to the maintained problem.
- With AOP: Now with AOP we do not need to write the same logic we will just create the cross concern and its entry will go into the XML file.
Working of AOP
We have various terminology in AOP like :
- Joinpoint
- Advice
- Pointcut
- Introduction
- Target Object
- Aspect
- Interceptor
- AOP Proxy
- Weaving
Let’s discuss them one by one :
1. Aspect: In this, we create a normal which but it is going to implement the cross-cutting concerns like security, logging, transaction, etc, all these are known as aspect. For these, we can either use XML configuration or annotation-based configuration. We need to make its entry in .xml file if we are using xl configuration and also we can use @Aspect annotation-based configuration.
2. Weaving: In this process, we try to link our Aspects with the Advised which we will discuss in the coming points. This process can be done at compile-time, load time or runtime. But in spring it is done at run time.
3. Advice: This tells us when to perform the job or we can define it as action taken by aspect.
We have five types of Advice in AOP after, before, afteThrowing, Around, AfterReturning.
- After: This advice is run when the method completes its execution. It does not depend upon the output of the program. We can use @After annotation to define it.
- Around: This advice run before and after the execution of the method, because it wraps around the whole code. We can use @Around annotation to define it. Hence it is considered as the powerful advice in all the advice.
- Before: This advice is run before the method execution. We can use @Before to define it.
- AfterReturning: This advice runs after the method execution but method execution should be successful without any exception. Hence we can say it get executed after the successful method execution. We can use @AfterReturning to define it.
- AfterThrowing: This advice runs when a method throws a runtime exception. We can use @AfterThrowing to define it.
Code:
@Aspect
class LoggingDemo {
// Before Advice
@Before("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice1()
{
System.out.println("Demo for Before advice");
}
// AfterRunning Advice
@AfterReturning("execution(public void com.aspect.ImplementAspect.myMethod())")
public void loggingAdvice5()
{
System.out.println("Demo for AfterReturning advice");
}
// Around Advice
@Around("execution(public void com.aspect.ImplementAspect.myMethod())")
public void loggingAdvice3()
{
System.out.println("Demo for around advice");
}
// AfterThrowing Advice
@AfterThrowing("execution(" public void com.aspect.ImplementAspect.aspectCall())
")
public void
loggingAdvice4()
{
System.out.println("Demo for AfterThrowing advice");
}
// After Advice
@After("execution(public void com.aspect.ImplementAspect.aspectCall())")
public void loggingAdvice2()
{
System.out.println("Demo for After Advice.");
}
}
4. Joinpoint: In our code, we have many areas where we can apply points to execute our advice these points are basically known as Joinpoint.
5. Pointcut: It matches the joining point because we cannot apply advice on every single place.
6. Introduction: Introduction of files and methods.
7. Target Object: It is a proxy object.
8. Interceptor: This can contain only one piece of advice.
9. AOP proxy: It is a JDK dynamic proxy.
Features of AOP
AOP provides many things like :
- Before Advice
- After Advice
- Around Advice
- After Returning Advice
- After Throwing Advice
This can be used to provide centralized logging, security, etc.
Advantages and Disadvantages of AOP
Below are the advantages and disadvantages of AOP:
Advantage
- Maintenance
- Debugging
- By having cross-cutting concerns it helps us to improve the understandability and maintainability.
- Reuse of aspects and classes
- Reduce the cost of coding.
- Shorter code
Disadvantage
- Code bloat: In AOP small sources can lead to large objects.
- Toolchain, profiler, and debuggers are not available.
- Runtime overhead
Conclusion
AOP basically used for modularization which divides our code into various small codes. Thus increase readability, maintainability of code. It provides us various terminology to implement this. Which helps us to create centralize logging, security, etc.
Recommended Articles
This is a guide to What is AOP? Here we discuss why do we need, various terminology in AOP with features and advantages and disadvantages. You can also go through our other related articles to learn more –