diff --git a/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java index 4153ec9636..c995bca68d 100644 --- a/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java +++ b/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java @@ -2,23 +2,19 @@ package org.baeldung.caching.config; import java.util.Arrays; -import org.baeldung.caching.example.CustomerDataService; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching +@ComponentScan("org.baeldung.caching.example") public class CachingConfig { - @Bean - public CustomerDataService customerDataService() { - return new CustomerDataService(); - } - @Bean public CacheManager cacheManager() { final SimpleCacheManager cacheManager = new SimpleCacheManager(); diff --git a/spring-all/src/main/java/org/baeldung/caching/example/AbstractService.java b/spring-all/src/main/java/org/baeldung/caching/example/AbstractService.java new file mode 100644 index 0000000000..38b5a5a3ec --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/AbstractService.java @@ -0,0 +1,76 @@ +package org.baeldung.caching.example; + +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.Caching; + +public abstract class AbstractService { + + // this method configuration is equivalent to xml configuration + @Cacheable(value = "addresses", key = "#customer.name") + public String getAddress(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + only it doesn't find it the cache- addresses and directory. + * + * @param customer the customer + * @return the address + */ + @Cacheable({ "addresses", "directory" }) + public String getAddress1(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + but refreshes all the entries in the cache to load new ones. + * + * @param customer the customer + * @return the address + */ + @CacheEvict(value = "addresses", allEntries = true) + public String getAddress2(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method returns the customer's address, + but not before selectively evicting the cache as per specified paramters. + * + * @param customer the customer + * @return the address + */ + @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value = "directory", key = "#customer.name") }) + public String getAddress3(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method uses the class level cache to look up for entries. + * + * @param customer the customer + * @return the address + */ + @Cacheable + // parameter not required as we have declared it using @CacheConfig + public String getAddress4(final Customer customer) { + return customer.getAddress(); + } + + /** + * The method selectively caches the results that meet the predefined criteria. + * + * @param customer the customer + * @return the address + */ + @CachePut(value = "addresses", condition = "#customer.name=='Tom'") + // @CachePut(value = "addresses", unless = "#result.length>64") + public String getAddress5(final Customer customer) { + return customer.getAddress(); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java index fe4de5d282..1c595057a8 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -1,7 +1,5 @@ package org.baeldung.caching.example; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; @@ -13,9 +11,6 @@ import org.springframework.stereotype.Component; @CacheConfig(cacheNames = { "addresses" }) public class CustomerDataService { - @Autowired - CacheManager cacheManager; - // this method configuration is equivalent to xml configuration @Cacheable(value = "addresses", key = "#customer.name") public String getAddress(final Customer customer) { diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java new file mode 100644 index 0000000000..a5ded7daf9 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerServiceWithParent.java @@ -0,0 +1,10 @@ +package org.baeldung.caching.example; + +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.stereotype.Component; + +@Component +@CacheConfig(cacheNames = { "addresses" }) +public class CustomerServiceWithParent extends AbstractService { + // +} diff --git a/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java deleted file mode 100644 index a4a3733dd8..0000000000 --- a/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.baeldung.caching.test; - -import org.baeldung.caching.config.CachingConfig; -import org.baeldung.caching.example.Customer; -import org.baeldung.caching.example.CustomerDataService; -import org.junit.Test; -import org.springframework.stereotype.Component; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; - -@Component -public class SpringCachingBehaviorTest { - - @Test - public void testCaching() { - @SuppressWarnings("resource") - final - // final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(CachingConfig.class); - context.refresh(); - final CustomerDataService service = context.getBean(CustomerDataService.class); - - final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); - service.getAddress(cust); - service.getAddress(cust); - - // fail("Unable to instantiate the CustomerDataService"); - } - -} diff --git a/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java b/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java new file mode 100644 index 0000000000..8c4ebaa7ec --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingIntegrationTest.java @@ -0,0 +1,71 @@ +package org.baeldung.caching.test; + +import org.baeldung.caching.config.CachingConfig; +import org.baeldung.caching.example.Customer; +import org.baeldung.caching.example.CustomerDataService; +import org.baeldung.caching.example.CustomerServiceWithParent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { CachingConfig.class }, loader = AnnotationConfigContextLoader.class) +public class SpringCachingIntegrationTest { + + @Autowired + private CustomerDataService service; + + @Autowired + private CustomerServiceWithParent serviceWithParent; + + // + + @Test + public void whenGettingAddress_thenCorrect() { + final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); + service.getAddress(cust); + service.getAddress(cust); + + service.getAddress1(cust); + service.getAddress1(cust); + + service.getAddress2(cust); + service.getAddress2(cust); + + service.getAddress3(cust); + service.getAddress3(cust); + + service.getAddress4(cust); + service.getAddress4(cust); + + service.getAddress5(cust); + service.getAddress5(cust); + } + + @Test + public void givenUsingServiceWithParent_whenGettingAddress_thenCorrect() { + final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); + + serviceWithParent.getAddress(cust); + serviceWithParent.getAddress(cust); + + serviceWithParent.getAddress1(cust); + serviceWithParent.getAddress1(cust); + + serviceWithParent.getAddress2(cust); + serviceWithParent.getAddress2(cust); + + serviceWithParent.getAddress3(cust); + serviceWithParent.getAddress3(cust); + + // serviceWithParent.getAddress4(cust); + // serviceWithParent.getAddress4(cust); + + serviceWithParent.getAddress5(cust); + serviceWithParent.getAddress5(cust); + } + +}