Use SpringX to smiplify AutoProxy config.

how-to
Oct 24, 20093 mins

It’s about Spring AOP auto proxy feature.

Spring Old Way

   AutoProxy is a greate feature of Spring framework. Spring provides two AutoProxyCreator class to do this, one is used to config bean name, another is used to config method name. The two ‘Creator’ is BeanNameAutoProxyCreator, NameMatchMethodPointcutAdvisor.    But the config of them is a little tedious. In a JEE application, we need to add some AOP interceptors or advisor to ‘Service’ class, e.g.transaction,log,performance interceptor. Config is something like below,


<bean id="businessLogAdvisor" class="com.sunrise.jop.common.businesslog.BusinessLogAdvisor"/>	
<bean id="transactionAdvisor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
	<!-- omitted tx strategies -->
</bean>
 <bean id="jamonInterceptor" class="org.springframework.aop.interceptor.JamonPerformanceMonitorInterceptor"/> 	

Add interceptors to *Service


<!--config bean name -->
<bean id="BizServiceAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	<property name="proxyTargetClass" value="true"></property>
	<property name="beanNames">
		<list>
			<value>*Service</value>				
		</list>
	</property>
	<property name="interceptorNames">
		<list>				
			<value>businessLogMethodPointcutAdvisor</value>
			<value>trasactionMethodPointcutAdvisor</value>
                        <value>jamonMethodPointcutAdvisor</value>			
		</list>
	</property>
</bean>

<!--1 config method  to be loggded -->
<bean id="businessLogMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
		<property name="mappedNames">
			<list>
				<value>doCreate*</value>
				<value>doUpdate*</value>
				<value>doRemove*</value>
			</list>
		</property>
		<property name="advice">
			<ref local="businessLogAdvisor" />
		</property>
	</bean>

<!--2 config method  to be transactated -->
<bean id="trasactionMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
		<property name="mappedNames">
			<list>
				<value>do*</value>
			</list>
		</property>
		<property name="advice">
			<ref local="transactionAdvisor" />
		</property>
	</bean>	

<!--3 config method  to be monitored -->
<bean id="jamonMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
	<property name="mappedNames">
		<list>
			<value>do*</value>
		</list>
	</property>
	<property name="advice">
		<ref local="jamonInterceptor" />
	</property>
</bean>	

As we can see, the config is very tedious because 3 MethodPointcutAdvisor need to be configed due to one MethodPointcutAdvisor can only has one advice property.

SpringX’s new Way

In SpringX framework, a new AutoProxyCreator is provided called SimpleNameMatchAutoProxyCreator. It can reduce the config greatly. SpringX integrated bean name and method name config into ONE bean, reducing much tedious work. As showed below


<!--	Simplify AOP config. -->  	
<bean id="ServiceSimpleNameMatchAutoProxyCreator" class="org.bamboo.springx.aop.autoproxy.SimpleNameMatchAutoProxyCreator" >
		<property name="proxyTargetClass" value="true"></property>
		<property name="beanNameAndMethodNames">
			<list>
				<value>*Service,delete*</value> 
				<value>*Service,find*</value>
				<value>*Service,create*</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>			
				<value>logAdvisor</value>
                                <value>transactionAdvisor</value>
                                <value>performanceAdvisor</value>
			</list>
		</property>
  </bean> 

Experience SpringX framework on google code:SpringX 1.2.2

Raymond KK He is a principal software engineer and architect in telecom field in Guangzhou,China. He wrote blogs and articles on javaeye.com community previousely. His interests are in JavaEE, framework design and development.

More from this author