SlideShare a Scribd company logo
Spring AOP
TSC DELHI
INDIA
Spring AOP
Features
• What are cross cutting concerns?
• How do you implement cross cutting concerns in
an application?
• If you would want to log every request to a web
application, what are the options you can think of?
• If you would want to track performance of every
request, what options can you think of?
• What is an Advice, Aspect, Pointcut and Joint Point,
Weaving ?
• What are the different types of AOP Advices Types?
• How do you implement AOP with Spring Boot?
• What are the AOP Best Practices?
Use Case :
Business
Problems
• Read/write locks. Instead of replicating the same snippet, I used an aspect to define the
methods that needed a read lock or an exclusive lock.
• Exception Handling: don't need to repeat the horrible list of try ... catch, catch, catch etc
- also means the exception handling is guaranteed to be consistent.
• Can use it to track events in the system, whenever some object is CRUD you can send an
event to your event system.
• Can use it for sending web/ios/email notifications before /after any method call.
• Audit logging (the gold standard AOP use case). Its a bit coarse but the general approach
is to annotate methods that represent some auditable action. Combined with something
like Spring Security, we can get a pretty good idea of:
o who the user is
o what method they're invoking
o what data they're providing
o what time the method was invoked
o whether the invocation was successful or not (i.e., if an exception was thrown)
What can be build in Spring
• Airline reservation system
• Inventory Management
• Banking / Finance Systems
• Social Network
• Security Models
• Mobile clients
• Messaging Systems
Cross
cutting
concerns
Applications are generally developed with
multiple layers. A typical Java application has :
 Web Layer - Exposing the services to outside world
using REST or a web application
 Business Layer - Business Logic
 Data Layer - Persistence Logic
While the responsibilities of each of these layers
are different, there are a few common aspects
that apply to all layers
 Logging
 Security
Advice,
Aspect,
Pointcut,
Joint Point,
Weaving
• Advice – Indicate the action to take either
before or after the method execution.
• Pointcut – Indicate which method should be
intercept, by method name or regular
expression pattern.
• Advisor – Group ‘Advice’ and ‘Pointcut’ into a
single unit, and pass it to a proxy factory
object.
Advice ,
aspect,
pointcut,
jointpoint
Spring aop
Spring AOP AspectJ
Implemented in pure Java
Implemented using extensions of Java
programming language
No need for separate compilation process Needs AspectJ compiler (ajc) unless LTW is set up
Only runtime weaving is available
Runtime weaving is not available. Supports
compile-time, post-compile, and load-time
Weaving
Less Powerful – only supports method level weaving
More Powerful – can weave fields, methods,
constructors, static initializers, final class/methods,
etc…
Can only be implemented on beans managed by Spring
container
Can be implemented on all domain objects
Supports only method execution pointcuts Support all pointcuts
Proxies are created of targeted objects, and aspects are
applied on these proxies
Aspects are weaved directly into code before
application is executed (before runtime)
Much slower than AspectJ Better Performance
Easy to learn and apply Comparatively more complicated than Spring AOP
Difference between Spring AOP and AspectJ
Pointcut
Expression
Examples
• The execution of any public method
• execution(public * * (…))
• The Execution of any method begin with set
• execution(* set*(…))
• The Execution of any method defined by
Service Interface
• execution(*
com.dbschenker.spring.AopService.*(…))
• The Execution of any method defined in Dao
Package
• execution(* com.dbschenker.spring.dao.*.*(…))
AOP
Advice
Types
• Before Advice: These advices runs before the execution of join point
methods. We can use @Before annotation to mark an advice type as
Before advice.
• After (finally) Advice: An advice that gets executed after the join point
method finishes executing, whether normally or by throwing an
exception. We can create after advice using @After annotation.
• After Returning Advice: Sometimes we want advice methods to execute
only if the join point method executes normally. We can use
@AfterReturning annotation to mark a method as after returning advice.
• After Throwing Advice: This advice gets executed only when join point
method throws exception, we can use it to rollback the transaction
declaratively. We use @AfterThrowing annotation for this type of advice.
• Around Advice: This is the most important and powerful advice. This
advice surrounds the join point method and we can also choose whether
to execute the join point method or not. We can write advice code that
gets executed before and after the execution of the join point method. It
is the responsibility of around advice to invoke the join point method and
return values if the method is returning something. We use @Around
annotation to create around advice methods.
Advice Hierarchy
codes / examples
REFERNCES
• https://www.Springsource.org
• https://Start.spring.io
• https://docs.spring.io/spring/docs/3.2.x/spring-framework-
reference/html/mvc.html
• http://www.baeldung.com/spring-aop
• Downloads: http://www.eclipse.org/aspectj/downloads.php

More Related Content

PPTX
Spring framework AOP
PPTX
Automated Fault Tolerance Testing
PPTX
Training material exceptions v1
PPTX
.NET Fest 2019. Arnon Axelrod. Test automation for developers
PPTX
Emerging Trends of Test automation - Talk in Software Testing Conference Nort...
PPTX
Generating unit tests based on user logs
PDF
Test automation_strategy_for_legacysystems
PDF
Mock Objects, Design and Dependency Inversion Principle
Spring framework AOP
Automated Fault Tolerance Testing
Training material exceptions v1
.NET Fest 2019. Arnon Axelrod. Test automation for developers
Emerging Trends of Test automation - Talk in Software Testing Conference Nort...
Generating unit tests based on user logs
Test automation_strategy_for_legacysystems
Mock Objects, Design and Dependency Inversion Principle

Similar to Spring aop (20)

PPTX
PPSX
Spring - Part 3 - AOP
PPTX
Session 45 - Spring - Part 3 - AOP
PDF
AOP (Aspect-Oriented Programming) spring boot
PDF
Jose Luis Soria - XP2014 - Designing a Release Pipeline
PPTX
Aspect j introduction for non-programmers
PPTX
Spring 1 day program
PPTX
Spring aop concepts
PPT
代码大全(内训)
PPTX
Introduction to Aspect Oriented Programming
PPTX
Introduction to Spring
PPT
Spring AOP
PPTX
Spring AOP in Nutshell
PPTX
Aspect Oriented Programming: Hidden Toolkit That You Already Have
PDF
Integration strategies best practices- Mulesoft meetup April 2018
PPT
Java applets
PPTX
spring aop
PPTX
Spring framework part 2
PPTX
OutSystems Tips and Tricks
PPTX
Application engine
Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
AOP (Aspect-Oriented Programming) spring boot
Jose Luis Soria - XP2014 - Designing a Release Pipeline
Aspect j introduction for non-programmers
Spring 1 day program
Spring aop concepts
代码大全(内训)
Introduction to Aspect Oriented Programming
Introduction to Spring
Spring AOP
Spring AOP in Nutshell
Aspect Oriented Programming: Hidden Toolkit That You Already Have
Integration strategies best practices- Mulesoft meetup April 2018
Java applets
spring aop
Spring framework part 2
OutSystems Tips and Tricks
Application engine
Ad

More from sanskriti agarwal (8)

PPTX
PPTX
Future plans ways to recover
DOCX
COVID -19 PRESENT & FUTURE
PPTX
Node js Global Packages
PPTX
Node js Introduction
PPTX
Introduction to Salesforce Platform - Basic
PPTX
Lightning Process Builder
PPTX
Lightning Process Builder
Future plans ways to recover
COVID -19 PRESENT & FUTURE
Node js Global Packages
Node js Introduction
Introduction to Salesforce Platform - Basic
Lightning Process Builder
Lightning Process Builder
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Hybrid model detection and classification of lung cancer
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
DP Operators-handbook-extract for the Mautical Institute
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
project resource management chapter-09.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Zenith AI: Advanced Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
Web App vs Mobile App What Should You Build First.pdf
Hybrid model detection and classification of lung cancer
Group 1 Presentation -Planning and Decision Making .pptx
DP Operators-handbook-extract for the Mautical Institute
SOPHOS-XG Firewall Administrator PPT.pptx
Getting Started with Data Integration: FME Form 101
WOOl fibre morphology and structure.pdf for textiles
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Digital-Transformation-Roadmap-for-Companies.pptx
project resource management chapter-09.pdf
1. Introduction to Computer Programming.pptx
1 - Historical Antecedents, Social Consideration.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A comparative analysis of optical character recognition models for extracting...
Zenith AI: Advanced Artificial Intelligence

Spring aop

  • 2. Spring AOP Features • What are cross cutting concerns? • How do you implement cross cutting concerns in an application? • If you would want to log every request to a web application, what are the options you can think of? • If you would want to track performance of every request, what options can you think of? • What is an Advice, Aspect, Pointcut and Joint Point, Weaving ? • What are the different types of AOP Advices Types? • How do you implement AOP with Spring Boot? • What are the AOP Best Practices?
  • 3. Use Case : Business Problems • Read/write locks. Instead of replicating the same snippet, I used an aspect to define the methods that needed a read lock or an exclusive lock. • Exception Handling: don't need to repeat the horrible list of try ... catch, catch, catch etc - also means the exception handling is guaranteed to be consistent. • Can use it to track events in the system, whenever some object is CRUD you can send an event to your event system. • Can use it for sending web/ios/email notifications before /after any method call. • Audit logging (the gold standard AOP use case). Its a bit coarse but the general approach is to annotate methods that represent some auditable action. Combined with something like Spring Security, we can get a pretty good idea of: o who the user is o what method they're invoking o what data they're providing o what time the method was invoked o whether the invocation was successful or not (i.e., if an exception was thrown)
  • 4. What can be build in Spring • Airline reservation system • Inventory Management • Banking / Finance Systems • Social Network • Security Models • Mobile clients • Messaging Systems
  • 5. Cross cutting concerns Applications are generally developed with multiple layers. A typical Java application has :  Web Layer - Exposing the services to outside world using REST or a web application  Business Layer - Business Logic  Data Layer - Persistence Logic While the responsibilities of each of these layers are different, there are a few common aspects that apply to all layers  Logging  Security
  • 6. Advice, Aspect, Pointcut, Joint Point, Weaving • Advice – Indicate the action to take either before or after the method execution. • Pointcut – Indicate which method should be intercept, by method name or regular expression pattern. • Advisor – Group ‘Advice’ and ‘Pointcut’ into a single unit, and pass it to a proxy factory object.
  • 9. Spring AOP AspectJ Implemented in pure Java Implemented using extensions of Java programming language No need for separate compilation process Needs AspectJ compiler (ajc) unless LTW is set up Only runtime weaving is available Runtime weaving is not available. Supports compile-time, post-compile, and load-time Weaving Less Powerful – only supports method level weaving More Powerful – can weave fields, methods, constructors, static initializers, final class/methods, etc… Can only be implemented on beans managed by Spring container Can be implemented on all domain objects Supports only method execution pointcuts Support all pointcuts Proxies are created of targeted objects, and aspects are applied on these proxies Aspects are weaved directly into code before application is executed (before runtime) Much slower than AspectJ Better Performance Easy to learn and apply Comparatively more complicated than Spring AOP Difference between Spring AOP and AspectJ
  • 10. Pointcut Expression Examples • The execution of any public method • execution(public * * (…)) • The Execution of any method begin with set • execution(* set*(…)) • The Execution of any method defined by Service Interface • execution(* com.dbschenker.spring.AopService.*(…)) • The Execution of any method defined in Dao Package • execution(* com.dbschenker.spring.dao.*.*(…))
  • 11. AOP Advice Types • Before Advice: These advices runs before the execution of join point methods. We can use @Before annotation to mark an advice type as Before advice. • After (finally) Advice: An advice that gets executed after the join point method finishes executing, whether normally or by throwing an exception. We can create after advice using @After annotation. • After Returning Advice: Sometimes we want advice methods to execute only if the join point method executes normally. We can use @AfterReturning annotation to mark a method as after returning advice. • After Throwing Advice: This advice gets executed only when join point method throws exception, we can use it to rollback the transaction declaratively. We use @AfterThrowing annotation for this type of advice. • Around Advice: This is the most important and powerful advice. This advice surrounds the join point method and we can also choose whether to execute the join point method or not. We can write advice code that gets executed before and after the execution of the join point method. It is the responsibility of around advice to invoke the join point method and return values if the method is returning something. We use @Around annotation to create around advice methods.
  • 14. REFERNCES • https://www.Springsource.org • https://Start.spring.io • https://docs.spring.io/spring/docs/3.2.x/spring-framework- reference/html/mvc.html • http://www.baeldung.com/spring-aop • Downloads: http://www.eclipse.org/aspectj/downloads.php