Enabling the caching proxy using Annotation

As you already know, Spring provides lots of features, but they are, mostly, disabled. You must enable these feature before using it. If you want to use Spring's cache abstraction in your application, you have to enable this feature. If you are using Java configuration, you can enable cache abstraction of Spring by adding the @EnableCaching annotation to one of your configuration classes. The following configuration class shows the @EnableCaching annotation:

    package com.packt.patterninspring.chapter9.bankapp.config; 
 
    import org.springframework.cache.CacheManager; 
    import org.springframework.cache.annotation.EnableCaching; 
    import org.springframework.cache.concurrent.
ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages=
{"com.packt.patterninspring.chapter9.bankapp"}) @EnableCaching //Enable caching public class AppConfig { @Bean public AccountService accountService() { ... } //Declare a cache manager @Bean public CacheManager cacheManager() { CacheManager cacheManager = new ConcurrentMapCacheManager(); return cacheManager; } }

In the preceding Java configuration file, we added the @EnableCaching annotation to the configuration class AppConfig.java; this annotation indicates to the Spring Framework to enable Spring cache behavior for the application.

Let's now look at how to enable Spring's cache abstraction by using XML configuration.

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

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