The @CacheConfig annotation

Spring's cache abstraction allows you to annotate @CacheConfig at the class level to avoid repeated mentioning in each method. In some cases, applying customizations of the caches to all methods can be quite tedious. Here, you can use the @CacheConfig annotation to all operations of the class. For example:

     @CacheConfig("accountCache ") 
     public class AccountServiceImpl implements AccountService { 
 
      @Cacheable 
      public Account findAccount(Long accountId) { 
        return (Account) accountRepository.findOne(accountId); 
      } 
    } 

You can see in the preceding code snippet that the @CacheConfig annotation is used at the class level, and it allows you to share the accountCache cache with all the cacheable methods.

Since Spring's cache abstraction module uses proxies, you should use the cache annotations only with public visibility methods. In all non-public methods, these annotations do not raise any error, but non-public methods annotated with these annotations do not show any caching behaviors.

We have already seen that Spring also offers XML namespace to configure and implement cache in a Spring application. Let's see how in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset