O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Aspect Oriented Prog with AspectJ & Spring AOP

4.458 visualizações

Publicada em

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

Publicada em: Educação, Tecnologia
  • DOWNLOAD FULL eBOOK INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc eBook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookeBOOK Crime, eeBOOK Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Responder 
    Tem certeza que deseja  Sim  Não
    Insira sua mensagem aqui

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 />

×