A spect O riented P rogramming using Spring Framework Arseni Grigorjev arsenikum[gmail.com]
Me Server side Java (~5y) Aqris Software Online gambling
Simple service method 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; } … } + authorization + error handling + performance monitoring + caching + logging
 
Aspect Oriented Programming A spect  O riented  P rogramming (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 Write your mainline application logic Write aspects to implement cross-cutting concerns 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 Suite AOP Support
SpringSource Tools Suite AOP Support (2)
Useful Links Aspect Oriented Programming with Spring http://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?

Spring AOP @ DevClub.eu

  • 1.
    A spect Oriented P rogramming using Spring Framework Arseni Grigorjev arsenikum[gmail.com]
  • 2.
    Me Server sideJava (~5y) Aqris Software Online gambling
  • 3.
    Simple service methodpublic 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( &quot;going to fetch orders for &quot; + customerId); result = customerDao.getOrders(customerId); logger.info( &quot;fetched orders for &quot; + customerId + &quot;: &quot; + result); ordersCache.putOrders(customerId, result); } } catch (CusstomerNotFoundException e) { logger.error( &quot;Customer &quot; + customerId + &quot; not found&quot; , e); result = null ; } long end = System. currentTimeMillis(); logger.debug( &quot;method took &quot; + (end - start) + &quot; ms&quot; ); return result; } … } + authorization + error handling + performance monitoring + caching + logging
  • 4.
  • 5.
    Aspect Oriented ProgrammingA spect O riented P rogramming (AOP) enables modularization of cross-cutting concerns Separation of Concerns principle – no code tangling 1:1 principle – no code scattering
  • 6.
    Cross-Cutting Concerns ExamplesLogging and Tracing Transaction Management Security Caching Error Handling Performance Monitoring Custom Business Rules
  • 7.
  • 8.
    How AOP WorksWrite your mainline application logic Write aspects to implement cross-cutting concerns Bind it all together
  • 9.
  • 10.
    Leading AOP TechnologiesAspectJ 1995, original AOP technology Offers a full Aspect Oriented Programming language Modifies byte code to add aspects into your application!
  • 11.
    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
  • 12.
    Core AOP ConceptsJoin 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
  • 13.
    Spring AOP ExampleBusiness logic: public class SimpleService { … public List getCustomerOrders( long operatorId, long customerId) { return customerDao.getOrders(customerId); } … }
  • 14.
    Spring AOP Example(2) Performance tracing aspect:
  • 15.
    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>
  • 16.
    Advice Types @Around@Before @After @AfterReturning @AfterThrowing
  • 17.
  • 18.
    SpringSource Tools SuiteAOP Support (2)
  • 19.
    Useful Links AspectOriented Programming with Spring http://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
  • 20.

Editor's Notes

  • #6 -- Разделение ответсвенности ( Tangling – переплетение) -- Scattering – рассеивание, ведёт к дупликации
  • #8 -- Tangling – переплетение -- Scattering – рассеивание, ведёт к дупликации