From fd6d988518296cc22e08f49ea2aec9d40a10bd8d Mon Sep 17 00:00:00 2001 From: eugenp Date: Mon, 22 Jun 2015 11:33:35 +0100 Subject: [PATCH] cleanup work --- .../baeldung/caching/config/MyAppConfig.java | 4 ++ .../baeldung/caching/example/Customer.java | 31 +++++++----- .../caching/example/CustomerDataService.java | 30 +++++++----- .../org/baeldung/caching/resouces/config.xml | 48 ++++++++++--------- .../test/SpringCachingBehaviorTest.java | 28 ++++++----- 5 files changed, 83 insertions(+), 58 deletions(-) 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 index d02727b421..467e50c15e 100644 --- a/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java +++ b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java @@ -1,3 +1,7 @@ +package org.baeldung.caching.config; + +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java index f4e6accae0..a57f08e35b 100644 --- a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -1,39 +1,48 @@ +package org.baeldung.caching.example; public class Customer { private int id; private String name; - private String customerAddress; + private String address; - public Customer(){ + public Customer() { + super(); } - Customer(String name, String address){ + public Customer(final String name, final String address) { this.name = name; - this.customerAddress = address; + this.address = address; } + // + public int getId() { return id; } - public void setId(int id) { + public void setId(final int id) { this.id = id; } public String getName() { return name; } - - public void setName(String name) { + + public void setName(final String name) { this.name = name; } - public String getCustomerAddress() { - return this.customerAddress; + public String getAddress() { + return address; } - public void setCustomerAddress(String address) { - this.customerAddress = this.name + "," + address; + public void setAddress(final String address) { + this.address = address; } + + public void setCustomerAddress(final String address) { + this.address = name + "," + address; + } + } 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 625caa802e..4c1c97adb8 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 @@ -2,12 +2,15 @@ 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; import org.springframework.cache.annotation.Cacheable; -import org.springframework.context.ApplicationContext; +import org.springframework.cache.annotation.Caching; import org.springframework.stereotype.Component; @Component -@CacheConfig("addressDemo") +@CacheConfig(cacheNames = { "addressDemo" }) public class CustomerDataService { @Autowired @@ -20,8 +23,8 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @Cacheable("addresses", “directory”) - public String getAddress1(Customer customer) { + @Cacheable({ "addresses", "directory" }) + public String getAddress1(final Customer customer) { return customer.getAddress(); } @@ -32,8 +35,8 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @CacheEvict(value="addresses", allEntries=true) - public String getAddress2(Customer customer) { + @CacheEvict(value = "addresses", allEntries = true) + public String getAddress2(final Customer customer) { return customer.getAddress(); } @@ -44,8 +47,8 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) - public String getAddress3(Customer customer) { + @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value = "directory", key = "customer.name") }) + public String getAddress3(final Customer customer) { return customer.getAddress(); } @@ -55,8 +58,9 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @Cacheable // parameter not required as we have declared it using @CacheConfig - public String getAddress4(Customer customer) { + @Cacheable + // parameter not required as we have declared it using @CacheConfig + public String getAddress4(final Customer customer) { return customer.getAddress(); } @@ -66,9 +70,9 @@ public class CustomerDataService { * @param customer the customer * @return the address */ - @CachePut(value="addresses", condition=”#customer.name=’Tom’”) - @CachePut(value="addresses", unless=”#result.length>64”) - public String getAddress5(Customer customer) { + @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/resouces/config.xml b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml index 2ca26f4468..fd54e85164 100644 --- a/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml +++ b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml @@ -1,30 +1,34 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" + xmlns:context="http://www.springframework.org/schema/context" + xmlns:aop="http://www.springframework.org/schema/aop" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd + http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd + http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd + "> - + - - + + + + - - + + + + + + - - - - - - - - - - - + + + + diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java index 989049bee0..c39e4bfa87 100644 --- a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -1,21 +1,25 @@ package org.baeldung.caching.test; -import org.springframework.beans.factory.annotation.Autowired; +import static org.junit.Assert.fail; + +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 static org.junit.Assert.*; -import org.junit.Test; @Component public class SpringCachingBehaviorTest { - @Test - public void testCaching() { - @SuppressWarnings("resource") - ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); - example = context.getBean(CustomerDataService.class); - Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); - example.getAddress(cust); - fail("Unable to instantiate the CustomerDataService"); - } + @Test + public void testCaching() { + @SuppressWarnings("resource") + final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); + final CustomerDataService service = context.getBean(CustomerDataService.class); + + final Customer cust = new Customer("Tom", "67-2, Downing Street, NY"); + service.getAddress1(cust); + fail("Unable to instantiate the CustomerDataService"); + } + }