RegEx based Spring AOP auto-proxy creator

When using Spring AOP in a medium-large application to add behavior to objects using crosscutting, it is desirable to have auto-proxy creators rather then creating a proxy for each bean (Spring AOP is proxy based). When auto-proxying a subset of beans, BeanNameAutoProxyCreator is most commonly used. But it has certain limitations, you have to provide a whole list of beanNames, which in a fairly large application becomes cumbersome and sometimes hard to maintain. Now because the application we were using had fairly well categorized beans with similar IDs, it was convenient to use a pattern or regular expression to identify them, but BeanNameAutoProxyCreator doesn’t support regular expressions. So I extended the BeanNameAutoProxyCreator to provide regular expression support.

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class RegExpBeanNameAutoProxyCreator extends BeanNameAutoProxyCreator implements ApplicationContextAware {

  private ApplicationContext applicationContext;

   @Autowired
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }

  public void setPattern(String pattern) {

    String beans[] = applicationContext.getBeanDefinitionNames();
    List<String> proxiedBeans = new ArrayList<String>();

    Pattern pat = Pattern.compile( pattern, Pattern.CASE_INSENSITIVE );
    for( String bean : beans ) {
        Matcher matcher = pat.matcher( bean );
        if( matcher.find() ) {
            proxiedBeans.add( bean );
        }
    }

    setBeanNames( proxiedBeans.toArray( new String [proxiedBeans.size()] ) );
  }
}

This auto-proxy creator takes a regular expression and matches all the beans available in the Spring Application Context to create implicit runtime proxies for the matched beans. The bean definition for RegExpBeanNameAutoProxyCreator is similar to BeanNameAutoProxyCreator but it takes a “pattern” rather then the “beanNames”.

<bean class="xeus.spring.aop.RegExpBeanNameAutoProxyCreator">
    <property name="pattern">
        <value>^(?!.*(NoProxy)).*Impl$</value>
    </property>
    <property name="interceptorNames">
        <list>
            <value>someInterceptor</value>
        </list>
    </property>
</bean>

Here the “pattern” property takes the regular expression to match the beans available in spring context. It is pretty handy if you don’t want to put the whole list of beanNames here; you can also use this to exclude beans as illustrated by the regular expression in this example.

1 Comment

Leave a Reply