BeanNameAutoProxyCreator, like the metadata attributes approach, allows you to configure declarative services to a large number of beans in the Spring IoC container without needing any extra configuration. Unlike the metadata attributes approach,
BeanNameAutoProxyCreator does not require any changes to the Java source code.
BeanNameAutoProxyCreator allows you to apply one or more method interceptors to a large number of beans. You only need to configure your caching settings once and then provide the name of the bean to the proxy creator. The following example shows how to specify the caching-related method interceptors in a
<coherence:methodMapInterceptors cachingInterceptorId=" cachingInterceptor" flushingInterceptorId=" flushingInterceptor"> <coherence:caching methodFQN="org.springmodules.cache.samples.dao.CustomerDao.load" cacheName="customerCache" /> <coherence:flushing methodFQN="org.springmodules.cache.samples.dao.CustomerDao.update" cacheNames="customerCache" /> </coherence:methodMapInterceptors> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <idref local="customerDao" /> </list> </property> <property name="interceptorNames"> <list> <value> cachingInterceptor</value> <value> flushingInterceptor</value> </list> </property> </bean> <bean id="customerDao" class="org.springmodules.cache.samples.dao.CustomerDao" /> <!-- Properties --> </bean>
As you may have guessed, the configuration presented above does exactly the same thing as the configuration shown in the sections explaining per-bean configuration and source-level metadata attributes.
BeanNameAutoProxyCreator is provided by the Spring Framework. To learn more, refer to the Spring online documentation.
Download the sample code used in this article: caching.zip
Although caching can dramatically improve the performance of enterprise applications, it also can introduce considerable complexity to the application life cycle. This article introduces Declarative Caching Services for Spring, a framework that encapsulates such complexity and provides code-free, transparent caching and cache flushing to Spring-powered applications.
In the spirit of Spring, declarative caching provides out-of-the-box support for various open source and commercial cache providers. It also offers different configuration strategies. It is up to us, the developers, to choose the combination that best suits the needs of our projects.
With declarative caching, developers can concentrate on the core requirements of their applications which results in code that is easier to understand, maintain, test, and reuse. Because caching services are configured in the Spring IoC container, they can be added only when truly needed, without making system-wide changes.
Alex Ruiz is a Software Engineer in the development tools organization at Oracle.