Spring AOP Concepts

As a natural lover, i was not very enthusiast about spring framework, for me i think spring framework is a make-up or add-on JVM for natural Java language. As spring authors mentioned, spring is following Hollywood concept, where a small swimming pool can show like big ocean. But Spring is really good for ‘all in one’ style. Spring is becoming fastest growing framework than EJB, or .Net. The same is now start worrying Java community that SpringSource now mobilizing something towards pay per use model, here is latest spring dm server licensing terms, http://www.springsource.com/products/suite/dmserver/licensingfaq and top executive profile looks little chary that other than Rod (CEO) and Adrian (CTO) are not techies but extensive business experiences and venture capitalists. Any way it is worth to pay some share for this excellent framework if that happens.

But any way more configurations and less coding is always good. I was ebullient with spring’s Aspect Oriented Programming (AOP) features. It was really great and helped me to become Spring enthusiast. Imagine we are in the mission of repairing an electrical wire system in a 100 years old skyscraper. The issue is (for sake of understanding), 100th floor is not getting enough voltage due to damaged wiring in lower floors. What we do? We have to use some modularity(cut across from floor 1 to 10 first and then 10-20 so on) to find out where is the more damage which causes power lose and then start repairing it or we want to go floor by floor to find out the issue. AOP is the great for modularity, cut across multiple objects to find out issues or retrieve some debugging message.

AOP is the main feature for spring framework to perform some basic stuff. When a singleton bean depends on a session bean, we have to aop scoped proxy to fetch real object at runtime, because singleton created only one time at container and session object changes per session. It is great example of Hollywood concept. A singleton bean thinks that it holds a real object but it doesn’t know that it is just calling a proxy method and in background it is fetching a real object each time. What a trick- shot?

Spring AOP working with interceptor and AspectJ concepts. Let me jump on to Spring AOP concepts (with simple real time analogy because AOP terminology not very intuitive).

Join points: How we want to join a one modular, our aim here to join 1 to 99 floors, are we want to join 1-10 first then 10-20 or 1-25 and then 25-50 etc. In Spring AOP context a joint point is method execution.
Advice: This is very simple, how we want wire on each floor, we need some advice from building blue print. To be clear on advice here for spring is only interceptor.
Pointcuts
: This is nothing but cutting across 1-10 floors thru an execution (outlet) points. Each floor has several outlets, our interest here is electric outlet not phone outlet. So we need to touch across electric outlets.

In spring context a method with particular names, spring provides several “pointcutadvisors” to simplify defining multiple method names and to integrate with advice. (this is little confusing but think like “pointcutadvisor” is some one who paints all electrical outlet with blue color(pointcut), so for us it is simple to wire only blue outlet without each time looking into blue print(advice)).

Once we understand our building wire repair model and AOP terminology, coding is just 3 lines configuration (assumption is we are going to use existing Spring AOP interceptors and point cuts).

Spring has following main AOP interceptors to ready to use. Please check out at http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/aop/interceptor/package-summary.html

PerformanceMonitorInterceptor
JamonPerformanceMonitorInterceptor
SimpleTraceInterceptor
ExposeInvocationInterceptor
ConcurrencyThrottleInterceptor
ClassLoaderAnalyzerInterceptor

If we want to use ClassLoaderAnalyzerInterceptor for your application to analyze how class loader impacting some method executions


//define a bean id. This is advisor( a blue print for building )
<bean id="ClassLoaderAnalyzer"
class="org.springframework.aop.interceptor.ClassLoaderAnalyzerInterceptor"/>

//main pointcut definitions. Spring has rich set of pointout
//definions support classes. RegexpMethodPointcutAdvisor will
//help us to define out point cuts. (someone we asked to paint
//blue color for all electrical outlet)

<bean id=" ClassLoaderAnalyzerPointcut"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

<property name="advice" ref=" ClassLoaderAnalyzer ">

<property name="patterns">

<list>

<value>com.foo.complexClasses.class1.loadMethod

<value>com.foo.complexClasses.class2.loadMethod2

</list>

</property>

</bean>




Source:
http://www.theserverside.com/news/thread.tss?thread_id=50873#270405 (for spring dm server pricing model)
http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-using-aspectj
http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/aop/interceptor/package-summary.html
http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/aop/support/package-summary.html

Comments

Popular posts from this blog

Coupon Crazy

Google's Killer Application.

Uncontrolled Musing