Common AOP utility classes. 1.0.0.2 API

Usually MethodInterceptor's are used with Spring's org.springframework.aop.framework.ProxyFactoryBean or org.springframework.aop.framework.ProxyFactoryBean.

See:
          Description

Packages
org.equilibriums.aop.utils.interceptor.composite  
org.equilibriums.aop.utils.interceptor.composite.handlers  
org.equilibriums.aop.utils.interceptor.convert  
org.equilibriums.aop.utils.interceptor.convert.converters  
org.equilibriums.aop.utils.interceptor.delegate  
org.equilibriums.aop.utils.interceptor.stub  

 

Usually MethodInterceptor's are used with Spring's org.springframework.aop.framework.ProxyFactoryBean or org.springframework.aop.framework.ProxyFactoryBean.

Here is sample setup using org.springframework.aop.framework.ProxyFactoryBean

Lest say we have ProcessHandler interface that we want to proxy and return some pre defined value for "process" using StubWithReturnValueInterceptor.

Other interceptors are configured in the same manner.

private static interface ProcessHandler {
				
     public String process(String data);
 } 
   
   
 public static void main(String[] args){  
     StubWithReturnValueInterceptor interceptor = StubWithReturnValueInterceptor();		
     interceptor.setReturnValue( "intercepted process" );
 		
     org.springframework.aop.framework.ProxyFactory factory = new org.springframework.aop.framework.ProxyFactory( 
     new Class[]{ProcessHandler.class} );
     //use this if you want to create proxy object that implements ProcessHandler.
     factory.setTargetSource( org.springframework.aop.target.EmptyTargetSource.INSTANCE );  
     //If you want to intercept "process" method on real object use this instead of above line
     //factory.setTarget(someRealObject);      				
     org.springframework.aop.support.NameMatchMethodPointcut nameMatchMethodPointcut1 = new org.springframework.aop.support.NameMatchMethodPointcut();	
     nameMatchMethodPointcut1.setMappedName( "process" );		
     factory.addAdvisor(  new org.springframework.aop.support.DefaultPointcutAdvisor( nameMatchMethodPointcut1, interceptor ) );
		
     ProcessHandler p = (ProcessHandler)factory.getProxy();
	    
     String value = p.process(); //returns "intercepted process".
 }

Same can be done with the following Spring XML configuration.

<bean id="proxyProcessHandler" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="interfaces"><list><value>ProcessHandler</value></list></property>
      <!-- use this if you want to create proxy object that implements ProcessHandler. -->
      <property name="targetSource">
          <util:constant static-field="org.springframework.aop.target.EmptyTargetSource.INSTANCE" />
      </property>
      <!-- If you want to intercept "process" method on real object use this instead of above line
      <property name="target" ref="someRealObject"/> -->
      <property name="interceptorNames">
          <list>
              <value>interceptor</value>
          </list>
      </property>
  </bean>
  
  <bean id="interceptor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
      <property name="pointcut">
          <bean class="org.springframework.aop.support.NameMatchMethodPointcut">
              <property name="mappedName" value="process" />
          </bean>
      </property>
      <property name="advice">
          <bean	class="org.equilibriums.aop.utils.interceptor.stub.StubWithReturnValueInterceptor">
              <property name="returnValue" value="intercepted process"/>
          </bean>
      </property>
  </bean>

Other types of configurations.

Both org.springframework.aop.framework.ProxyFactoryBean and org.springframework.aop.framework.ProxyFactoryBean take list of MethodInterceptor's which can be used for some interesting configurations. For example ConvertMethodReturnValueInterceptor can be configured before DelegateInterceptor to do additional return value conversion before returning to the caller.

Performance issues.

Both org.springframework.aop.framework.ProxyFactoryBean and org.springframework.aop.framework.ProxyFactoryBean will reuse same looked up method before calling the chain of interceptors. This should help to lower performance impact but we still will see from 0.5 to 4 times of performance degradation in certain configurations which should be considered when using AOP in highly transactional situations.



Copyright © 2011. All Rights Reserved.