Working on Spring, we do read a lot on AOP – Aspect Oriented Programming. Is it something different from Object Oriented Programming. Nope, AOP is kind of a design pattern that we had been using fairly regularly in our natural programming but has been given a big name by Spring, coz of the beauty with which it has been implemented by Rod Johnson.

Its called a Proxy-pattern. Its a facade. If you have dealt with proxies in core java or with an interface called java.lang.reflect.InvocationHandler, you would agree with me. For more details, see my previous post http://sanchit6.wordpress.com/2008/08/02/springing-dynamic-proxy-with-pure-java
However thats not all what spring does. There are few limitations with using this core java way of Proxy. It only works on classes that have Interfaces. So if you want to proxy your class, without using an interface, you can’t; untill you use the savior CGLib (Code Generation Library). This library can even help you proxy or advice your classes.

<bean id="name"
	class="org.springframework.aop.framework.ProxyFactoryBean" >
	<property name="target" ref="myClassTargetBean"></property>
	<property name="interfaces">
		<list>
			<value>com.java.MyInterface</value>
		</list>
	</property>
	<property name="interceptorNames" >
		<list>
			<value>someInterceptor</value>
		</list>
	</property>
</bean>

This is a sample spring configuration to define a proxy around some class implementing com.java.MyInterface declared as myClassTargetBean and advised/proxied by someInterceptor. What if we don’t have an interface. Invoking CGLib is fairly simple. Just don’t provide the interfaces property; instead declare this property

<property name="proxyTargetClass" value="true" />

Hope that helps…cheers, bye.



No Responses Yet to “Springing – AOP/Proxy Patterns”  

  1. No Comments Yet

Leave a Reply