Spring AOP
ASPECT ORIENTED PROGRAMMING
Spring AOP - Introduction

Aspect – Unit of Modularity

Entails breaking down program logic into distinct parts called so-
called concerns. The functions that span multiple points of an
application are called cross-cutting concerns

Cross-cutting concerns are conceptually separate from the
application's business logic.
AOP in Business

Logging

Transaction Management – Declarative

Caching

Security
AOP Terminologies

Aspect

Advice

Join Point

Pointcut

Introduction

Target Object

Weaving
AOP Advice

This is the actual action to be taken either before or after
the method execution.

This is actual piece of code that is invoked during program
execution by Spring AOP framework.
Types of Advices

Before - Run advice before the a method execution.

After - Run advice after the a method execution regardless
of its outcome.

After Returning - Run advice after the a method execution
only if method completes successfully.

After throwing - Run advice after the a method execution
only if method exits by throwing an exception.

Around- Run advice before and after the advised method
is invoked.
Advices - Examples

@Before("execution(* *.*(..))")

@After("execution(* *.*(..))")

@AfterReturning("execution(* *.*(..))")

@AfterThrowing("execution(* *.*(..))")

@Around("execution(* *.*(..))")
Aspects Implementation

XML Schema Based

Annotaion Based
Join Point

An Advice is applied to different program execution points,
which are called join points.

An Advice to take the correct action, it often requires
detailed information about join points.
Accessing Join Point
Information

Kind

Method signature

Argument values

Target Object

Proxy Object
AspectJ Precedence

More than one Aspect classes – need Precedence

How to implement?

Ordered Interface

Order Annotation
Point Cut Expressions

It is a powerful expression language that can match
various kinds of join points.

Indicate which method should be intercept, by method
name or regular expression pattern.

expression(<method scope> <return type> <fully qualified
class name>.*(parametes))
Continue..,

method scope: Advice will be applied to all the methods
having this scope. For e.g., public, private, etc. Please note
that Spring AOP only supports advising public methods.

return type: Advice will be applied to all the methods
having this return type.
Continue..,

fully qualified class name: Advice will be applied to all the
methods of this type. If the class and advice are in the
same package then package name is not required

parameters: You can also filter the method names based
on the types. Two dots(..) means any number and type of
parameters.
Pointcut Examples

execution(* com.aspects.pointcut.DemoClass.*(..)) : This
advice will be applied to all the methods of DemoClass.

execution(* DemoClass.*(..)): You can omit the package if
the DemoClass and the advice is in the same package.

execution(public * DemoClass.*(..)): This advice will be
applied to the public methods of DemoClass.
Pointcut Examples

execution(public int DemoClass.*(..)): This advice will be
applied to the public methods of DemoClass and returning
an int.

execution(public int DemoClass.*(int, ..)): This advice will be
applied to the public methods of DemoClass and returning
an int and having first parameter as int.

execution(public int DemoClass.*(int, int)): This advice will
be applied to the public methods of DemoClass and
returning an int and having both parameters as int.
Reusing Pointcut Expression
@Pointcut("execution(* *.*(..))")
private void loggingOperation()
{}
@Before("loggingOperation()")
public void logBefore(JoinPoint joinPoint)
{}
PointCut Class Aspect
@Aspect
Class PointCut
{
@Pointcut("execution(* *.*(..))")
private void loggingOperation()
{}
}
@AfterReturning(
pointcut = "PointCut.loggingOperation()",
returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
}
Type Signature Pattern

Within - pointcut expressions matches all join points within
certain types.

pointcut matches all the method execution join points
within the com.msoftgp.spring package
within(com.msoftgp.spring.*)

To match the join points within a package and its
subpackage, you have to add one more dot before the
wildcard.
within(com.msoftgp.spring..*)

The pointcut expression matches the method execution
join points within a particular class:
within(com.msoftgp.spring.ArithmeticCalculatorImpl)

Again, if the target class is located in the same package
as this aspect, the package name can be omitted.
within(ArithmeticCalculatorImpl)

You can match the method execution join points within all
classes that implement the ArithmeticCalculator interface
by adding a plus symbol.
within(ArithmeticCalculator+)
Combining Pointcuts
@Aspect
public class CalculatorPointcuts {
@Pointcut("within(ArithmeticCalculator+)")
public void arithmeticOperation() {}
@Pointcut("within(UnitCalculator+)")
public void unitOperation() {}
@Pointcut("arithmeticOperation()|| unitOperation()")
public void loggingOperation() {}
}
Pointcut Parameters

execution(* *.*(..)) && target(target) && args(a,b)

Public void logBefore(Object target,int a,int b)
AOP Introduction

Special type of Advice

Same effect as multiple inheritance

Mechanism used – Dynamic Proxy

@DeclareParents
Load Time Weaving AspectJ

Purpose – Additional Pointcuts ,apply aspects
to objects created outside the Spring IoC container

Weaving is the process of applying aspects to your target
objects.

Weaving happens at runtime through dynamic proxies.

Supports compile time and load time weaving.
Types of AspectJ Weaving

Complie time weaving

Load time weaving

By AspectJ Weaver

Spring Load time Weaver

Application Used : Caching
Configuring AspectJ Aspects in
Spring

Factory-method is aspectof()
Injecting Spring Beans into
Domain Beans

Wiring Spring objects and Domain Objects

Injection of Spring beans is a cross cutting concern

Annotate the Domain class with @Configurable
THANK YOU

More Related Content

PDF
Spring framework aop
PPT
Spring AOP
KEY
Spring AOP
PPTX
Spring boot - an introduction
PDF
Spring Framework - AOP
PDF
Spring Framework - Core
PPTX
Java 8 - Features Overview
PPT
Java Persistence API (JPA) Step By Step
Spring framework aop
Spring AOP
Spring AOP
Spring boot - an introduction
Spring Framework - AOP
Spring Framework - Core
Java 8 - Features Overview
Java Persistence API (JPA) Step By Step

What's hot (20)

PDF
Java 8 Stream API. A different way to process collections.
PDF
Spring core module
PPSX
Spring - Part 1 - IoC, Di and Beans
PPTX
Spring data jpa
PDF
Spring Data JPA
PPTX
Spring boot Introduction
PPTX
Spring framework in depth
ODP
Introduction to Spring Framework and Spring IoC
PPTX
Spring boot
PDF
REST APIs with Spring
PPTX
Spring Framework
PPT
Spring Framework
PPTX
Spring boot
PPTX
Spring AOP in Nutshell
PPT
Java Class Loader
PDF
Collections In Java
PPTX
The Singleton Pattern Presentation
PDF
Spring Boot
PPTX
jQuery Fundamentals
PDF
Gradle Introduction
Java 8 Stream API. A different way to process collections.
Spring core module
Spring - Part 1 - IoC, Di and Beans
Spring data jpa
Spring Data JPA
Spring boot Introduction
Spring framework in depth
Introduction to Spring Framework and Spring IoC
Spring boot
REST APIs with Spring
Spring Framework
Spring Framework
Spring boot
Spring AOP in Nutshell
Java Class Loader
Collections In Java
The Singleton Pattern Presentation
Spring Boot
jQuery Fundamentals
Gradle Introduction
Ad

Similar to Spring AOP (20)

PPTX
Spring aop concepts
PPTX
Aspect Oriented Programming
PPTX
Session 45 - Spring - Part 3 - AOP
PPSX
Spring - Part 3 - AOP
PDF
Spring aop
DOC
Programming style guideline very good
PPTX
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
DOCX
Methods in Java
PDF
2-Algorithms and Complexit data structurey.pdf
PPTX
2-Algorithms and Complexity analysis.pptx
PPTX
Introduction to Aspect Oriented Programming
PPT
Generic Synchronization Policies in C++
PPT
PPTX
Spring framework part 2
PPTX
Mastering Python lesson 4_functions_parameters_arguments
PPT
TDD And Refactoring
PPT
Spring AOP
PPTX
11. java methods
PPT
UNIT III.ppt
PPT
UNIT III (2).ppt
Spring aop concepts
Aspect Oriented Programming
Session 45 - Spring - Part 3 - AOP
Spring - Part 3 - AOP
Spring aop
Programming style guideline very good
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
Methods in Java
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexity analysis.pptx
Introduction to Aspect Oriented Programming
Generic Synchronization Policies in C++
Spring framework part 2
Mastering Python lesson 4_functions_parameters_arguments
TDD And Refactoring
Spring AOP
11. java methods
UNIT III.ppt
UNIT III (2).ppt
Ad

Recently uploaded (20)

PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
Flame analysis and combustion estimation using large language and vision assi...
PPT
Geologic Time for studying geology for geologist
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PPTX
Modernising the Digital Integration Hub
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PPTX
Configure Apache Mutual Authentication
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PPTX
The various Industrial Revolutions .pptx
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
Custom Battery Pack Design Considerations for Performance and Safety
A contest of sentiment analysis: k-nearest neighbor versus neural network
UiPath Agentic Automation session 1: RPA to Agents
Flame analysis and combustion estimation using large language and vision assi...
Geologic Time for studying geology for geologist
Consumable AI The What, Why & How for Small Teams.pdf
Modernising the Digital Integration Hub
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Configure Apache Mutual Authentication
The influence of sentiment analysis in enhancing early warning system model f...
Chapter 5: Probability Theory and Statistics
Enhancing plagiarism detection using data pre-processing and machine learning...
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Convolutional neural network based encoder-decoder for efficient real-time ob...
The various Industrial Revolutions .pptx
A review of recent deep learning applications in wood surface defect identifi...
Microsoft Excel 365/2024 Beginner's training
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
Taming the Chaos: How to Turn Unstructured Data into Decisions

Spring AOP

  • 2. Spring AOP - Introduction  Aspect – Unit of Modularity  Entails breaking down program logic into distinct parts called so- called concerns. The functions that span multiple points of an application are called cross-cutting concerns  Cross-cutting concerns are conceptually separate from the application's business logic.
  • 3. AOP in Business  Logging  Transaction Management – Declarative  Caching  Security
  • 5. AOP Advice  This is the actual action to be taken either before or after the method execution.  This is actual piece of code that is invoked during program execution by Spring AOP framework.
  • 6. Types of Advices  Before - Run advice before the a method execution.  After - Run advice after the a method execution regardless of its outcome.  After Returning - Run advice after the a method execution only if method completes successfully.  After throwing - Run advice after the a method execution only if method exits by throwing an exception.  Around- Run advice before and after the advised method is invoked.
  • 7. Advices - Examples  @Before("execution(* *.*(..))")  @After("execution(* *.*(..))")  @AfterReturning("execution(* *.*(..))")  @AfterThrowing("execution(* *.*(..))")  @Around("execution(* *.*(..))")
  • 8. Aspects Implementation  XML Schema Based  Annotaion Based
  • 9. Join Point  An Advice is applied to different program execution points, which are called join points.  An Advice to take the correct action, it often requires detailed information about join points.
  • 10. Accessing Join Point Information  Kind  Method signature  Argument values  Target Object  Proxy Object
  • 11. AspectJ Precedence  More than one Aspect classes – need Precedence  How to implement?  Ordered Interface  Order Annotation
  • 12. Point Cut Expressions  It is a powerful expression language that can match various kinds of join points.  Indicate which method should be intercept, by method name or regular expression pattern.  expression(<method scope> <return type> <fully qualified class name>.*(parametes))
  • 13. Continue..,  method scope: Advice will be applied to all the methods having this scope. For e.g., public, private, etc. Please note that Spring AOP only supports advising public methods.  return type: Advice will be applied to all the methods having this return type.
  • 14. Continue..,  fully qualified class name: Advice will be applied to all the methods of this type. If the class and advice are in the same package then package name is not required  parameters: You can also filter the method names based on the types. Two dots(..) means any number and type of parameters.
  • 15. Pointcut Examples  execution(* com.aspects.pointcut.DemoClass.*(..)) : This advice will be applied to all the methods of DemoClass.  execution(* DemoClass.*(..)): You can omit the package if the DemoClass and the advice is in the same package.  execution(public * DemoClass.*(..)): This advice will be applied to the public methods of DemoClass.
  • 16. Pointcut Examples  execution(public int DemoClass.*(..)): This advice will be applied to the public methods of DemoClass and returning an int.  execution(public int DemoClass.*(int, ..)): This advice will be applied to the public methods of DemoClass and returning an int and having first parameter as int.  execution(public int DemoClass.*(int, int)): This advice will be applied to the public methods of DemoClass and returning an int and having both parameters as int.
  • 17. Reusing Pointcut Expression @Pointcut("execution(* *.*(..))") private void loggingOperation() {} @Before("loggingOperation()") public void logBefore(JoinPoint joinPoint) {}
  • 18. PointCut Class Aspect @Aspect Class PointCut { @Pointcut("execution(* *.*(..))") private void loggingOperation() {} }
  • 19. @AfterReturning( pointcut = "PointCut.loggingOperation()", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { }
  • 20. Type Signature Pattern  Within - pointcut expressions matches all join points within certain types.  pointcut matches all the method execution join points within the com.msoftgp.spring package within(com.msoftgp.spring.*)  To match the join points within a package and its subpackage, you have to add one more dot before the wildcard. within(com.msoftgp.spring..*)
  • 21.  The pointcut expression matches the method execution join points within a particular class: within(com.msoftgp.spring.ArithmeticCalculatorImpl)  Again, if the target class is located in the same package as this aspect, the package name can be omitted. within(ArithmeticCalculatorImpl)
  • 22.  You can match the method execution join points within all classes that implement the ArithmeticCalculator interface by adding a plus symbol. within(ArithmeticCalculator+)
  • 23. Combining Pointcuts @Aspect public class CalculatorPointcuts { @Pointcut("within(ArithmeticCalculator+)") public void arithmeticOperation() {} @Pointcut("within(UnitCalculator+)") public void unitOperation() {} @Pointcut("arithmeticOperation()|| unitOperation()") public void loggingOperation() {} }
  • 24. Pointcut Parameters  execution(* *.*(..)) && target(target) && args(a,b)  Public void logBefore(Object target,int a,int b)
  • 25. AOP Introduction  Special type of Advice  Same effect as multiple inheritance  Mechanism used – Dynamic Proxy  @DeclareParents
  • 26. Load Time Weaving AspectJ  Purpose – Additional Pointcuts ,apply aspects to objects created outside the Spring IoC container  Weaving is the process of applying aspects to your target objects.  Weaving happens at runtime through dynamic proxies.  Supports compile time and load time weaving.
  • 27. Types of AspectJ Weaving  Complie time weaving  Load time weaving  By AspectJ Weaver  Spring Load time Weaver  Application Used : Caching
  • 28. Configuring AspectJ Aspects in Spring  Factory-method is aspectof()
  • 29. Injecting Spring Beans into Domain Beans  Wiring Spring objects and Domain Objects  Injection of Spring beans is a cross cutting concern  Annotate the Domain class with @Configurable