AOP - Spring  (Aspect Oriented Programming with Spring) Technical Knowledge Sharing  Session 2
Common Functionalities used across the application modules Ex: Transaction management Logging Security Crosscutting Concerns
public void withdraw(int amount){ balance = this.balance – amount;  accountDao.saveBalance(balance); } bankLogger.info(“Withdraw - ” + amount); txn.begin(); txn.commit(); public void deposit(int amount){ balance = this.balance + amount;  accountDao.saveBalance(balance); } bankLogger.info(“Deposit - ” + amount); txn.begin(); txn.commit(); Crosscutting Concerns Eg: Banking Application Transaction Management Logging
public void withdraw(int amount){ balance = this.balance – amount;  accountDao.saveBalance(balance); } public void deposit(int amount){ balance = this.balance + amount;  accountDao.saveBalance(balance); } Coupling between Core & Crosscutting Concerns Duplicate Code Hard to maintain Code Why Worrying about Crosscutting Concerns? Logging bankLogger.info(“Withdraw - ” + amount); bankLogger.info(“Deposit - ” + amount); Transaction Management txn.begin(); txn.commit(); txn.begin(); txn.commit();
Transaction Management Logging public void withdraw(int amount){ balance = this.balance – amount;  accountDao.saveBalance(balance); } public void deposit(int amount){ balance = this.balance + amount;  accountDao.saveBalance(balance); } Any solution for Crosscutting Concerns?  You can modularize   Crosscutting Concerns   Using Spring – AOP txn.begin(); txn.commit(); txn.begin(); txn.commit(); bankLogger.info(“Withdraw - ” + amount); bankLogger.info(“Deposit - ” + amount);
AOP will separate the Crosscutting concerns from the System What AOP Does?
withdraw deposit public void withdraw(int amount){ balance = this.balance – amount;  accountDao.saveBalance(balance); } public void deposit(int amount){ balance = this.balance + amount;  accountDao.saveBalance(balance); } Joinpoint Applying AOP Logging Transaction Management Advise
Joinpoint  – Defines A Point During the Execution of a program.   We can insert Additional logics at Joinpoints Advice   – Action taken at a Joinpoint. Pointcut   – Combination of Joinpoints where the Advice need to    be Applied. Aspect   – a modularization of a Crosscutting concern.  AOP Concepts
Target object  - object being advised by one or more aspects. AOP proxy  – Will manage the way of Applying Aspects at    particular Pointcuts. AOP Concepts Contd…
Action taken at a particular Joinpoint is called as an Advice Defines  What  needs to be Applied and  When What  – Functionality When  – Advice Type Eg: Applying Logging Advice withdraw Advice
Before advice  After returning advice  After advice Around advice After throwing advice Method Method Method Method Method Exception Advice Types
Process of Applying Aspects to a Target Object. Will Create a proxied Object. AOP Weaving Class Bank{ public void withdraw(int amnt); } Proxy Target Caller
public class  CustomerImpl   implements  Customer { public void  browse (){ System.out.println(“ Browsing Internet ”); } } public class  CafeOwner { public void  loginTime (){ System.out.println(“ Login Time ”); } public void  logoutTime (){ System.out.println(“ Logout Time ”); } public void  issueBill (){ System.out.println(“ Calculate and issue  the usage Bill ”); } } An Internet café owner wants a system to Issue an Usage bill and track the Login and Logout times at the end of Browsing Internet by  A Customer. Example – Use of AOP with Spring
Output: Login Time Browsing Internet... Logout Time Example – Use of AOP with Spring Browse CafeOwner.loginTime () CafeOwner.logoutTime () Before Advice After Advice Pointcut
Reference  Spring Documentation :  Chapter 6. Aspect Oriented Programming with Spring Spring In Action – Manning  (Updated for Spring 3.0)   Video Tutorial –  Spring AOP Tutorial
Thank you

Aop spring

  • 1.
    AOP - Spring (Aspect Oriented Programming with Spring) Technical Knowledge Sharing Session 2
  • 2.
    Common Functionalities usedacross the application modules Ex: Transaction management Logging Security Crosscutting Concerns
  • 3.
    public void withdraw(intamount){ balance = this.balance – amount; accountDao.saveBalance(balance); } bankLogger.info(“Withdraw - ” + amount); txn.begin(); txn.commit(); public void deposit(int amount){ balance = this.balance + amount; accountDao.saveBalance(balance); } bankLogger.info(“Deposit - ” + amount); txn.begin(); txn.commit(); Crosscutting Concerns Eg: Banking Application Transaction Management Logging
  • 4.
    public void withdraw(intamount){ balance = this.balance – amount; accountDao.saveBalance(balance); } public void deposit(int amount){ balance = this.balance + amount; accountDao.saveBalance(balance); } Coupling between Core & Crosscutting Concerns Duplicate Code Hard to maintain Code Why Worrying about Crosscutting Concerns? Logging bankLogger.info(“Withdraw - ” + amount); bankLogger.info(“Deposit - ” + amount); Transaction Management txn.begin(); txn.commit(); txn.begin(); txn.commit();
  • 5.
    Transaction Management Loggingpublic void withdraw(int amount){ balance = this.balance – amount; accountDao.saveBalance(balance); } public void deposit(int amount){ balance = this.balance + amount; accountDao.saveBalance(balance); } Any solution for Crosscutting Concerns? You can modularize Crosscutting Concerns Using Spring – AOP txn.begin(); txn.commit(); txn.begin(); txn.commit(); bankLogger.info(“Withdraw - ” + amount); bankLogger.info(“Deposit - ” + amount);
  • 6.
    AOP will separatethe Crosscutting concerns from the System What AOP Does?
  • 7.
    withdraw deposit publicvoid withdraw(int amount){ balance = this.balance – amount; accountDao.saveBalance(balance); } public void deposit(int amount){ balance = this.balance + amount; accountDao.saveBalance(balance); } Joinpoint Applying AOP Logging Transaction Management Advise
  • 8.
    Joinpoint –Defines A Point During the Execution of a program. We can insert Additional logics at Joinpoints Advice – Action taken at a Joinpoint. Pointcut – Combination of Joinpoints where the Advice need to be Applied. Aspect – a modularization of a Crosscutting concern. AOP Concepts
  • 9.
    Target object - object being advised by one or more aspects. AOP proxy – Will manage the way of Applying Aspects at particular Pointcuts. AOP Concepts Contd…
  • 10.
    Action taken ata particular Joinpoint is called as an Advice Defines What needs to be Applied and When What – Functionality When – Advice Type Eg: Applying Logging Advice withdraw Advice
  • 11.
    Before advice After returning advice After advice Around advice After throwing advice Method Method Method Method Method Exception Advice Types
  • 12.
    Process of ApplyingAspects to a Target Object. Will Create a proxied Object. AOP Weaving Class Bank{ public void withdraw(int amnt); } Proxy Target Caller
  • 13.
    public class CustomerImpl implements Customer { public void browse (){ System.out.println(“ Browsing Internet ”); } } public class CafeOwner { public void loginTime (){ System.out.println(“ Login Time ”); } public void logoutTime (){ System.out.println(“ Logout Time ”); } public void issueBill (){ System.out.println(“ Calculate and issue the usage Bill ”); } } An Internet café owner wants a system to Issue an Usage bill and track the Login and Logout times at the end of Browsing Internet by A Customer. Example – Use of AOP with Spring
  • 14.
    Output: Login TimeBrowsing Internet... Logout Time Example – Use of AOP with Spring Browse CafeOwner.loginTime () CafeOwner.logoutTime () Before Advice After Advice Pointcut
  • 15.
    Reference SpringDocumentation : Chapter 6. Aspect Oriented Programming with Spring Spring In Action – Manning (Updated for Spring 3.0) Video Tutorial – Spring AOP Tutorial
  • 16.