Aspect-Oriented Programming by Arseni Grigorjev
-
Upload
anton-arhipov -
Category
Technology
-
view
2.580 -
download
2
description
Transcript of Aspect-Oriented Programming by Arseni Grigorjev
![Page 1: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/1.jpg)
AspectOrientedProgrammingusing Spring Framework
Arseni Grigorjevarsenikum[gmail.com]
![Page 2: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/2.jpg)
Me
• Server side Java (~5y)• Aqris Software• Online gambling
![Page 3: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/3.jpg)
public class SimpleService { … public List getCustomerOrders(long operatorId, long customerId) { List result; long start = System.currentTimeMillis(); if (!hasPermissions(operatorId)) { throw new NotAuthorizedException(); } try {
ordersCache.getOrders(customerId); if (result == null) { logger.info("going to fetch orders for " + customerId); result = customerDao.getOrders(customerId); logger.info("fetched orders for " + customerId + ": " + result); ordersCache.putOrders(customerId, result); }
} catch (CusstomerNotFoundException e) { logger.error("Customer " + customerId + " not found", e); result = null; } long end = System.currentTimeMillis(); logger.debug("method took " + (end - start) + " ms"); return result; } …}
Simple service method+ authorization+ authorization
+ error handling+ error handling
+ performance monitoring+ performance monitoring
+ caching+ caching
+ logging+ logging
![Page 4: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/4.jpg)
![Page 5: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/5.jpg)
Aspect Oriented Programming
• Aspect Oriented Programming (AOP) enables modularization of cross-cutting concerns– Separation of Concerns principle – no code tangling– 1:1 principle – no code scattering
![Page 6: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/6.jpg)
Cross-Cutting Concerns Examples
• Logging and Tracing• Transaction Management• Security• Caching• Error Handling• Performance Monitoring• Custom Business Rules
![Page 7: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/7.jpg)
System Evolution: Conventional
![Page 8: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/8.jpg)
How AOP Works
1. Write your mainline application logic2. Write aspects to implement cross-cutting
concerns3. Bind it all together
![Page 9: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/9.jpg)
System Evolution: AOP Based
![Page 10: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/10.jpg)
Leading AOP Technologies
• AspectJ– 1995, original AOP technology– Offers a full Aspect Oriented Programming language– Modifies byte code to add aspects into your application!
![Page 11: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/11.jpg)
Leading AOP Technologies (2)
• Spring AOP– Uses dynamic proxies to add aspects into your application– Only spring beans can be advised– Uses some of AspectJ expression syntax
![Page 12: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/12.jpg)
Core AOP Concepts
• Join Point– A point of execution of a program such as method call or field
assignment
• Pointcut– An expression that selects one or more Join Points
• Advice– Code to be executed at a Join Point that has been selected by a
Pointcut
• Aspect– A module that encapsulates Pointcuts and Advice
![Page 13: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/13.jpg)
Spring AOP Example
• Business logic:
public class SimpleService { … public List getCustomerOrders(long operatorId, long customerId) { return customerDao.getOrders(customerId); } …}
![Page 14: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/14.jpg)
Spring AOP Example (2)
• Performance tracing aspect:
![Page 15: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/15.jpg)
Spring AOP Example (3)
Configure the Aspect as a Bean:
<beans>
<aop:aspectj-autoproxy> <aop:include name=“performanceTracer” /> </aop:aspectj-autoproxy>
<bean id=“performanceTracer” class=“com.foo.bar.PerformanceTracer” />
</beans>
![Page 16: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/16.jpg)
Advice Types
• @Around• @Before• @After• @AfterReturning• @AfterThrowing
![Page 17: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/17.jpg)
SpringSource Tool SuiteAOP Support
![Page 18: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/18.jpg)
SpringSource Tools SuiteAOP Support (2)
![Page 19: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/19.jpg)
Useful Links
• Aspect Oriented Programming with Springhttp://static.springsource.org/spring/docs/2.5.x/reference/aop.html– Lots of examples– Highlights on AspectJ syntax
• SpringSource Tool Suite:http://www.springsource.com/products/sts– Eclipse IDE with Spring (and Spring AOP) support
![Page 20: Aspect-Oriented Programming by Arseni Grigorjev](https://reader033.fdocuments.in/reader033/viewer/2022061111/5455f65cb1af9fb66e8b4c49/html5/thumbnails/20.jpg)
Thank You!
• Questions?