Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aspect Oriented Programming

Similar presentations


Presentation on theme: "Aspect Oriented Programming"— Presentation transcript:

1 Aspect Oriented Programming
By Rohit Ghatol & Loukik Purohit

2 What is Aspect Oriented Programming?

3 Core Business Logic

4 Concerns Pooling Tracing Logging Caching Transactions Contract
Profiling

5 Problem Statement

6 Shopping Cart Operator
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 DAO Shopping Cart DAO Inventory Service Shopping Service Shopping Cart Operator Logging Security Transactions Caching Pooling

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

10 AspectJ

11 Problem -1 Tracing/Logging

12 New Project New Person Logging/Tracing Debugging Flow

13 Shopping Cart Operator
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

16 Problem -3 Caching

17 Shopping Cart Operator
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

18 Problem -4 Connection Pooling

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

20 Problem -5 Contracts

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

22 Problem -6 Adding to existing Classes

23 Extending without changing code
boolean equals(); Int hashcode(); Item String getID(); String getName(); String getDescription(); Item clone(); String toString(); Referred as “Introduction” or ITD

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

25 HelloWorld AspectJ Code Example

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

27 package com.test.aspects;
/** 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

28 ackage com.test.aspects;
/** 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

29 Output of Program **** --> Hello World! ****

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

31 Different Types of Aspect
Dynamic Cross Cutting Static Cross Cutting

32 Dynamic Cross Cutting Join Points Point Cut Advice Join Points

33 Flow Example InventoryService InventoryDAO JoinPoint
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 } InventoryDAO Item getItem(long id) PointCut :execution(InventoryDAO.getItem(..)) PointCut :execution(*DAO.getItem(..))

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

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

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

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

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

39 Springs Advice Before Around After Returning After Throwing Method
Successful Return Exception Before Around After Returning After Throwing

40 AspectJ Syntax

41 public aspect ExampleAspect {
Map<Long, Object> userIdMap = new HashMap<Long, Object>(); //Caching Point Cut pointcut daoGetters(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"); return userIdMap.get(user.getUserId()); } else { Object object = proceed(user); System.out.println(" #Caching: Got ShoppingCart from Database - Expensive"); userIdMap.put(user.getUserId(), object); return object; } //…. Turn to next slide

42 public aspect ExampleAspect {
…… //ITD / Introduction public String ShopItem.toString(){ return "ShopItem [itemId=" + getItemId() + ", itemName=" + getItemName() + ", itemDescription=" + getItemDescription() + "]"; } //Compiler Warnign and Errors declare warning:get(* System.out):"Do not use System.out.println"; declare warning : (call(* com.test..*Dao.*(..)) && !within(com.test..*Service)): "DAOs should only be called from Service layer";

43 Logging, Tracing & Profiling
Code Example

44 Pooling and Caching Code Example

45 Spring AOP

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

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

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

49 Springs Advice Before Around After Returning After Throwing Method
Successful Return Exception Before Around After Returning After Throwing

50 Introduction

51 Modelling Domain Spring Context Bean Instantiate Configure

52 Hibern-ate Modelling Domain Spring Context Bean Instantiates
Configures Bean

53 <aop:spring-configured/>
Spring + AspectJ <aop:spring-configured/>

54 Conclusion AspectJ Spring AOP
More Powerful – methods, fields, constructor, aspect, field weaving Compile time weaving – works with likes of Android Learning curve is more Much better in speed Needs special compiler Less Powerful – only supports method level weaving Runtime weaving strictly requires Spring Easier to learn Much slower than AspectJ No such need


Download ppt "Aspect Oriented Programming"

Similar presentations


Ads by Google