EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial What is AOP?
Secondary Sidebar
Spring Tutorial
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring boot grpc
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

What is AOP?

By Shalu SharmaShalu Sharma

what is aop

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,388 ratings)
  • 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 –

  1. What is Spring Integration?
  2. Python Global Variable
  3. String Array in Python
  4. Spring AOP
Popular Course in this category
All in One Software Development Bundle (600+ Courses, 50+ projects)
  600+ Online Courses |  3000+ Hours |  Verifiable Certificates |  Lifetime Access
4.6
Price

View Course

Related Courses

Spring Boot Training Program (2 Courses, 3 Project)4.9
Spring Framework Training (4 Courses, 6 Projects)4.8
All in One Data Science Bundle (360+ Courses, 50+ projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more