540 likes | 727 Views
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.
E N D
Aspect Oriented Programming By Rohit Ghatol & LoukikPurohit
Pooling Tracing Logging Concerns Caching Transactions Contract Profiling
Layered Architecture Inventory.jsp Inventory Service Inventory DAO CartView.jsp Shopping Cart Operator Shopping Service Shopping Cart DAO View Service Database
Cross Cutting Concerns Inventory Service Logging Shopping Cart Operator Security Shopping Service Transactions Inventory DAO Caching Shopping Cart DAO Pooling
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
New Project New Person Logging/Tracing Debugging Flow
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
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
Connection Pooling Inventory.jsp Inventory Service Inventory DAO CartView.jsp Shopping Cart Operator 4 Shopping Service Shopping Cart DAO View Service 4 Database
Use of System.out.println View calling DAO ……….
Extending without changing code Item String getID(); String getName(); String getDescription(); boolean equals(); Inthashcode(); Item clone(); String toString(); Referred as “Introduction” or ITD
AspectJ using STS Download from http://www.springsource.com/developer/sts
HelloWorldAspectJ Code Example
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
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
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
Output of Program **** --> Hello World! ****
AspectJ Process Main Java Source .Classes Business Logic .Classes AspectJ Compiler Business Logic with Concerns now weaved in Aspects Source
Different Types of Aspect Dynamic Cross Cutting Static Cross Cutting
Dynamic Cross Cutting Join Points Point Cut Advice Join Points Join Points
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(..))
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
Static Cross Cutting Compile Type Declaration declare warning : get(* System.out): ”Use Logging Framework";
RUN TIME WEAVING Proxy Target Caller Pointcut Advice 1 Advice 2 Pointcut Only Method Pointcuts
Spring Proxy Generation Using java.lang.reflect.Proxy Class Using the CGLIB library
Writing pointcuts Any Return type execution(* com.mypackage.myClass.method(..)) Takes any args Trigger on method execution Method Declaration
Springs Advice Before Method Around After Returning Successful Return Exception After Throwing
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 }
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"; }
Logging, Tracing & Profiling Code Example
Pooling and Caching Code Example
RUN TIME WEAVING Proxy Target Caller Pointcut Advice 1 Advice 2 Pointcut Only Method Pointcuts
Spring Proxy Generation Using java.lang.reflect.Proxy Class Using the CGLIB library
Writing pointcuts Any Return type execution(* com.mypackage.myClass.method(..)) Takes any args Trigger on method execution Method Declaration
Springs Advice Before Method Around After Returning Successful Return Exception After Throwing