EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring AOP
 

Spring AOP

Priya Pedamkar
Article byPriya Pedamkar

Updated March 27, 2023

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW