1 / 54

Aspect Oriented Programming

Aspect Oriented Programming. By Rohit Ghatol & Loukik Purohit. What is Aspect Oriented Programming?. Core Business Logic. Pooling. Tracing. Logging. Concerns. Caching. Transactions. Contract. Profiling. Problem Statement. Layered Architecture. Inventory.jsp. Inventory Service.

harsha
Download Presentation

Aspect Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Aspect Oriented Programming By Rohit Ghatol & LoukikPurohit

  2. What is Aspect Oriented Programming?

  3. Core Business Logic

  4. Pooling Tracing Logging Concerns Caching Transactions Contract Profiling

  5. Problem Statement

  6. Layered Architecture Inventory.jsp Inventory Service Inventory DAO CartView.jsp Shopping Cart Operator Shopping Service Shopping Cart DAO View Service Database

  7. Non AOP Solution

  8. Cross Cutting Concerns Inventory Service Logging Shopping Cart Operator Security Shopping Service Transactions Inventory DAO Caching Shopping Cart DAO Pooling

  9. AOP Implementations AspectJ Spring AOP Works with runtime proxies which extend and wrap and delegate calls to original objects • Compile time changes the byte code of existing .class to inject new concerns in code

  10. AspectJ

  11. Problem -1 Tracing/Logging

  12. New Project New Person Logging/Tracing Debugging Flow

  13. Logging/Tracing Inventory.jsp Inventory Service Inventory DAO L L L CartView.jsp Shopping Cart Operator L L Shopping Service Shopping Cart DAO L View Service Database

  14. Problem -2 Profiling

  15. Problem -3 Caching

  16. Caching to avoid DB hit Inventory.jsp Inventory Service Inventory DAO Cache CartView.jsp Shopping Cart Operator Shopping Service Shopping Cart DAO Cache View Service Database

  17. Problem -4 Connection Pooling

  18. Connection Pooling Inventory.jsp Inventory Service Inventory DAO CartView.jsp Shopping Cart Operator 4 Shopping Service Shopping Cart DAO View Service 4 Database

  19. Problem -5 Contracts

  20. Use of System.out.println View calling DAO ……….

  21. Problem -6 Adding to existing Classes

  22. Extending without changing code Item String getID(); String getName(); String getDescription(); boolean equals(); Inthashcode(); Item clone(); String toString(); Referred as “Introduction” or ITD

  23. AspectJ using STS Download from http://www.springsource.com/developer/sts

  24. HelloWorldAspectJ Code Example

  25. package com.test; /** * @author rohit * */ public class Helloworld { public static void main(String args[]){ Helloworldhelloworld = new Helloworld(); helloworld.hello(); } public void hello(){ System.out.print("Hello"); } } HelloWorld.java

  26. package com.test.aspects; /** * @author rohit * */ public aspect WorldAspect { pointcut world() : call( public void com.test.Helloworld.hello()) ; before(): world(){ System.out.print("--> "); } after() : world() { System.out.print(" World!"); } } WorldAspect.java

  27. ackagecom.test.aspects; /** * @author rohit * */ public aspect Decorator { declare precedence: Decorator, WorldAspect; pointcut world() : call(* *.hello(..)) && !within(Decorator) && !within(WorldAspect) ; void around(): world(){ System.out.print("**** "); proceed(); System.out.print(" ****"); } } Decorator.java

  28. Output of Program **** --> Hello World! ****

  29. AspectJ Process Main Java Source .Classes Business Logic .Classes AspectJ Compiler Business Logic with Concerns now weaved in Aspects Source

  30. Different Types of Aspect Dynamic Cross Cutting Static Cross Cutting

  31. Dynamic Cross Cutting Join Points Point Cut Advice Join Points Join Points

  32. Flow Example JoinPoint InventoryService InventoryDAO Item getItem(long id) Item getItem(long id) Around Advice Psuedo Code If(item in cache){ return item; }else{ //fetch from db; proceed(); put item in cache; return item } PointCut :execution(InventoryDAO.getItem(..)) PointCut :execution(*DAO.getItem(..))

  33. Static Cross Cutting Introduction/ITD Employee Inject equals() & hashCode() private String firstName; private String lastName; private long birthDate //…getters & setters() Inject persist() & merge() Inject implements Serializable

  34. Static Cross Cutting Compile Type Declaration declare warning : get(* System.out): ”Use Logging Framework";

  35. RUN TIME WEAVING Proxy Target Caller Pointcut Advice 1 Advice 2 Pointcut Only Method Pointcuts

  36. Spring Proxy Generation Using java.lang.reflect.Proxy Class Using the CGLIB library

  37. Writing pointcuts Any Return type execution(* com.mypackage.myClass.method(..)) Takes any args Trigger on method execution Method Declaration

  38. Springs Advice Before Method Around After Returning Successful Return Exception After Throwing

  39. AspectJ Syntax

  40. public aspect ExampleAspect { Map<Long, Object> userIdMap = new HashMap<Long, Object>(); //Caching Point Cut pointcutdaoGetters(User user): execution(* com.test..*ShoppingCartDao.get*(..)) && args(user); Object around(User user) : daoGetters(user) { if (userIdMap.containsKey(user.getUserId())) { System.out.println(" #Caching: Got ShoppingCart from Cache - Cheap"); returnuserIdMap.get(user.getUserId()); } else { Object object = proceed(user); System.out.println(" #Caching: Got ShoppingCart from Database - Expensive"); userIdMap.put(user.getUserId(), object); returnobject; } } //…. Turn to next slide }

  41. public aspect ExampleAspect { …… //ITD / Introduction public StringShopItem.toString(){ return "ShopItem [itemId=" + getItemId() + ", itemName=" + getItemName() + ", itemDescription=" + getItemDescription() + "]"; } //Compiler Warnign and Errors declarewarning:get(* System.out):"Do not useSystem.out.println"; declarewarning : (call(* com.test..*Dao.*(..)) && !within(com.test..*Service)): "DAOsshouldonlybecalled from Service layer"; }

  42. Logging, Tracing & Profiling Code Example

  43. Pooling and Caching Code Example

  44. Spring AOP

  45. RUN TIME WEAVING Proxy Target Caller Pointcut Advice 1 Advice 2 Pointcut Only Method Pointcuts

  46. Spring Proxy Generation Using java.lang.reflect.Proxy Class Using the CGLIB library

  47. Writing pointcuts Any Return type execution(* com.mypackage.myClass.method(..)) Takes any args Trigger on method execution Method Declaration

  48. Springs Advice Before Method Around After Returning Successful Return Exception After Throwing

  49. Introduction

More Related