diff --git a/spring-all/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-all/.settings/org.eclipse.wst.common.project.facet.core.xml index 9ca0d1c1b7..991897a4ac 100644 --- a/spring-all/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/spring-all/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,6 +1,6 @@ - + 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 new file mode 100644 index 0000000000..4153ec9636 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java @@ -0,0 +1,29 @@ +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.Configuration; + +@Configuration +@EnableCaching +public class CachingConfig { + + @Bean + public CustomerDataService customerDataService() { + return new CustomerDataService(); + } + + @Bean + public CacheManager cacheManager() { + final SimpleCacheManager cacheManager = new SimpleCacheManager(); + cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("directory"), new ConcurrentMapCache("addresses"))); + return cacheManager; + } + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java deleted file mode 100644 index 467e50c15e..0000000000 --- a/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.baeldung.caching.config; - -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.annotation.Configuration; - -@Configuration -@EnableCaching -public class MyAppConfig { - // Your configuration code goes here. -} 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 86026de93a..fe4de5d282 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 @@ -10,12 +10,18 @@ import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Component; @Component -@CacheConfig(cacheNames = { "addressDemo" }) +@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) { + return customer.getAddress(); + } + /** * The method returns the customer's address, only it doesn't find it the cache- addresses and directory. diff --git a/spring-all/src/main/resources/config.xml b/spring-all/src/main/resources/config.xml index 244e9027ec..23458539b0 100644 --- a/spring-all/src/main/resources/config.xml +++ b/spring-all/src/main/resources/config.xml @@ -10,8 +10,7 @@ - - + @@ -21,7 +20,6 @@ - @@ -34,7 +32,7 @@ - + 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 index 0b3edaffb3..a4a3733dd8 100644 --- a/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ b/spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -1,11 +1,11 @@ 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.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @Component public class SpringCachingBehaviorTest { @@ -13,12 +13,16 @@ public class SpringCachingBehaviorTest { @Test public void testCaching() { @SuppressWarnings("resource") - final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); + 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.getAddress1(cust); - service.getAddress1(cust); + service.getAddress(cust); + service.getAddress(cust); // fail("Unable to instantiate the CustomerDataService"); }