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

Spring AOP @ DevClub.eu

Embed Size (px)

DESCRIPTION

General introduction into Aspect-Oriented programming and small overview to its implementation in Spring. This presentation was made at DevClub.eu gathering in Tallinn.

Citation preview

Page 1: Spring AOP @ DevClub.eu

AspectOrientedProgrammingusing Spring Framework

Arseni Grigorjevarsenikum[gmail.com]

Page 2: Spring AOP @ DevClub.eu

Me

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

Page 3: Spring AOP @ DevClub.eu

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: Spring AOP @ DevClub.eu
Page 5: Spring AOP @ DevClub.eu

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: Spring AOP @ DevClub.eu

Cross-Cutting Concerns Examples

• Logging and Tracing• Transaction Management• Security• Caching• Error Handling• Performance Monitoring• Custom Business Rules

Page 7: Spring AOP @ DevClub.eu

System Evolution: Conventional

Page 8: Spring AOP @ DevClub.eu

How AOP Works

1. Write your mainline application logic2. Write aspects to implement cross-cutting

concerns3. Bind it all together

Page 9: Spring AOP @ DevClub.eu

System Evolution: AOP Based

Page 10: Spring AOP @ DevClub.eu

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: Spring AOP @ DevClub.eu

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: Spring AOP @ DevClub.eu

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: Spring AOP @ DevClub.eu

Spring AOP Example

• Business logic:

public class SimpleService { … public List getCustomerOrders(long operatorId, long customerId) { return customerDao.getOrders(customerId); } …}

Page 14: Spring AOP @ DevClub.eu

Spring AOP Example (2)

• Performance tracing aspect:

Page 15: Spring AOP @ DevClub.eu

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: Spring AOP @ DevClub.eu

Advice Types

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

Page 17: Spring AOP @ DevClub.eu

SpringSource Tool SuiteAOP Support

Page 18: Spring AOP @ DevClub.eu

SpringSource Tools SuiteAOP Support (2)

Page 19: Spring AOP @ DevClub.eu

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: Spring AOP @ DevClub.eu

Thank You!

• Questions?