Home / Java / RegEx based Spring AOP auto-proxy creator

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.

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”.

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.

About Author:

Leave A Comment

Your email address will not be published. Required fields are marked *