commit
f38af8b345
|
@ -32,6 +32,20 @@
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- aspectj -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-aspects</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-orm</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -47,7 +47,7 @@ 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(final Customer customer) {
|
public String getAddress3(final Customer customer) {
|
||||||
return customer.getAddress();
|
return customer.getAddress();
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ 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(final Customer customer) {
|
public String getAddress5(final Customer customer) {
|
||||||
return customer.getAddress();
|
return customer.getAddress();
|
||||||
|
|
|
@ -12,11 +12,21 @@
|
||||||
<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. -->
|
<!-- the service that you wish to make cacheable. -->
|
||||||
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService" />
|
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService" />
|
||||||
|
|
||||||
|
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
|
||||||
|
<property name="caches">
|
||||||
|
<set>
|
||||||
|
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="directory"/>
|
||||||
|
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addresses"/>
|
||||||
|
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addressDemo"/>
|
||||||
|
</set>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
<!-- define caching behavior -->
|
<!-- define caching behavior -->
|
||||||
<cache:advice id="cachingBehavior" cache-manager="cacheManager">
|
<cache:advice id="cachingBehavior" cache-manager="cacheManager">
|
||||||
<cache:caching cache="addresses">
|
<cache:caching cache="addresses">
|
||||||
|
@ -24,11 +34,12 @@
|
||||||
</cache:caching>
|
</cache:caching>
|
||||||
</cache:advice>
|
</cache:advice>
|
||||||
|
|
||||||
|
|
||||||
<!-- apply the behavior to all the implementations of CustomerDataService
|
<!-- apply the behavior to all the implementations of CustomerDataService
|
||||||
interface -->
|
interface -->
|
||||||
<aop:config>
|
<aop:config>
|
||||||
<aop:advisor advice-ref="cachingBehavior"
|
<aop:advisor advice-ref="cachingBehavior"
|
||||||
pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))" />
|
pointcut="execution(* org.baeldung.caching.example.CustomerDataService.*(..))" />
|
||||||
</aop:config>
|
</aop:config>
|
||||||
|
|
||||||
</beans>
|
</beans>
|
|
@ -1,7 +1,5 @@
|
||||||
package org.baeldung.caching.test;
|
package org.baeldung.caching.test;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import org.baeldung.caching.example.Customer;
|
import org.baeldung.caching.example.Customer;
|
||||||
import org.baeldung.caching.example.CustomerDataService;
|
import org.baeldung.caching.example.CustomerDataService;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -11,6 +9,7 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class SpringCachingBehaviorTest {
|
public class SpringCachingBehaviorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCaching() {
|
public void testCaching() {
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
|
@ -19,7 +18,9 @@ public class SpringCachingBehaviorTest {
|
||||||
|
|
||||||
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
|
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
|
||||||
service.getAddress1(cust);
|
service.getAddress1(cust);
|
||||||
fail("Unable to instantiate the CustomerDataService");
|
service.getAddress1(cust);
|
||||||
|
|
||||||
|
// fail("Unable to instantiate the CustomerDataService");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue