SlideShare a Scribd company logo
1 of 30
Spring AOP

ASPECT ORIENTED PROGRAMMING
Spring AOP - Introduction

Aspect – Unit of Modularity

Entails breaking down program logic into distinct parts called socalled 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

What's hot

Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
Asif Tayef
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 
Session 7 code_functional_coverage
Session 7 code_functional_coverageSession 7 code_functional_coverage
Session 7 code_functional_coverage
Nirav Desai
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11
Terry Yoast
 
Testcase definition
Testcase definitionTestcase definition
Testcase definition
Oana Feidi
 

What's hot (20)

Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
 
Model-Driven Testing with UML 2.0
Model-Driven Testing with UML 2.0Model-Driven Testing with UML 2.0
Model-Driven Testing with UML 2.0
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
 
Template pattern
Template patternTemplate pattern
Template pattern
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Control structures i
Control structures i Control structures i
Control structures i
 
Session 7 code_functional_coverage
Session 7 code_functional_coverageSession 7 code_functional_coverage
Session 7 code_functional_coverage
 
White box testing
White box testingWhite box testing
White box testing
 
Model Driven Testing: requirements, models & test
Model Driven Testing: requirements, models & test Model Driven Testing: requirements, models & test
Model Driven Testing: requirements, models & test
 
Var arg methods
Var arg methodsVar arg methods
Var arg methods
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
JUnit Goodness
JUnit GoodnessJUnit Goodness
JUnit Goodness
 
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 2) A Concise and Practical Introduction to Programming Algorithms in...
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11
 
Java unit3
Java unit3Java unit3
Java unit3
 
Testcase definition
Testcase definitionTestcase definition
Testcase definition
 
Junit4.0
Junit4.0Junit4.0
Junit4.0
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 

Viewers also liked (12)

Welcome to the Next Level in Sponsorship
Welcome to the Next Level in SponsorshipWelcome to the Next Level in Sponsorship
Welcome to the Next Level in Sponsorship
 
3 36764 the_nextlevelofsocialsponsorshipsbymassrelevancemedia
3 36764 the_nextlevelofsocialsponsorshipsbymassrelevancemedia3 36764 the_nextlevelofsocialsponsorshipsbymassrelevancemedia
3 36764 the_nextlevelofsocialsponsorshipsbymassrelevancemedia
 
The civil war begins
The civil war beginsThe civil war begins
The civil war begins
 
The civil war begins
The civil war beginsThe civil war begins
The civil war begins
 
The civil war begins
The civil war beginsThe civil war begins
The civil war begins
 
The civil war begins final
The civil war begins finalThe civil war begins final
The civil war begins final
 
Web services
Web services Web services
Web services
 
Outline of Android
Outline of AndroidOutline of Android
Outline of Android
 
Sales and Marketing Alignment
Sales and Marketing AlignmentSales and Marketing Alignment
Sales and Marketing Alignment
 
Test automationslides
Test automationslidesTest automationslides
Test automationslides
 
Networking Fundamentals
Networking FundamentalsNetworking Fundamentals
Networking Fundamentals
 
Effective Spring Transaction Management
Effective Spring Transaction ManagementEffective Spring Transaction Management
Effective Spring Transaction Management
 

Similar to Spring aop

2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 

Similar to Spring aop (20)

Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
 
Spring framework aop
Spring framework aopSpring framework aop
Spring framework aop
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
 
Spring - Part 3 - AOP
Spring - Part 3 - AOPSpring - Part 3 - AOP
Spring - Part 3 - AOP
 
Spring aop
Spring aopSpring aop
Spring aop
 
Programming style guideline very good
Programming style guideline very goodProgramming style guideline very good
Programming style guideline very good
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Generic Synchronization Policies in C++
Generic Synchronization Policies in C++Generic Synchronization Policies in C++
Generic Synchronization Policies in C++
 
Introduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingIntroduction to Aspect Oriented Programming
Introduction to Aspect Oriented Programming
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
 
J Unit
J UnitJ Unit
J Unit
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
 
Spring framework part 2
Spring framework  part 2Spring framework  part 2
Spring framework part 2
 
Understanding Annotations in Java
Understanding Annotations in JavaUnderstanding Annotations in Java
Understanding Annotations in Java
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design Pattern
 
11. java methods
11. java methods11. java methods
11. java methods
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Spring aop

  • 2. Spring AOP - Introduction Aspect – Unit of Modularity  Entails breaking down program logic into distinct parts called socalled 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