Spring AOP
Radhakrishna M
Agenda
• What is AOP?
• Why it is required?
• AOP concepts
• Types of Advices
• Configuration
• Advantages
• Conclusion
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.
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.
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(){
……………………..
……………………..
}
Before AOP
Timecard
Transaction
Security
Scheduling
Transaction
Security
Financial
Transaction
Security
Crosscutting Concerns
After AOP
Timecard SchedulingFinancial
Transaction Security
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.
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.
• 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.
AOP Key Idea
base/core program
Aspects
weave
Final system
Hierarchy of advice interfaces
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.
Configuration
1. Create a Aspect with the required logic:
<bean id="customerAspect" class="com.jcms.jms.CustomerAspect“/>
<aop:aspect ref="customerAspect">
2 . Declare pointcuts :
<aop:pointcut id="customerPoint" expression="execution(*
com.jcms.biz.AccountService.saveAccount(..)) " />
3. Declare advices:
<aop:after pointcut-ref="customerPoint" method="log" />
<aop:before pointcut-ref="customerPoint"
method="setChangeHistoryId"/>
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.
Conclusion
• Important to remember that AOP is not
replacing OOP, but it enhancing the features
of OOP with a new methodology.
Thank you

Spring AOP

  • 1.
  • 2.
    Agenda • What isAOP? • Why it is required? • AOP concepts • Types of Advices • Configuration • Advantages • Conclusion
  • 3.
    Aspect – Ageneral 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(){ …………………….. …………………….. }
  • 6.
  • 7.
    Aspects • In AOPcrosscutting 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.
  • 10.
    AOP Key Idea base/coreprogram Aspects weave Final system
  • 11.
  • 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.
  • 14.
    Configuration 1. Create aAspect with the required logic: <bean id="customerAspect" class="com.jcms.jms.CustomerAspect“/> <aop:aspect ref="customerAspect"> 2 . Declare pointcuts : <aop:pointcut id="customerPoint" expression="execution(* com.jcms.biz.AccountService.saveAccount(..)) " /> 3. Declare advices: <aop:after pointcut-ref="customerPoint" method="log" /> <aop:before pointcut-ref="customerPoint" method="setChangeHistoryId"/>
  • 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 toremember that AOP is not replacing OOP, but it enhancing the features of OOP with a new methodology.
  • 17.