Aspect Oriented Programming<br />By <br />Rohit Ghatol <br />& <br />LoukikPurohit<br />
What is Aspect Oriented Programming?<br />
Core Business Logic<br />
Concerns<br />Pooling<br />Tracing<br />Logging<br />Caching<br />Transactions<br />Contract<br />Profiling<br />
Problem Statement<br />
Layered Architecture<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />Shopping Cart Operator<br />CartVie...
Non AOP Solution<br />
Cross Cutting Concerns<br />Inventory Service<br />Logging<br />Shopping Cart Operator<br />Security<br />Shopping Service...
AOP Implementations<br />AspectJ<br />Compile time changes the byte code of existing .class to inject new concerns in code...
What is Weaving?<br />Class B<br />Class A1<br />Class A2<br />String getData()<br />void method1()<br />void method2()<br />
What is Weaving?<br />Class B<br />Class A1<br />Class A2<br />String getData()<br />void method1()<br />void method2()<br />
What is Weaving?<br />Class B<br />Class A1<br />Class A2<br />String getData()<br />void method1()<br />void method2()<br...
AspectJ<br />
Problem -1 Tracing/Logging<br />
New Project<br />New Person<br />Logging/Tracing<br />Debugging Flow<br />
Logging/Tracing<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />L<br />L<br />L<br />Shopping Cart Opera...
Problem -2 Profiling<br />
Problem -3 Caching<br />
Caching to avoid DB hit<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />Cache<br />Shopping Cart Operato...
Problem -4 Connection Pooling<br />
Connection Pooling<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />Shopping Cart Operator<br />CartView....
Problem -5 Contracts<br />
Use of System.out.println<br />View calling DAO<br />……….<br />
Problem -6 Adding to existing Classes<br />
Extending without changing code<br />Item<br />String getID();<br />String getName();<br />String getDescription();<br />b...
AspectJ using STS<br />Download from http://www.springsource.com/developer/sts<br />
HelloWorldAspectJ<br />Code Example<br />
package com.test;<br />/**<br /> * @author rohit<br /> *<br /> */<br />public class Helloworld {<br />public static void m...
package com.test.aspects;<br />/**<br /> * @author rohit<br /> *<br /> */<br />public aspect WorldAspect {<br />pointcut w...
ackagecom.test.aspects;<br />/**<br /> * @author rohit<br /> *<br /> */<br />public aspect Decorator {<br />declare preced...
Output of Program<br />****    -->  Hello World!    ****<br />
AspectJ Process<br />Main Java Source<br />.Classes<br />Business Logic<br />.Classes<br />AspectJ Compiler<br />Business ...
Different Types of Aspect<br />Dynamic Cross Cutting<br />Static Cross Cutting<br />
Dynamic Cross Cutting<br />Join Points<br />Point Cut<br />Advice<br />Join Points<br />Join Points<br />
Flow Example<br />JoinPoint<br />InventoryService<br />InventoryDAO<br />Item getItem(long id)<br />Item getItem(long id)<...
Writing pointcuts<br />accessor return type<br />execution(* com.mypackage.myClass.method(..))<br />Takes any args<br />ex...
Static Cross Cutting<br />Introduction/ITD<br />Employee<br />Inject equals() & hashCode()<br />private String firstName;<...
Static Cross Cutting<br />Compile Type Declaration<br />declare warning : get(* System.out):<br /> ”Use Logging Framework"...
RUN TIME WEAVING<br />Proxy<br />Target<br />Caller<br />Pointcut<br />Advice 2<br />Advice 1<br />Pointcut<br />Only Meth...
Spring Proxy Generation<br />   Using java.lang.reflect.Proxy Class<br />   Using the CGLIB library<br />
Writing pointcuts<br />Any Return type<br />execution(* com.mypackage.myClass.method(..))<br />Takes any args<br />Trigger...
Springs Advice<br />Before<br />Method<br />Around<br />After Returning<br />Successful Return<br />Exception<br />After T...
AspectJ Syntax<br />
public aspect ExampleAspect {<br />	Map<Long, Object> userIdMap = new HashMap<Long, Object>();<br />	//Caching Point Cut<b...
public aspect ExampleAspect {<br />……<br />	//ITD / Introduction<br />public StringShopItem.toString(){<br />return "ShopI...
Logging, Tracing & Profiling<br />Code Example<br />
Pooling and Caching<br />Code Example<br />
Spring AOP<br />
RUN TIME WEAVING<br />Proxy<br />Target<br />Caller<br />Pointcut<br />Advice 1<br />Advice 2<br />Pointcut<br />Only Meth...
Spring Proxy Generation<br />   Using java.lang.reflect.Proxy Class<br />   Using the CGLIB library<br />
Writing pointcuts<br />Any Return type<br />execution(* com.mypackage.myClass.method(..))<br />Takes any args<br />Trigger...
Springs Advice<br />Before<br />Method<br />Around<br />After Returning<br />Successful Return<br />Exception<br />After T...
Introduction<br />
Modelling Domain<br />Spring Context<br />Bean<br />Instantiate<br />Configure<br />
Modelling Domain<br />Spring Context<br />Hibern-ate<br />Instantiates<br />Configures<br />Bean<br />
Spring + AspectJ<br /><aop:spring-configured/><br />
Conclusion<br />AspectJ<br />More Powerful – methods, fields, constructor, aspect, field weaving<br />Compile time weaving...
Upcoming SlideShare
Loading in...5
×

Aspect Oriented Prog with AspectJ & Spring AOP

2,473

Published on

Detailed Presentation talking about Aspect Oriented Programming using
1. AspectJ
2. Spring AOP

Published in: Education, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,473
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
73
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Aspect Oriented Prog with AspectJ & Spring AOP

  1. 1. Aspect Oriented Programming<br />By <br />Rohit Ghatol <br />& <br />LoukikPurohit<br />
  2. 2. What is Aspect Oriented Programming?<br />
  3. 3. Core Business Logic<br />
  4. 4. Concerns<br />Pooling<br />Tracing<br />Logging<br />Caching<br />Transactions<br />Contract<br />Profiling<br />
  5. 5. Problem Statement<br />
  6. 6. Layered Architecture<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />Shopping Cart Operator<br />CartView.jsp<br />Shopping Cart DAO<br />Shopping Service<br />Database<br />Service<br />View<br />
  7. 7. Non AOP Solution<br />
  8. 8. Cross Cutting Concerns<br />Inventory Service<br />Logging<br />Shopping Cart Operator<br />Security<br />Shopping Service<br />Transactions<br />Inventory DAO<br />Caching<br />Shopping Cart DAO<br />Pooling<br />
  9. 9. AOP Implementations<br />AspectJ<br />Compile time changes the byte code of existing .class to inject new concerns in code<br />Spring AOP<br />Works with runtime proxies which extend and wrap and delegate calls to original objects<br />
  10. 10. What is Weaving?<br />Class B<br />Class A1<br />Class A2<br />String getData()<br />void method1()<br />void method2()<br />
  11. 11. What is Weaving?<br />Class B<br />Class A1<br />Class A2<br />String getData()<br />void method1()<br />void method2()<br />
  12. 12. What is Weaving?<br />Class B<br />Class A1<br />Class A2<br />String getData()<br />void method1()<br />void method2()<br />Proxy<br />
  13. 13. AspectJ<br />
  14. 14. Problem -1 Tracing/Logging<br />
  15. 15. New Project<br />New Person<br />Logging/Tracing<br />Debugging Flow<br />
  16. 16. Logging/Tracing<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />L<br />L<br />L<br />Shopping Cart Operator<br />CartView.jsp<br />L<br />L<br />Shopping Cart DAO<br />Shopping Service<br />L<br />Database<br />Service<br />View<br />
  17. 17. Problem -2 Profiling<br />
  18. 18.
  19. 19. Problem -3 Caching<br />
  20. 20. Caching to avoid DB hit<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />Cache<br />Shopping Cart Operator<br />CartView.jsp<br />Shopping Cart DAO<br />Shopping Service<br />Cache<br />Database<br />Service<br />View<br />
  21. 21. Problem -4 Connection Pooling<br />
  22. 22. Connection Pooling<br />Inventory DAO<br />Inventory Service<br />Inventory.jsp<br />Shopping Cart Operator<br />CartView.jsp<br />4<br />Shopping Cart DAO<br />Shopping Service<br />Database<br />Service<br />View<br />4<br />
  23. 23. Problem -5 Contracts<br />
  24. 24. Use of System.out.println<br />View calling DAO<br />……….<br />
  25. 25. Problem -6 Adding to existing Classes<br />
  26. 26. Extending without changing code<br />Item<br />String getID();<br />String getName();<br />String getDescription();<br />boolean equals();<br />Inthashcode();<br />Item clone();<br />String toString();<br />Referred as “Introduction” or ITD<br />
  27. 27. AspectJ using STS<br />Download from http://www.springsource.com/developer/sts<br />
  28. 28. HelloWorldAspectJ<br />Code Example<br />
  29. 29. package com.test;<br />/**<br /> * @author rohit<br /> *<br /> */<br />public class Helloworld {<br />public static void main(String args[]){<br />Helloworldhelloworld = new Helloworld();<br />helloworld.hello();<br /> }<br />public void hello(){<br />System.out.print("Hello");<br /> }<br />}<br />HelloWorld.java<br />
  30. 30. package com.test.aspects;<br />/**<br /> * @author rohit<br /> *<br /> */<br />public aspect WorldAspect {<br />pointcut world() : <br />call( public void com.test.Helloworld.hello()) ;<br />before(): world(){<br />System.out.print("--> ");<br /> }<br />after() : world() {<br />System.out.print(" World!");<br /> }<br />}<br />WorldAspect.java<br />
  31. 31. ackagecom.test.aspects;<br />/**<br /> * @author rohit<br /> *<br /> */<br />public aspect Decorator {<br />declare precedence: Decorator, WorldAspect;<br />pointcut world() : <br />call(* *.hello(..)) <br /> && !within(Decorator)<br /> && !within(WorldAspect)<br /> ;<br />void around(): world(){<br />System.out.print("**** ");<br />proceed();<br />System.out.print(" ****");<br /> }<br />}<br />Decorator.java<br />
  32. 32. Output of Program<br />**** --> Hello World! ****<br />
  33. 33. AspectJ Process<br />Main Java Source<br />.Classes<br />Business Logic<br />.Classes<br />AspectJ Compiler<br />Business Logic with Concerns now weaved in<br />Aspects Source<br />
  34. 34. Different Types of Aspect<br />Dynamic Cross Cutting<br />Static Cross Cutting<br />
  35. 35. Dynamic Cross Cutting<br />Join Points<br />Point Cut<br />Advice<br />Join Points<br />Join Points<br />
  36. 36. Flow Example<br />JoinPoint<br />InventoryService<br />InventoryDAO<br />Item getItem(long id)<br />Item getItem(long id)<br />Around Advice<br />Psuedo Code<br />If(item in cache){<br /> return item;<br />}else{<br /> //fetch from db;<br /> proceed();<br /> put item in cache;<br /> return item<br />}<br />PointCut :execution(InventoryDAO.getItem(..))<br />PointCut :execution(*DAO.getItem(..))<br />
  37. 37. Writing pointcuts<br />accessor return type<br />execution(* com.mypackage.myClass.method(..))<br />Takes any args<br />execution/<br />call/<br />new/<br />handler<br />Method Declaration<br />Reference : http://www.eclipse.org/aspectj/doc/released/progguide/quick.html<br />
  38. 38. Static Cross Cutting<br />Introduction/ITD<br />Employee<br />Inject equals() & hashCode()<br />private String firstName;<br />private String lastName;<br />private long birthDate<br />//…getters & setters()<br />Inject persist() & merge()<br />Inject implements Serializable<br />
  39. 39. Static Cross Cutting<br />Compile Type Declaration<br />declare warning : get(* System.out):<br /> ”Use Logging Framework";<br />
  40. 40. RUN TIME WEAVING<br />Proxy<br />Target<br />Caller<br />Pointcut<br />Advice 2<br />Advice 1<br />Pointcut<br />Only Method Pointcuts<br />
  41. 41. Spring Proxy Generation<br /> Using java.lang.reflect.Proxy Class<br /> Using the CGLIB library<br />
  42. 42. Writing pointcuts<br />Any Return type<br />execution(* com.mypackage.myClass.method(..))<br />Takes any args<br />Trigger on method execution<br />Method Declaration<br />
  43. 43. Springs Advice<br />Before<br />Method<br />Around<br />After Returning<br />Successful Return<br />Exception<br />After Throwing<br />
  44. 44. AspectJ Syntax<br />
  45. 45. public aspect ExampleAspect {<br /> Map<Long, Object> userIdMap = new HashMap<Long, Object>();<br /> //Caching Point Cut<br />pointcutdaoGetters(User user): <br />execution(* com.test..*ShoppingCartDao.get*(..)) <br /> && args(user);<br /> Object around(User user) : daoGetters(user) {<br />if (userIdMap.containsKey(user.getUserId())) {<br />System.out.println(" #Caching: Got ShoppingCart from Cache - Cheap");<br />returnuserIdMap.get(user.getUserId());<br /> } else {<br /> Object object = proceed(user);<br />System.out.println(" #Caching: Got ShoppingCart from Database - Expensive");<br />userIdMap.put(user.getUserId(), object);<br />returnobject;<br /> }<br /> }<br />//…. Turn to next slide <br />}<br />
  46. 46. public aspect ExampleAspect {<br />……<br /> //ITD / Introduction<br />public StringShopItem.toString(){<br />return "ShopItem [itemId=" + getItemId() + ", itemName=" + getItemName()<br /> + ", itemDescription=" + getItemDescription() + "]";<br /> }<br /> //Compiler Warnign and Errors<br />declarewarning:get(* System.out):"Do not useSystem.out.println";<br />declarewarning : (call(* com.test..*Dao.*(..)) && <br /> !within(com.test..*Service)):<br /> "DAOsshouldonlybecalled from Service layer";<br />}<br />
  47. 47. Logging, Tracing & Profiling<br />Code Example<br />
  48. 48. Pooling and Caching<br />Code Example<br />
  49. 49. Spring AOP<br />
  50. 50. RUN TIME WEAVING<br />Proxy<br />Target<br />Caller<br />Pointcut<br />Advice 1<br />Advice 2<br />Pointcut<br />Only Method Pointcuts<br />
  51. 51. Spring Proxy Generation<br /> Using java.lang.reflect.Proxy Class<br /> Using the CGLIB library<br />
  52. 52. Writing pointcuts<br />Any Return type<br />execution(* com.mypackage.myClass.method(..))<br />Takes any args<br />Trigger on method execution<br />Method Declaration<br />
  53. 53. Springs Advice<br />Before<br />Method<br />Around<br />After Returning<br />Successful Return<br />Exception<br />After Throwing<br />
  54. 54. Introduction<br />
  55. 55. Modelling Domain<br />Spring Context<br />Bean<br />Instantiate<br />Configure<br />
  56. 56. Modelling Domain<br />Spring Context<br />Hibern-ate<br />Instantiates<br />Configures<br />Bean<br />
  57. 57. Spring + AspectJ<br /><aop:spring-configured/><br />
  58. 58. Conclusion<br />AspectJ<br />More Powerful – methods, fields, constructor, aspect, field weaving<br />Compile time weaving – works with likes of Android<br />Learning curve is more<br />Much better in speed<br />Needs special compiler<br />Spring AOP<br />Less Powerful – only supports method level weaving<br />Runtime weaving strictly requires Spring<br />Easier to learn<br />Much slower than AspectJ<br />No such need<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×