spring aop @ devclub.eu

Post on 22-May-2015

1.135 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

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.

TRANSCRIPT

AspectOrientedProgrammingusing Spring Framework

Arseni Grigorjevarsenikum[gmail.com]

Me

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

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

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

Cross-Cutting Concerns Examples

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

System Evolution: Conventional

How AOP Works

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

concerns3. Bind it all together

System Evolution: AOP Based

Leading AOP Technologies

• AspectJ– 1995, original AOP technology– Offers a full Aspect Oriented Programming language– Modifies byte code to add aspects into your application!

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

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

Spring AOP Example

• Business logic:

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

Spring AOP Example (2)

• Performance tracing aspect:

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>

Advice Types

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

SpringSource Tool SuiteAOP Support

SpringSource Tools SuiteAOP Support (2)

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

Thank You!

• Questions?

top related