EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring 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 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

Spring AOP

By Priya PedamkarPriya Pedamkar

Spring AOP

Introduction to Spring AOP

AOP (Aspect Oriented Programming) introduces a new way of visualizing the programming structure. Like, OOP (Object-Oriented Programming), whose main unit of modularity is class, the unit of modularity for AOP is an aspect. Spring Framework also provides Aspect-Oriented Programming by implementing AOP concepts. Spring AOP is widely used as an implementation of cross-cutting concern i.e., a module or a functionality which is defined in one place but needed in many places across the project.

In simple terms, the cross-cutting concern is something that is centralized in one place of the project & used across multiple places such as Logging, Authentication, Security, Transaction Management, etc.

Need for Spring AOP

To understand the need for Spring AOP in a better way, let’s look at a problem statement in brief & also how Spring AOP resolved it.

Problem Statement

Let’s say we have around 5 methods in a service layer & we need to perform some notification when a method is called as well as when the control exits the method.

To achieve this, we need to call the notification methods during the start & end in all the 5 methods in the service layer.

So, this is a tedious & repetitive work of writing the same code again & again in all the 5 methods. Also, in the future, if we wanted to remove the notification event for one of the methods, we need to remove the code again. So, there will a problem with maintenance too.

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,754 ratings)

Code Snippets

Spring AOP - Code Snippets 1

 Spring AOP - Code Snippets2

So, if you notice in TransactionService class, for each & every method we are calling startService() and endService() method from NotificationService. This acts as a boilerplate coding & repetitive work for the developers. Also, if the client requires to stop calling the NotificationService for emailService() and loggingService(), again we need to remove the code from TransactionService class.

Solution

With the help of Spring AOP, the aspect of calling the NotificationService during the start & end of each method can be centralized which reduces the repetitive work & also will be a good fit for the future with less maintenance.

So, Spring AOP dynamically provides a way to add cross-cutting concerns (i.e., calling the Notification Service in this case) using simple pluggable XML Configuration files or by using Java Annotations.

Spring AOP Terminologies

Some of the terminologies of Spring AOP are as follows.

Spring AOP Terminologies

  • Aspect
  • Pointcut
  • Advice
  • JoinPoint
  • Weaving

It would be difficult to understand the above terminologies theoretically. So, let us witness a practical example of using Spring AOP and how the terminologies are interconnected.

Spring AOP Code Snippet

Transaction Service (package: com.muneer.service)

Spring AOP - Transaction Service 1

Spring AOP - Transaction Service 2

From the above code snippet, Logging Management needs to be achieved during the start & completion for each & every method in TransactionService.

If we need to write loggers during the start & end of each method in the business layer, that would be a tedious task. So, we are assigning the logging activities to be taken care of by Spring AOP.

So, we are making use of Spring AOP to centralize the Logging Management.

1. Aspect

Aspect is nothing but the concern or the functionality that you are trying to implement generally or centrally.

From the above example, the aspect is Logging Management.

Some of the aspects used across the industry are Logging Management, Transaction Management, Exception Handling, Performance Metrics, Authentication, Security, etc.,

In Simple terms, aspect is the functionality that you are trying to achieve through Spring AOP.

2. Pointcut

A pointcut is nothing but, a kind of regular expression that specifies, what are the method calls that need to be intercepted.

From the code snippet, Line no. 24 & 30 in AspectConfiguration,

@Before("execution(* com.muneer.service.*.*(..))")

The Regular expression denoting that all the methods (using *.*) in package com.muneer.service needs to be intercepted. Also, @Before specifies, that logging needs to happen once the control enters into the method & @After specifies, that logging needs to happen once the control exits the method.

3. Advice

Advice is nothing but, what are the action that needs to be done when a Pointcut is met.

From the code snippet, Line no. 27 & 33 in AspectConfiguration,

logger.info("Started Executing " + joinPoint.getSignature().getName());

So here the Advice is, to start logging the information along with the method name when a Pointcut is met.

4. JoinPoints

At run time, the point at which, when the conditions are met i.e. when the Pointcut is met & advice is being executed, it is referred to as JoinPoints.

It is not limited to only execution of methods, but also when an exception is thrown during Exception Handling Management.

5. Weaving

Weaving is nothing but when a Pointcut is met, the corresponding method will get executed.

From the code snippet, Line no. 24 & 25 – When Pointcut is met, it makes sure to execute before() method. This process of binding the Pointcut with the method is referred to as Weaving.

Advantages of Spring AOP

Some of the advantages of Spring AOP are,

  • AOP is one of the key components of the Spring Framework. It is important to note that Spring IoC Containers is not dependent on AOP. So, it provides the advantage to developers to whether to use AOP or not.
  • As an implementation of cross-cutting concerns, functionalities such as Logging, Notification Management, Authentication, Security, Transaction Management can be kept in a centralized manner & can be used across multiple places in the application.
  • As AOP is implemented using Java, there is no need for any special compilation unit or class loaders.
  • As the cross-cutting concerns are centralized, Boilerplate coding is reduced.
  • It becomes easy for the developers to maintain the system.
  • Spring AOP provides XML based configuration as well as advanced Java Annotation configuration.

Examples of Spring AOP

Let us look at a real-time example with additional features such as printing the method parameters & return value with Spring AOP.

Code Snippets

 example 1

example 1.1

Output

Output

Conclusion

From this article, we have done a deep dive into the problem statements before AOP & how Spring AOP solved it efficiently. Also, we have gone through the Spring AOP terminologies, its advantages along with real-time examples & code snippets.

Recommended Articles

This is a guide to Spring AOP. Here we discuss the introduction to Spring AOP along with its terminologies, advantages, and respective examples. You can also go through our other related articles to learn more–

  1. Spring Architecture
  2. Frameworks In Java
  3. IoC Containers
  4. SpringLayout in Java
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 Login

Forgot Password?

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

Already registered !

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