cleanup work

This commit is contained in:
eugenp 2015-06-22 11:33:35 +01:00
parent e4a301dd99
commit fd6d988518
5 changed files with 83 additions and 58 deletions

View File

@ -1,3 +1,7 @@
package org.baeldung.caching.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableCaching @EnableCaching

View File

@ -1,39 +1,48 @@
package org.baeldung.caching.example;
public class Customer { public class Customer {
private int id; private int id;
private String name; 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.name = name;
this.customerAddress = address; this.address = address;
} }
//
public int getId() { public int getId() {
return id; return id;
} }
public void setId(int id) { public void setId(final int id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(final String name) {
this.name = name; this.name = name;
} }
public String getCustomerAddress() { public String getAddress() {
return this.customerAddress; return address;
} }
public void setCustomerAddress(String address) { public void setAddress(final String address) {
this.customerAddress = this.name + "," + address; this.address = address;
} }
public void setCustomerAddress(final String address) {
this.address = name + "," + address;
}
} }

View File

@ -2,12 +2,15 @@ package org.baeldung.caching.example;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager; 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.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext; import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@CacheConfig("addressDemo") @CacheConfig(cacheNames = { "addressDemo" })
public class CustomerDataService { public class CustomerDataService {
@Autowired @Autowired
@ -20,8 +23,8 @@ public class CustomerDataService {
* @param customer the customer * @param customer the customer
* @return the address * @return the address
*/ */
@Cacheable("addresses", directory) @Cacheable({ "addresses", "directory" })
public String getAddress1(Customer customer) { public String getAddress1(final Customer customer) {
return customer.getAddress(); return customer.getAddress();
} }
@ -32,8 +35,8 @@ public class CustomerDataService {
* @param customer the customer * @param customer the customer
* @return the address * @return the address
*/ */
@CacheEvict(value="addresses", allEntries=true) @CacheEvict(value = "addresses", allEntries = true)
public String getAddress2(Customer customer) { public String getAddress2(final Customer customer) {
return customer.getAddress(); return customer.getAddress();
} }
@ -44,8 +47,8 @@ public class CustomerDataService {
* @param customer the customer * @param customer the customer
* @return the address * @return the address
*/ */
@Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") }) @Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value = "directory", key = "customer.name") })
public String getAddress3(Customer customer) { public String getAddress3(final Customer customer) {
return customer.getAddress(); return customer.getAddress();
} }
@ -55,8 +58,9 @@ public class CustomerDataService {
* @param customer the customer * @param customer the customer
* @return the address * @return the address
*/ */
@Cacheable // parameter not required as we have declared it using @CacheConfig @Cacheable
public String getAddress4(Customer customer) { // parameter not required as we have declared it using @CacheConfig
public String getAddress4(final Customer customer) {
return customer.getAddress(); return customer.getAddress();
} }
@ -66,9 +70,9 @@ public class CustomerDataService {
* @param customer the customer * @param customer the customer
* @return the address * @return the address
*/ */
@CachePut(value="addresses", condition=#customer.name=Tom) @CachePut(value = "addresses", condition = "#customer.name='Tom'")
@CachePut(value="addresses", unless=#result.length>64) // @CachePut(value = "addresses", unless = "#result.length>64")
public String getAddress5(Customer customer) { public String getAddress5(final Customer customer) {
return customer.getAddress(); return customer.getAddress();
} }
} }

View File

@ -1,30 +1,34 @@
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd xsi:schemaLocation="
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> 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
">
<cache:annotation-driven /> <cache:annotation-driven />
<context:annotation-config/> <context:annotation-config />
<bean class="org.baeldung.caching.config.myAppConfig"/> <bean class="org.baeldung.caching.config.myAppConfig" />
<!-- the service that you wish to make cacheable. -->
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService" />
<!-- the service that you wish to make cacheable.--> <!-- define caching behavior -->
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/> <cache:advice id="cachingBehavior" cache-manager="cacheManager">
<cache:caching cache="addresses">
<cache:cacheable method="getAddress" key="#customer.name" />
</cache:caching>
</cache:advice>
<!-- define caching behavior --> <!-- apply the behavior to all the implementations of CustomerDataService
<cache:advice id="cachingBehavior" cache-manager="cacheManager"> interface -->
<cache:caching cache="addresses"> <aop:config>
<cache:cacheable method="getAddress" key="#customer.name"/> <aop:advisor advice-ref="cachingBehavior"
</cache:caching> pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))" />
</cache:advice> </aop:config>
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
<aop:config>
<aop:advisor advice-ref="cachingBehavior"
pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))"/>
</aop:config>
</beans> </beans>

View File

@ -1,21 +1,25 @@
package org.baeldung.caching.test; 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.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import static org.junit.Assert.*;
import org.junit.Test;
@Component @Component
public class SpringCachingBehaviorTest { public class SpringCachingBehaviorTest {
@Test @Test
public void testCaching() { public void testCaching() {
@SuppressWarnings("resource") @SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
example = context.getBean(CustomerDataService.class); final CustomerDataService service = context.getBean(CustomerDataService.class);
Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
example.getAddress(cust); final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
fail("Unable to instantiate the CustomerDataService"); service.getAddress1(cust);
} fail("Unable to instantiate the CustomerDataService");
}
} }