Aspect-Oriented Programming by Arseni Grigorjev

20
Aspect Oriented Programming using Spring Framework Arseni Grigorjev arsenikum[gmail.com]

description

AOP with SpringFramework

Transcript of Aspect-Oriented Programming by Arseni Grigorjev

Page 1: Aspect-Oriented Programming by Arseni Grigorjev

AspectOrientedProgrammingusing Spring Framework

Arseni Grigorjevarsenikum[gmail.com]

Page 2: Aspect-Oriented Programming by Arseni Grigorjev

Me

• Server side Java (~5y)• Aqris Software• Online gambling

Page 3: Aspect-Oriented Programming by Arseni Grigorjev

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
Page 5: Aspect-Oriented Programming by Arseni Grigorjev

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

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

System Evolution: Conventional

Page 8: Aspect-Oriented Programming by Arseni Grigorjev

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

System Evolution: AOP Based

Page 10: Aspect-Oriented Programming by Arseni Grigorjev

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

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

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

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

Spring AOP Example (2)

• Performance tracing aspect:

Page 15: Aspect-Oriented Programming by Arseni Grigorjev

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

Advice Types

• @Around• @Before• @After• @AfterReturning• @AfterThrowing

Page 17: Aspect-Oriented Programming by Arseni Grigorjev

SpringSource Tool SuiteAOP Support

Page 18: Aspect-Oriented Programming by Arseni Grigorjev

SpringSource Tools SuiteAOP Support (2)

Page 19: Aspect-Oriented Programming by Arseni Grigorjev

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

Thank You!

• Questions?