Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-16295

Programmatic creation of caching proxies using CacheProxyFactoryBean does not work

    XMLWordPrintable

    Details

      Description

      When a user attempts to create caching proxies using the o.s.cache.interceptor.CacheProxyFactoryBean, this fails to work.

      A user might want to use the CacheProxyFactoryBean to introduce caching behavior and logic to classes from a 3rd party library. This recently came up in a StackOverflow post. Please read the post and the answers (especially this one) for further details.

      Ideally, a user would be able to add caching behavior to any class, programmatically (not declaratively) using...

      @Bean
      CacheProxyFactoryBean someBean() {
      
          CacheProxyFactoryBean factoryBean = new CacheProxyFactoryBean();
      
          factoryBean.setCacheOperationSources(...);
          factoryBean.setTarget(new ThirdPartyClass());
      
          return factoryBean;
      }
      

      Unfortunately, this does not work because the internal CacheInterceptor's afterSingletonesInitialized() method never gets called, thereby setting the initialized bit and therefore allowing the caching behavior/logic to be evaluated. As result, the target object's original operation is always invoked, forgoing any caching behavior, making any caching proxy configuration useless.

      This can be fixed by having CacheProxyFactoryBean implement the SmartInitializingSingleton interface along with BeanFactoryAware and delegating said operations to the internal CacheInterceptor.

      See Pull Request for fix.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              jblum John Blum
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 37 weeks, 1 day ago