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
@EnableCaching

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -1,30 +1,34 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
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
">
<cache:annotation-driven />
<cache:annotation-driven />
<context:annotation-config/>
<bean class="org.baeldung.caching.config.myAppConfig"/>
<context:annotation-config />
<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.-->
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
<!-- define caching behavior -->
<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 -->
<cache:advice id="cachingBehavior" cache-manager="cacheManager">
<cache:caching cache="addresses">
<cache:cacheable method="getAddress" key="#customer.name"/>
</cache:caching>
</cache:advice>
<!-- 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>
<!-- 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>

View File

@ -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");
}
}