2. Agenda
• What is AOP?
• Why it is required?
• AOP concepts
• Types of Advices
• Configuration
• Advantages
• Conclusion
3. Aspect – A general feature that is applied globally
to an application (logging, performance monitoring,
exception handling, transaction management, etc).
Concern: AOP breaks the program logic into distinct
parts (called concerns).
cross-cutting concern : A cross-cutting concern is a
concern that can affect the whole application and
should be centralized in one location in code as
possible, such as transaction management,
authentication, logging, security etc.
4. Aspect oriented programming
• Aspect Oriented Programming (AOP) refers to
the programming paradigm which isolates
secondary or supporting functions from the
main program’s business logic.
• It provides the pluggable way to dynamically
add the additional concern before, after or
around the actual logic.
5. Need for AOP
Scenario: Maintain log and send notification after calling
methods that starts from m.
class A{
public void m1(){...}
public void m2(){...}
public void m3(){...}
public void n1(){...}
public void n2(){...}
public void p1(){...}
public void p2(){...}
}
Solution with AOP: We don't have to call methods from the
method. Now we can define the additional concern like
maintaining log, sending notification etc. in the method of a class.
Its entry is given in the xml file.
public logAndNotify(){
……………………..
……………………..
}
7. Aspects
• In AOP crosscutting concerns are implemented in
aspects instead of fusing them into core modules.
• Aspects are an additional unit of modularity.
• Aspects can be reused.
• By reducing code tangling it makes it easier to
understand what the core functionality of a module
is.
8. AOP concepts
• Joinpoint – Defines a point during the execution of a
program.
• Advice – Action taken at a Joinpoint.
Before Advice: It executes before a join point.
After Returning Advice: It executes after a joint point
completes normally.
After Throwing Advice: It executes if method exits by
throwing an exception.
After (finally) Advice: It executes after a join point
regardless of join point exit whether normally or
exceptional return.
Around Advice: It executes before and after a join point.
9. • Pointcut – Combination of joinpoints where the advice
need to be applied.
• Introduction – It means introduction of additional method
and fields for a type. It allows you to introduce new
interface to any advised object.
• Aspect – It is a class that contains advices, joinpoints etc.
• Interceptor – It is an aspect that contains only one advice.
• Weaving – It is the process of linking aspect with other
application types or objects to create an advised object.
Weaving can be done at compile time, load time or
runtime. Spring AOP performs weaving at runtime.
13. AOP vs OOP
AOP
1. Aspect - Code unit that
encapsulates pointcuts,
advice and attributes.
2. Pointcut – define the set of
entry points(triggers)
in which advice is executed.
3. Advice – Implementation of
cross cutting concern
4. Weaver – Construct
code(source /object) with
advice.
OOP
1. Class – Code unit that
encapsulates methods and
attributes
2. Method signature – Define
the entry points for the
execution of method bodies
3. Method bodies-
Implementation of the
business logic concerns
4. Compiler – Convert source
code to object code.
15. Advantages
Better separates processes (concerns) than
previous approaches.
Creates cleaner code.
Properly separates business requirements into
clean reusable code.
Code is reusable at a greater extent than OOP.
16. Conclusion
• Important to remember that AOP is not
replacing OOP, but it enhancing the features
of OOP with a new methodology.