SlideShare a Scribd company logo
Spring Framework
AOP
2st WEEK
30 Jun
GEEKY ACADEMY 2013
2 | GEEK ACADEMY 2013
INSTRUCTOR TEAM
SALAHPIYA
Salah (Dean) Chalermthai
Siam Chamnan Kit
SChalermthai@sprint3r.com
Piya (Tae) Lumyong
-
piya.tae@gmail.com
3 | GEEK ACADEMY 2013
Topic
‱ Intro to AOP
‱ Basic AOP Concepts
‱ Getting Started with Spring AOP
Introduction to AOP
GEEK ACADEMY 2013
5 | GEEK ACADEMY 2013
Intro to AOP
‱ Why worrying about Crosscutting Concerns?
‱ What AOP Does?
‱ Crosscutting Concerns
‱ Example
6 | GEEK ACADEMY 2013
Why worrying about Crosscutting Concerns?
public void withdraw(long accId, int amount) {
Account account = (Account)session.get(Account.class, accId);
double balance = account.getBalance();
balance = balance - amount;
account.setBalance(balance);
session.save(balance);
}
Business
7 | GEEK ACADEMY 2013
Why worrying about Crosscutting Concerns?
public void withdraw(long accId, int amount) {
if (hasPermission(accId)) {
Account account = (Account)session.get(Account.class, accId);
double balance = account.getBalance();
balance = balance - amount;
account.setBalance(balance);
session.save(balance);
} else {
throw new SecurityException("Access Denied");
}
}
Business
Permission
8 | GEEK ACADEMY 2013
Why worrying about Crosscutting Concerns?
public void withdraw(long accId, int amount) {
if (hasPermission(accId)) {
bankLogger.info(accId + " withdrow amount " + amount);
Account account = (Account)session.get(Account.class, accId);
double balance = account.getBalance();
balance = balance - amount;
account.setBalance(balance);
session.save(balance);
} else {
throw new SecurityException("Access Denied");
}
}
Business
Permission
Logger
9 | GEEK ACADEMY 2013
Why worrying about Crosscutting Concerns?
public void withdraw(long accId, int amount) {
if (hasPermission(accId)) {
bankLogger.info(accId + " withdrow amount " + amount);
Transaction tx = null;
try {
Transaction tx = session.beginTransaction();
Account account = (Account)session.get(Account.class, accId);
double balance = account.getBalance();
balance = balance - amount;
account.setBalance(balance);
session.save(balance);
tx.commit();
} catch(RuntimeException ex) {
if (tx!=null) tx.rollback;
}
} else {
throw new SecurityException("Access Denied");
}
}
Business
Permission
Logger
Transaction
10 | GEEK ACADEMY 2013
Why worrying about Crosscutting Concerns?
Business
Permission
Logger
Transaction
Service A
Service B
Service C
11 | GEEK ACADEMY 2013
Why worrying about Crosscutting Concerns?
‱ Too many relationship to the crosscutting objects
‱ Code is still required in all methods
‱ Cannot all be changed at one
12 | GEEK ACADEMY 2013
What AOP Does?
‱ AOP will separate the Crosscutting concerns from the
System
13 | GEEK ACADEMY 2013
Crosscutting Concerns
public void withdraw(int amount) {
balance = balance - amount;
}
public void deposit(int amount) {
balance = balance + amount;
}
Logger
bankLogger.info("Withdraw " + amount); bankLogger.info("Withdraw " + amount);
Transaction
tx.commit(); tx.commit();
tx.begin(); tx.begin();
Ref AOP Spring
14 | GEEK ACADEMY 2013
Cross-Cutting Concerns Examples
‱ Logging and Tracing
‱ Transaction Management
‱ Security
‱ Caching
‱ Error Handling
‱ Performance Monitoring
‱ Custom Business Rules
Basic AOP Concepts
GEEK ACADEMY 2013
16 | GEEK ACADEMY 2013
Basic AOP Concepts
‱ What is AOP?
‱ AOP Terminology
‱ Advice
‱ How Spring AOP Works
‱ AOP and OOP
‱ Leading AOP Technologies
17 | GEEK ACADEMY 2013
What is AOP?
‱ is a programming paradigm
‱ extends OOP
‱ enables modularization of crosscutting concerns
‱ is second heart of
Spring Framework
18 | GEEK ACADEMY 2013
AOP Terminology
19 | GEEK ACADEMY 2013
AOP Terminology
‱ Joinpoint – Defines A Point During the Execution of a
program. We can insert Additional logics at Joinpoints
‱ Advice – Action taken(Functionality) at a Joinpoint.
‱ Pointcut – Combination of Joinpoints where the Advice
need to be Applied.
20 | GEEK ACADEMY 2013
AOP Terminology
‱ Aspect – a modularization of a Crosscutting concern.
‱ Target object - object being advised by one or more aspects.
‱ AOP proxy – Will manage the way of Applying Aspects at
particular Pointcuts.
PointCut
Advice
PointCut
Advice
PointCut
Advice
Aspect
Target Object
Proxy
Call
21 | GEEK ACADEMY 2013
AOP Weaving
‱ Process of applying aspects to a target object
‱ Will create a proxied object
22 | GEEK ACADEMY 2013
How AOP Works
‱ Write your mainline application logic (Solve core problem)
‱ Write aspects to implement cross-cutting concerns (Write aspects)
‱ Bind it all together (Weave at runtime)
23 | GEEK ACADEMY 2013
AOP and OOP
‱ Aspect – code unit that encapsulates
pointcuts, advice, and attributes
‱ Pointcut – define the set of entry points
(triggers) in which advice is executed
‱ Advice – implementation of cross
cutting concern
‱ Weaver – construct code (source or
object) with advice
‱ Class – code unit that encapsulates
methods and attributes
‱ Method signature – define the entry
points for the execution of method bodies
‱ Method bodies – implementation of the
business logic concerns
‱ Compiler – convert source code to object
code
AOP OOP
24 | GEEK ACADEMY 2013
Leading AOP Technologies
‱ AspectJ
– 1995, original AOP technology
– Offers a full Aspect Oriented Programming language
– Modifies byte code to add aspects into your application!
‱ Spring AOP
– Uses dynamic proxies to add aspects into your application
– Only spring beans can be advised
– Uses some of AspectJ expression syntax
Getting Started with
Spring AOP
GEEK ACADEMY 2013
26 | GEEK ACADEMY 2013
Getting Started with Spring AOP
‱ Spring AOP
‱ Aspect
‱ Pointcut
‱ Advice
‱ Bean in Spring container
‱ How it really works
‱ Additional
‱ Spring AOP vs AspectJ
‱ @AspectJ vs XML
27 | GEEK ACADEMY 2013
Spring AOP
‱ Implemented in pure java
‱ No need for a special compilation process
‱ Supports only method execution join points
‱ Only runtime weaving is available
‱ AOP proxy
– JDK dynamic proxy
– CGLIB proxy
‱ Configuration
– @AspectJ annotation-style
– Spring XML configuration-style
28 | GEEK ACADEMY 2013
@Aspect
@Aspect
public class LoggingAspect {
...
}
<aop:aspectj-autoproxy />
<bean class="mypackage.LoggingAspect" />
29 | GEEK ACADEMY 2013
Pointcut
30 | GEEK ACADEMY 2013
Pointcut
‱ @Pointcut
‱ Pointcut expression
‱ Pointcut designators
‱ Example
31 | GEEK ACADEMY 2013
@Pointcut
@Aspect
public class LoggingAspect {
@Pointcut("execution(* mypackage.service..*.*(..))")
public void ifService() {}
@Pointcut("execution(* mypackage.repository..*.*(..))")
public void ifRepository() {}
...
}
32 | GEEK ACADEMY 2013
Pointcut expression
‱ PCD - Pointcut designators.
‱ method scope
– Advice will be applied to all the methods having this scope.
For e.g., public, private, etc. Spring AOP only supports
advising public methods.
‱ return type
– Advice will be applied to all the methods having this return
type.
PCD(<method scope> <return type> <declaring type>.<name(param)> <throw>)
33 | GEEK ACADEMY 2013
Pointcut expression(2)
‱ declaring type
– 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
‱ name(param)
– Filter the method names and the argument types.
Two dots(..) means any number(0-more) and type of parameters.
‱ throw
PCD(<method scope> <return type> <declaring type>.<name(param)> <throw>)
34 | GEEK ACADEMY 2013
Pointcut designators
‱ Code based
– execution (Method Patterns)
‱ the primary pointcut designator you will use when working with Spring AOP
– within (Type Patterns)
‱ within certain types (simply the execution of a method declared within a
matching type when using Spring AOP)
– target
‱ where the target object (application object being proxied) is an instance of
the given type
35 | GEEK ACADEMY 2013
Pointcut designators(2)
‱ Code based(2)
– this
‱ where the bean reference (Spring AOP proxy) is an instance of the given
type
– args
‱ where the arguments are instances of the given types
– bean (Bean Name Patterns)
‱ to a particular named Spring bean, or to a set of named Spring beans (when
using wildcards)
36 | GEEK ACADEMY 2013
Pointcut designators(3)
‱ Annotation based
– @annotation
‱ where the subject of the join point (method being executed in Spring AOP)
has the given annotation
– @within
‱ within types that have the given annotation (the execution of methods
declared in types with the given annotation when using Spring AOP)
37 | GEEK ACADEMY 2013
Pointcut designators(4)
‱ Annotation based(2)
– @target
‱ where the class of the executing object has an annotation of the given type
– @args
‱ where the runtime type of the actual arguments passed have annotations of
the given type(s)
38 | GEEK ACADEMY 2013
Pointcut example
‱ the execution of any public method:
execution(public * *(..))
‱ the execution of any method with a name beginning with “set”:
execution(* set*(..))
‱ the execution of any method defined by the MyService interface
execution(* com.xyz.service.MyService.*(..))
‱ the execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..))
‱ the execution of any method defined in the service package or a
sub-package:
execution(* com.xyz.service..*.*(..))
39 | GEEK ACADEMY 2013
Pointcut example(2)
‱ any join point (method execution only in Spring AOP) WITHIN the
service package:
within(com.xyz.service.*)
‱ any join point (method execution only in Spring AOP) within the
service package or a sub-package:
within(com.xyz.service..*)
‱ any join point (method execution only in Spring AOP) where the
proxy implements the AccountService interface:
this(com.xyz.service.AccountService)
40 | GEEK ACADEMY 2013
Pointcut example(3)
‱ Other example
– Pointcut example - Spring Framework Reference
41 | GEEK ACADEMY 2013
Advice
42 | GEEK ACADEMY 2013
Advice
‱ Advice types
‱ @Before
‱ @After
‱ @AfterReturning
‱ @AfterThrowing
‱ @Around
43 | GEEK ACADEMY 2013
Advice
‱ Action teken at particular Joinpoint is called as an Advice
‱ Defines What needs to be Applied and When
– What – Functionality
– When – Advice Type
44 | GEEK ACADEMY 2013
Advice types
‱ Before
‱ After
‱ After Returning
‱ After Throwing
‱ Around
Method
Return Throw
Success call
Call
45 | GEEK ACADEMY 2013
@Before
@Aspect
public class LoggingAspect {
@Pointcut("execution(* *(..))")
public void ifService() {}
@Before("ifService()")
public void logBefore(JoinPoint joinPoint) {
...
}
...
}
46 | GEEK ACADEMY 2013
@After
@Aspect
public class LoggingAspect {
@Pointcut("execution(* *(..))")
public void ifService() {}
@After("ifService()")
public void logAfter(JoinPoint joinPoint) {
...
}
...
}
47 | GEEK ACADEMY 2013
@AfterReturning
@Aspect
public class LoggingAspect {
@Pointcut("execution(* *(..))")
public void ifService() {}
@AfterReturning(pointcut = "ifService()", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
...
}
...
}
48 | GEEK ACADEMY 2013
@AfterThrowing
@Aspect
public class LoggingAspect {
@Pointcut("execution(* *(..))")
public void ifService() {}
@AfterThrowing(pointcut = "ifService()", throwing = "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
...
}
...
}
49 | GEEK ACADEMY 2013
@Around
@Aspect
public class LoggingAspect {
@Pointcut("execution(* *(..))")
public void ifService() {}
@Around("ifService()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
...
Object result = joinPoint.proceed();
...
return result;
}
...
}
50 | GEEK ACADEMY 2013
Security
Bean in Spring container
Logging
Caching
Target
Object
51 | GEEK ACADEMY 2013
How it really works (TX example)
52 | GEEK ACADEMY 2013
LAB1 Basic Spring AOP
53 | GEEK ACADEMY 2013
LAB1 Basic Spring AOP
Target
Object
Logging
54 | GEEK ACADEMY 2013
Links
‱ SpringSource
‱ Spring Reference 3.2
‱ Thank for
– Dmitry Noskov
– chamilavt
– Java Brains
55 | GEEK ACADEMY 2013
Books
GEEK ACADEMY 2013
THANK YOU FOR
YOUR TIME
GEEK ACADEMY 2013
Ad

More Related Content

What's hot (20)

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
HĂčng Nguyễn Huy
 
Spring Boot
Spring BootSpring Boot
Spring Boot
HongSeong Jeon
 
Spring core module
Spring core moduleSpring core module
Spring core module
Raj Tomar
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
Anuj Singh Rajput
 
Spring Boot
Spring BootSpring Boot
Spring Boot
koppenolski
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
AnushaNaidu
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
Lhouceine OUHAMZA
 
Spring boot
Spring bootSpring boot
Spring boot
Pradeep Shanmugam
 
Spring Core
Spring CoreSpring Core
Spring Core
Pushan Bhattacharya
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
nomykk
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
Vinay Kumar
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
Jonathan Holloway
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Dineesha Suraweera
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
NexThoughts Technologies
 
API Asynchrones en Java 8
API Asynchrones en Java 8API Asynchrones en Java 8
API Asynchrones en Java 8
José Paumard
 
Java Spring
Java SpringJava Spring
Java Spring
AathikaJava
 
Spring Boot and Microservices
Spring Boot and MicroservicesSpring Boot and Microservices
Spring Boot and Microservices
seges
 
Spring core module
Spring core moduleSpring core module
Spring core module
Raj Tomar
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
Anuj Singh Rajput
 
Spring Boot
Spring BootSpring Boot
Spring Boot
koppenolski
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
AnushaNaidu
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
nomykk
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
Vinay Kumar
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
Jonathan Holloway
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Dineesha Suraweera
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
 
API Asynchrones en Java 8
API Asynchrones en Java 8API Asynchrones en Java 8
API Asynchrones en Java 8
José Paumard
 
Java Spring
Java SpringJava Spring
Java Spring
AathikaJava
 
Spring Boot and Microservices
Spring Boot and MicroservicesSpring Boot and Microservices
Spring Boot and Microservices
seges
 

Similar to Spring framework aop (20)

Spring framework part 2
Spring framework  part 2Spring framework  part 2
Spring framework part 2
Skillwise Group
 
Spring aop
Spring aopSpring aop
Spring aop
UMA MAHESWARI
 
Introduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingIntroduction to Aspect Oriented Programming
Introduction to Aspect Oriented Programming
Amir Kost
 
Spring framework core
Spring framework coreSpring framework core
Spring framework core
Taemon Piya-Lumyong
 
2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx
pradeepwalter
 
AOP on Android
AOP on AndroidAOP on Android
AOP on Android
Tibor SrĂĄmek
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
Radhakrishna Mutthoju
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
PawanMM
 
Spring aop
Spring aopSpring aop
Spring aop
Hamid Ghorbani
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
RushiBShah
 
Angular
AngularAngular
Angular
Lilia Sfaxi
 
Spring - Part 3 - AOP
Spring - Part 3 - AOPSpring - Part 3 - AOP
Spring - Part 3 - AOP
Hitesh-Java
 
Aspect oriented programming with spring
Aspect oriented programming with springAspect oriented programming with spring
Aspect oriented programming with spring
Sreenivas Kappala
 
Utilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learningUtilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learning
Paris Data Engineers !
 
RPA_EC_2Cre_Chapter 03-Process Methodologies.pptx
RPA_EC_2Cre_Chapter 03-Process Methodologies.pptxRPA_EC_2Cre_Chapter 03-Process Methodologies.pptx
RPA_EC_2Cre_Chapter 03-Process Methodologies.pptx
huongdq21411
 
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
saber tabatabaee
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020
Joseph Kuo
 
Software_effort_estimation for Software engineering.pdf
Software_effort_estimation for Software engineering.pdfSoftware_effort_estimation for Software engineering.pdf
Software_effort_estimation for Software engineering.pdf
snehan789
 
SQL Optimizer vs Hive
SQL Optimizer vs Hive SQL Optimizer vs Hive
SQL Optimizer vs Hive
Vishaka Balasubramanian Sekar
 
Algorithmic Software Cost Modeling
Algorithmic Software Cost ModelingAlgorithmic Software Cost Modeling
Algorithmic Software Cost Modeling
Kasun Ranga Wijeweera
 
Spring framework part 2
Spring framework  part 2Spring framework  part 2
Spring framework part 2
Skillwise Group
 
Introduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingIntroduction to Aspect Oriented Programming
Introduction to Aspect Oriented Programming
Amir Kost
 
2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx
pradeepwalter
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOP
PawanMM
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
RushiBShah
 
Spring - Part 3 - AOP
Spring - Part 3 - AOPSpring - Part 3 - AOP
Spring - Part 3 - AOP
Hitesh-Java
 
Aspect oriented programming with spring
Aspect oriented programming with springAspect oriented programming with spring
Aspect oriented programming with spring
Sreenivas Kappala
 
Utilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learningUtilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learning
Paris Data Engineers !
 
RPA_EC_2Cre_Chapter 03-Process Methodologies.pptx
RPA_EC_2Cre_Chapter 03-Process Methodologies.pptxRPA_EC_2Cre_Chapter 03-Process Methodologies.pptx
RPA_EC_2Cre_Chapter 03-Process Methodologies.pptx
huongdq21411
 
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
saber tabatabaee
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020
Joseph Kuo
 
Software_effort_estimation for Software engineering.pdf
Software_effort_estimation for Software engineering.pdfSoftware_effort_estimation for Software engineering.pdf
Software_effort_estimation for Software engineering.pdf
snehan789
 
Algorithmic Software Cost Modeling
Algorithmic Software Cost ModelingAlgorithmic Software Cost Modeling
Algorithmic Software Cost Modeling
Kasun Ranga Wijeweera
 
Ad

Recently uploaded (20)

Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient CareAn Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
Cyntexa
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
Kit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜ž
Kit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜žKit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜ž
Kit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜ž
Wonjun Hwang
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšve
UiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšveUiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšve
UiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšve
UiPathCommunity
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Ć imek
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient CareAn Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
Cyntexa
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
Kit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜ž
Kit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜žKit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜ž
Kit-Works Team Study_아직도 Dockefile.pdf_êč€ì„±í˜ž
Wonjun Hwang
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšve
UiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšveUiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšve
UiPath Automation Suite – Cas d'usage d'une NGO internationale basĂ©e Ă  GenĂšve
UiPathCommunity
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Ć imek
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Ad

Spring framework aop

  • 1. Spring Framework AOP 2st WEEK 30 Jun GEEKY ACADEMY 2013
  • 2. 2 | GEEK ACADEMY 2013 INSTRUCTOR TEAM SALAHPIYA Salah (Dean) Chalermthai Siam Chamnan Kit SChalermthai@sprint3r.com Piya (Tae) Lumyong - piya.tae@gmail.com
  • 3. 3 | GEEK ACADEMY 2013 Topic ‱ Intro to AOP ‱ Basic AOP Concepts ‱ Getting Started with Spring AOP
  • 5. 5 | GEEK ACADEMY 2013 Intro to AOP ‱ Why worrying about Crosscutting Concerns? ‱ What AOP Does? ‱ Crosscutting Concerns ‱ Example
  • 6. 6 | GEEK ACADEMY 2013 Why worrying about Crosscutting Concerns? public void withdraw(long accId, int amount) { Account account = (Account)session.get(Account.class, accId); double balance = account.getBalance(); balance = balance - amount; account.setBalance(balance); session.save(balance); } Business
  • 7. 7 | GEEK ACADEMY 2013 Why worrying about Crosscutting Concerns? public void withdraw(long accId, int amount) { if (hasPermission(accId)) { Account account = (Account)session.get(Account.class, accId); double balance = account.getBalance(); balance = balance - amount; account.setBalance(balance); session.save(balance); } else { throw new SecurityException("Access Denied"); } } Business Permission
  • 8. 8 | GEEK ACADEMY 2013 Why worrying about Crosscutting Concerns? public void withdraw(long accId, int amount) { if (hasPermission(accId)) { bankLogger.info(accId + " withdrow amount " + amount); Account account = (Account)session.get(Account.class, accId); double balance = account.getBalance(); balance = balance - amount; account.setBalance(balance); session.save(balance); } else { throw new SecurityException("Access Denied"); } } Business Permission Logger
  • 9. 9 | GEEK ACADEMY 2013 Why worrying about Crosscutting Concerns? public void withdraw(long accId, int amount) { if (hasPermission(accId)) { bankLogger.info(accId + " withdrow amount " + amount); Transaction tx = null; try { Transaction tx = session.beginTransaction(); Account account = (Account)session.get(Account.class, accId); double balance = account.getBalance(); balance = balance - amount; account.setBalance(balance); session.save(balance); tx.commit(); } catch(RuntimeException ex) { if (tx!=null) tx.rollback; } } else { throw new SecurityException("Access Denied"); } } Business Permission Logger Transaction
  • 10. 10 | GEEK ACADEMY 2013 Why worrying about Crosscutting Concerns? Business Permission Logger Transaction Service A Service B Service C
  • 11. 11 | GEEK ACADEMY 2013 Why worrying about Crosscutting Concerns? ‱ Too many relationship to the crosscutting objects ‱ Code is still required in all methods ‱ Cannot all be changed at one
  • 12. 12 | GEEK ACADEMY 2013 What AOP Does? ‱ AOP will separate the Crosscutting concerns from the System
  • 13. 13 | GEEK ACADEMY 2013 Crosscutting Concerns public void withdraw(int amount) { balance = balance - amount; } public void deposit(int amount) { balance = balance + amount; } Logger bankLogger.info("Withdraw " + amount); bankLogger.info("Withdraw " + amount); Transaction tx.commit(); tx.commit(); tx.begin(); tx.begin(); Ref AOP Spring
  • 14. 14 | GEEK ACADEMY 2013 Cross-Cutting Concerns Examples ‱ Logging and Tracing ‱ Transaction Management ‱ Security ‱ Caching ‱ Error Handling ‱ Performance Monitoring ‱ Custom Business Rules
  • 15. Basic AOP Concepts GEEK ACADEMY 2013
  • 16. 16 | GEEK ACADEMY 2013 Basic AOP Concepts ‱ What is AOP? ‱ AOP Terminology ‱ Advice ‱ How Spring AOP Works ‱ AOP and OOP ‱ Leading AOP Technologies
  • 17. 17 | GEEK ACADEMY 2013 What is AOP? ‱ is a programming paradigm ‱ extends OOP ‱ enables modularization of crosscutting concerns ‱ is second heart of Spring Framework
  • 18. 18 | GEEK ACADEMY 2013 AOP Terminology
  • 19. 19 | GEEK ACADEMY 2013 AOP Terminology ‱ Joinpoint – Defines A Point During the Execution of a program. We can insert Additional logics at Joinpoints ‱ Advice – Action taken(Functionality) at a Joinpoint. ‱ Pointcut – Combination of Joinpoints where the Advice need to be Applied.
  • 20. 20 | GEEK ACADEMY 2013 AOP Terminology ‱ Aspect – a modularization of a Crosscutting concern. ‱ Target object - object being advised by one or more aspects. ‱ AOP proxy – Will manage the way of Applying Aspects at particular Pointcuts. PointCut Advice PointCut Advice PointCut Advice Aspect Target Object Proxy Call
  • 21. 21 | GEEK ACADEMY 2013 AOP Weaving ‱ Process of applying aspects to a target object ‱ Will create a proxied object
  • 22. 22 | GEEK ACADEMY 2013 How AOP Works ‱ Write your mainline application logic (Solve core problem) ‱ Write aspects to implement cross-cutting concerns (Write aspects) ‱ Bind it all together (Weave at runtime)
  • 23. 23 | GEEK ACADEMY 2013 AOP and OOP ‱ Aspect – code unit that encapsulates pointcuts, advice, and attributes ‱ Pointcut – define the set of entry points (triggers) in which advice is executed ‱ Advice – implementation of cross cutting concern ‱ Weaver – construct code (source or object) with advice ‱ Class – code unit that encapsulates methods and attributes ‱ Method signature – define the entry points for the execution of method bodies ‱ Method bodies – implementation of the business logic concerns ‱ Compiler – convert source code to object code AOP OOP
  • 24. 24 | GEEK ACADEMY 2013 Leading AOP Technologies ‱ AspectJ – 1995, original AOP technology – Offers a full Aspect Oriented Programming language – Modifies byte code to add aspects into your application! ‱ Spring AOP – Uses dynamic proxies to add aspects into your application – Only spring beans can be advised – Uses some of AspectJ expression syntax
  • 25. Getting Started with Spring AOP GEEK ACADEMY 2013
  • 26. 26 | GEEK ACADEMY 2013 Getting Started with Spring AOP ‱ Spring AOP ‱ Aspect ‱ Pointcut ‱ Advice ‱ Bean in Spring container ‱ How it really works ‱ Additional ‱ Spring AOP vs AspectJ ‱ @AspectJ vs XML
  • 27. 27 | GEEK ACADEMY 2013 Spring AOP ‱ Implemented in pure java ‱ No need for a special compilation process ‱ Supports only method execution join points ‱ Only runtime weaving is available ‱ AOP proxy – JDK dynamic proxy – CGLIB proxy ‱ Configuration – @AspectJ annotation-style – Spring XML configuration-style
  • 28. 28 | GEEK ACADEMY 2013 @Aspect @Aspect public class LoggingAspect { ... } <aop:aspectj-autoproxy /> <bean class="mypackage.LoggingAspect" />
  • 29. 29 | GEEK ACADEMY 2013 Pointcut
  • 30. 30 | GEEK ACADEMY 2013 Pointcut ‱ @Pointcut ‱ Pointcut expression ‱ Pointcut designators ‱ Example
  • 31. 31 | GEEK ACADEMY 2013 @Pointcut @Aspect public class LoggingAspect { @Pointcut("execution(* mypackage.service..*.*(..))") public void ifService() {} @Pointcut("execution(* mypackage.repository..*.*(..))") public void ifRepository() {} ... }
  • 32. 32 | GEEK ACADEMY 2013 Pointcut expression ‱ PCD - Pointcut designators. ‱ method scope – Advice will be applied to all the methods having this scope. For e.g., public, private, etc. Spring AOP only supports advising public methods. ‱ return type – Advice will be applied to all the methods having this return type. PCD(<method scope> <return type> <declaring type>.<name(param)> <throw>)
  • 33. 33 | GEEK ACADEMY 2013 Pointcut expression(2) ‱ declaring type – 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 ‱ name(param) – Filter the method names and the argument types. Two dots(..) means any number(0-more) and type of parameters. ‱ throw PCD(<method scope> <return type> <declaring type>.<name(param)> <throw>)
  • 34. 34 | GEEK ACADEMY 2013 Pointcut designators ‱ Code based – execution (Method Patterns) ‱ the primary pointcut designator you will use when working with Spring AOP – within (Type Patterns) ‱ within certain types (simply the execution of a method declared within a matching type when using Spring AOP) – target ‱ where the target object (application object being proxied) is an instance of the given type
  • 35. 35 | GEEK ACADEMY 2013 Pointcut designators(2) ‱ Code based(2) – this ‱ where the bean reference (Spring AOP proxy) is an instance of the given type – args ‱ where the arguments are instances of the given types – bean (Bean Name Patterns) ‱ to a particular named Spring bean, or to a set of named Spring beans (when using wildcards)
  • 36. 36 | GEEK ACADEMY 2013 Pointcut designators(3) ‱ Annotation based – @annotation ‱ where the subject of the join point (method being executed in Spring AOP) has the given annotation – @within ‱ within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP)
  • 37. 37 | GEEK ACADEMY 2013 Pointcut designators(4) ‱ Annotation based(2) – @target ‱ where the class of the executing object has an annotation of the given type – @args ‱ where the runtime type of the actual arguments passed have annotations of the given type(s)
  • 38. 38 | GEEK ACADEMY 2013 Pointcut example ‱ the execution of any public method: execution(public * *(..)) ‱ the execution of any method with a name beginning with “set”: execution(* set*(..)) ‱ the execution of any method defined by the MyService interface execution(* com.xyz.service.MyService.*(..)) ‱ the execution of any method defined in the service package: execution(* com.xyz.service.*.*(..)) ‱ the execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))
  • 39. 39 | GEEK ACADEMY 2013 Pointcut example(2) ‱ any join point (method execution only in Spring AOP) WITHIN the service package: within(com.xyz.service.*) ‱ any join point (method execution only in Spring AOP) within the service package or a sub-package: within(com.xyz.service..*) ‱ any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface: this(com.xyz.service.AccountService)
  • 40. 40 | GEEK ACADEMY 2013 Pointcut example(3) ‱ Other example – Pointcut example - Spring Framework Reference
  • 41. 41 | GEEK ACADEMY 2013 Advice
  • 42. 42 | GEEK ACADEMY 2013 Advice ‱ Advice types ‱ @Before ‱ @After ‱ @AfterReturning ‱ @AfterThrowing ‱ @Around
  • 43. 43 | GEEK ACADEMY 2013 Advice ‱ Action teken at particular Joinpoint is called as an Advice ‱ Defines What needs to be Applied and When – What – Functionality – When – Advice Type
  • 44. 44 | GEEK ACADEMY 2013 Advice types ‱ Before ‱ After ‱ After Returning ‱ After Throwing ‱ Around Method Return Throw Success call Call
  • 45. 45 | GEEK ACADEMY 2013 @Before @Aspect public class LoggingAspect { @Pointcut("execution(* *(..))") public void ifService() {} @Before("ifService()") public void logBefore(JoinPoint joinPoint) { ... } ... }
  • 46. 46 | GEEK ACADEMY 2013 @After @Aspect public class LoggingAspect { @Pointcut("execution(* *(..))") public void ifService() {} @After("ifService()") public void logAfter(JoinPoint joinPoint) { ... } ... }
  • 47. 47 | GEEK ACADEMY 2013 @AfterReturning @Aspect public class LoggingAspect { @Pointcut("execution(* *(..))") public void ifService() {} @AfterReturning(pointcut = "ifService()", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { ... } ... }
  • 48. 48 | GEEK ACADEMY 2013 @AfterThrowing @Aspect public class LoggingAspect { @Pointcut("execution(* *(..))") public void ifService() {} @AfterThrowing(pointcut = "ifService()", throwing = "error") public void logAfterThrowing(JoinPoint joinPoint, Throwable error) { ... } ... }
  • 49. 49 | GEEK ACADEMY 2013 @Around @Aspect public class LoggingAspect { @Pointcut("execution(* *(..))") public void ifService() {} @Around("ifService()") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { ... Object result = joinPoint.proceed(); ... return result; } ... }
  • 50. 50 | GEEK ACADEMY 2013 Security Bean in Spring container Logging Caching Target Object
  • 51. 51 | GEEK ACADEMY 2013 How it really works (TX example)
  • 52. 52 | GEEK ACADEMY 2013 LAB1 Basic Spring AOP
  • 53. 53 | GEEK ACADEMY 2013 LAB1 Basic Spring AOP Target Object Logging
  • 54. 54 | GEEK ACADEMY 2013 Links ‱ SpringSource ‱ Spring Reference 3.2 ‱ Thank for – Dmitry Noskov – chamilavt – Java Brains
  • 55. 55 | GEEK ACADEMY 2013 Books
  • 57. THANK YOU FOR YOUR TIME GEEK ACADEMY 2013
  çż»èŻ‘ïŒš