1 / 58

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.

normapierce
Télécharger la présentation

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. What is Weaving? Class B Class A1 Class A2 String getData() void method1() void method2()

  11. What is Weaving? Class B Class A1 Class A2 String getData() void method1() void method2()

  12. What is Weaving? Class B Class A1 Class A2 String getData() void method1() void method2() Proxy

  13. AspectJ

  14. Problem -1 Tracing/Logging

  15. New Project New Person Logging/Tracing Debugging Flow

  16. 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

  17. Problem -2 Profiling

  18. Problem -3 Caching

  19. 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

  20. Problem -4 Connection Pooling

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

  22. Problem -5 Contracts

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

  24. Problem -6 Adding to existing Classes

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

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

  27. HelloWorldAspectJ Code Example

  28. 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

  29. 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

  30. 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

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

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

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

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

  35. 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(..))

  36. Writing pointcuts accessor return type execution(* com.mypackage.myClass.method(..)) Takes any args execution/ call/ new/ handler Method Declaration Reference : http://www.eclipse.org/aspectj/doc/released/progguide/quick.html

  37. 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

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

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

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

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

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

  43. AspectJ Syntax

  44. 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 }

  45. 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"; }

  46. Logging, Tracing & Profiling Code Example

  47. Pooling and Caching Code Example

  48. Spring AOP

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

More Related